【发布时间】:2017-05-31 01:13:29
【问题描述】:
我必须在我的程序中实现基本加密。我可以使用被客户端拒绝的 Base64。所以我使用以下方法。我面临的问题是加密中有特殊字符导致异常。我可以更改此代码以某种方式加密为没有特殊字符的纯文本。
protected static byte[] encrypt(String text)
{
try
{
String key = "6589745268754125";
// Create key and cipher
Key aesKey = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");
// encrypt the text
cipher.init(Cipher.ENCRYPT_MODE, aesKey);
byte[] encrypted = cipher.doFinal(text.getBytes());
return encrypted;
}
catch(Exception ex)
{
WriteLog("Encryption Failed");
WriteLog(ex.getMessage());
return null;
}
}
protected static String decrypt(byte[] pass)
{
try
{
String key = "6589745268754125";
// Create key and cipher
Key aesKey = new SecretKeySpec(key.getBytes(), "AES");
Cipher cipher = Cipher.getInstance("AES");
// decrypt the text
cipher.init(Cipher.DECRYPT_MODE, aesKey);
String decrypted = new String(cipher.doFinal(pass));
return decrypted;
}
catch(Exception ex)
{
WriteLog("Encryption Failed");
WriteLog(ex.getMessage());
return null;
}
}
异常消息显示“未正确填充最终块” javax.crypto.BadPaddingException:给定最终块未正确填充
【问题讨论】:
-
什么是问题字符?有什么例外?哪里有异常?
-
Base64 是一种编码,而不是一种加密。如果你想要加密字节的明文表示,base64 是一种很好的编码方式。
-
我知道,我尝试通过编码来获取,但现在我必须进行加密
-
“给定的最终块没有正确填充”
-
做一些搜索,伙计..
标签: java encryption