Java AES解密始终为空
我尝试使用salt对加密的消息进行解密,但它总是返回Java AES解密始终为空,java,cryptography,aes,salt,Java,Cryptography,Aes,Salt,我尝试使用salt对加密的消息进行解密,但它总是返回null值。有人能看看我哪里做错了吗 public static String decryptMessage(String encryptedMessage, String salt) { String decryptedMessage = null; String valueToDecrypt = encryptedMessage; try { Key key = generateKey(); Cipher c
null
值。有人能看看我哪里做错了吗
public static String decryptMessage(String encryptedMessage, String salt) {
String decryptedMessage = null;
String valueToDecrypt = encryptedMessage;
try {
Key key = generateKey();
Cipher c = Cipher.getInstance(ALGORITHM);
c.init(Cipher.DECRYPT_MODE, key);
for (int i = 0; i < ITERATIONS; i++) {
byte[] decodedValue = Base64.decodeBase64(valueToDecrypt);
byte[] decVal = c.doFinal(decodedValue);
decryptedMessage = new String(decVal).substring(salt.length());
valueToDecrypt = decryptedMessage;
}
} catch (Exception e) {
e.printStackTrace();
}
return decryptedMessage;
}
**EDIT:**
publicstaticstringdecryptedmessage(stringencryptedmessage,stringsalt){
字符串decryptedMessage=null;
String valueToDecrypt=encryptedMessage;
试一试{
Key=generateKey();
Cipher c=Cipher.getInstance(算法);
c、 init(Cipher.DECRYPT_模式,密钥);
对于(int i=0;i
这里是相应的加密方法,我假设它是有效的
private static final String ALGORITHM = "AES";
private static final int ITERATIONS = 5;
private static final byte[] keyValue = new byte[] { '1', '2', '3', '4',
'5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6' };
public static String encryptMessage(String message, String salt) {
String encMessage = null;
byte[] encVal = null;
String messageWithSalt = null;
try {
Key key = generateKey();
Cipher c = Cipher.getInstance(ALGORITHM);
c.init(Cipher.ENCRYPT_MODE, key);
for (int i = 0; i < ITERATIONS; i++) {
messageWithSalt = salt + encMessage;
encVal = c.doFinal(messageWithSalt.getBytes());
byte[] encryptedValue = new Base64().encode(encVal);
encMessage = new String(encryptedValue);
}
} catch (Exception e) {
e.printStackTrace();
}
return encMessage;
}
private static final String ALGORITHM=“AES”;
私有静态最终整数迭代=5;
私有静态最终字节[]keyValue=新字节[]{'1','2','3','4',
'5', '6', '7', '8', '9', '0', '1', '2', '3', '4', '5', '6' };
公共静态字符串加密消息(字符串消息、字符串盐){
字符串encMessage=null;
字节[]encVal=null;
字符串messageWithSalt=null;
试一试{
Key=generateKey();
Cipher c=Cipher.getInstance(算法);
c、 init(Cipher.ENCRYPT_模式,密钥);
对于(int i=0;i
PS:迭代不是0 我认为您的方法可能长期被破坏或命名错误。每次解密时都会生成一个密钥,而您应该拥有一个与用于加密的密钥相匹配的预先存在的密钥 您还传递了一个“salt”值-注意:这是一个通常为哈希保留的术语-然后您将完全忽略它,除了使用大小作为结果的截断长度
当然,我在上面看到的并不是以一种合理的方式解密任何东西。如果您能够准确地描述您想要实现的目标,我们可能会更正代码,或者为您指出执行该任务的同行评审方法(可能已经在标准libs中实现了)。好吧,我发现了错误。这是在加密方法<加密过程开始前,代码>加密消息为
null
String encMessage=message
成功了。因此,加密方法是:
public static String encryptMessage(String message, String salt) {
String encMessage = message;
byte[] encVal = null;
String messageWithSalt = null;
try {
Key key = generateKey();
Cipher c = Cipher.getInstance(ALGORITHM);
c.init(Cipher.ENCRYPT_MODE, key);
for (int i = 0; i < ITERATIONS; i++) {
messageWithSalt = salt + encMessage;
encVal = c.doFinal(messageWithSalt.getBytes());
byte[] encryptedValue = new Base64().encode(encVal);
encMessage = new String(encryptedValue);
}
} catch (Exception e) {
e.printStackTrace();
}
return encMessage;
}
publicstaticstringencryptmessage(stringmessage,stringsalt){
字符串encMessage=消息;
字节[]encVal=null;
字符串messageWithSalt=null;
试一试{
Key=generateKey();
Cipher c=Cipher.getInstance(算法);
c、 init(Cipher.ENCRYPT_模式,密钥);
对于(int i=0;i
您忘记发布相应的加密代码。@Leigh好吧,使用该catch
语句,您可以假设它在任何解密错误时也会返回null:)不过,很严重,每一行都至少有一个错误,可能除了Cipher getInstance()
方法之外,我想当我们知道算法的值时,我们就会知道了。