java 如何在java中将数据加密为没有特殊字符的纯文本
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30092044/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to Encrypt data to a Plain text without Special character in java
提问by Sachi-17
I need a encryption algorithm by whick I can encrypt the data to a simple plain text.
我需要一种加密算法,通过它我可以将数据加密为简单的纯文本。
Currently i am using AESencrypting algorithmwhich converts to encrypt string contains Special character.
目前我正在使用AES加密算法,该算法转换为包含特殊字符的加密字符串。
Here if i send this string through a query string in url I am missing some character like- "+".
在这里,如果我通过 url 中的查询字符串发送此字符串,我会丢失一些字符,例如“+”。
So i need a encrypt logic which is secure and contains only alphabet.
所以我需要一个安全且仅包含字母表的加密逻辑。
Here is my encryption logic :
这是我的加密逻辑:
public static String encrypt(String Data) throws Exception {
Key key = generateKey();
Cipher c = Cipher.getInstance(ALGO);
c.init(Cipher.ENCRYPT_MODE, key);
byte[] encVal = c.doFinal(Data.getBytes());
String encryptedValue = new BASE64Encoder().encode(encVal);
return encryptedValue;
}
@SuppressWarnings("restriction")
public static String decrypt(String encryptedData) throws Exception {
Key key = generateKey();
Cipher c = Cipher.getInstance(ALGO);
c.init(Cipher.DECRYPT_MODE, key);
byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData);
byte[] decValue = c.doFinal(decordedValue);
String decryptedValue = new String(decValue);
return decryptedValue;
}
Here I am getting an encrypted String "TEj+TWBQExpz8/p5SAjIhA=="
在这里我得到一个加密的字符串“ TEj+TWBQExpz8/p5SAjIhA==”
while I am sending through query string as
当我通过查询字符串发送时
localhost:8080/myproject/home?code=TEj+TWBQExpz8/p5SAjIhA==
本地主机:8080/myproject/home?code=TEj+TWBQExpz8/p5SAjIhA==
i am getting the String in controlleras-
"TEj TWBQExpz8/p5SAjIhA=="
我得到字符串在控制器原样
“ TEJ TWBQExpz8 / p5SAjIhA ==”
"+" symbol is missing thats why i am getting issue while decryption.
缺少“+”符号,这就是为什么我在解密时遇到问题。
Please suggest any new Algorithm or any solution to avoid the Special character.
请提出任何新算法或任何解决方案以避免特殊字符。
Thank you.
谢谢你。
回答by Kai Mechel
You can encode your crypted part with URLEncoder
您可以使用 URLEncoder 对加密部分进行编码
URLEncoder.encode("TEj+TWBQExpz8/p5SAjIhA==", "UTF-8")
to make it valid for the URL.
使其对 URL 有效。
http://docs.oracle.com/javase/8/docs/api/java/net/URLEncoder.html
http://docs.oracle.com/javase/8/docs/api/java/net/URLEncoder.html
回答by jbarrueta
You should try Apache Commons Base64 URL safe encoder, which will generate what you need.
您应该尝试 Apache Commons Base64 URL safe encoder,它将生成您需要的内容。
Hope it helps,
希望能帮助到你,
Jose Luis
何塞·路易斯
回答by Sach141
You have to use URLEncoder
to encode the encrypted value, before sending it through URL.
URLEncoder
在通过 URL 发送之前,您必须使用对加密值进行编码。
and at decryption side, you have to use URLDecoder
first to decode received data.
在解密方面,您必须首先使用URLDecoder
来解码接收到的数据。
You can do following in your code to achieve this :
您可以在代码中执行以下操作来实现此目的:
public static String encrypt(String Data) throws Exception {
Key key = generateKey();
Cipher c = Cipher.getInstance(ALGO);
c.init(Cipher.ENCRYPT_MODE, key);
byte[] encVal = c.doFinal(Data.getBytes());
String encryptedValue = new BASE64Encoder().encode(encVal);
String urlEncodeddata=URLEncoder.encode(encryptedValue,"UTF-8");
return urlEncodeddata;
}
@SuppressWarnings("restriction")
public static String decrypt(String encryptedData) throws Exception {
Key key = generateKey();
Cipher c = Cipher.getInstance(ALGO);
c.init(Cipher.DECRYPT_MODE, key);
String urlDecodedData=URLDecoder.decode(encryptedData, "UTF-8");
byte[] decordedValue = new BASE64Decoder().decodeBuffer(urlDecodedData);
byte[] decValue = c.doFinal(decordedValue);
String decryptedValue = new String(decValue);
return decryptedValue;
}
回答by shrikant katakdhond
Use only URLEncode
no need to change code.
仅使用URLEncode
无需更改代码。
String encodedStr = URLEncoder.encode(encrypt("XYZ"), "UTF-8");
String encodedStr = URLEncoder.encode(encrypt("XYZ"), "UTF-8");
And there is no need to use URLDecode for decoding url as you can use normal
decrypt(encodedStr);
并且不需要使用 URLDecode 来解码 url,因为您可以正常使用
decrypt(encodedStr);
回答by Saket Yadav
You have to use this in C# Application to encode the encrypted value, before sending it through URL. Decryption side, you have to use UrlDecode first to decode QueryString data.
在通过 URL 发送加密值之前,您必须在 C# 应用程序中使用它对加密值进行编码。解密方面,必须先使用 UrlDecode 对 QueryString 数据进行解码。
Server.UrlEncode("");
Server.UrlDecode("");
回答by Ali Mert ?zdemir
I had same issue in Spring based application and I solved the problem with Base64 Encoding/Decoding process.
我在基于 Spring 的应用程序中遇到了同样的问题,我用 Base64 编码/解码过程解决了这个问题。
Encoding:
编码:
String encryptedString = encrypt(stringData);
byte[] encoded = Base64.getEncoder().encode(encryptedString.getBytes(StandardCharsets.UTF_8));
String base64Encoded = new String(encoded);
Decoding:
解码:
byte[] decoded = Base64.getDecoder().decode(base64Encoded.getBytes(StandardCharsets.UTF_8));
String encryptedString = new String(decoded);
Than you can successfully decrypt "encryptedString" value on your decrypt method.
比您可以在您的解密方法上成功解密“encryptedString”值。
String decryptedString = decrypt(encryptedString);
I hope it helps.
我希望它有帮助。