我需要为每次运行 AES(在 JAVA 中)生成不同的令牌,因为我所做的是System.currentTimeMillis()
在 java 中使用当前系统时间附加要加密的字符串,并使用管道字符“|”分隔它们。但是面临的问题是每次运行的加密字符串都是相同的,并且在解密时我得到了正确的字符串。为什么会这样?
加密代码:
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
public class AESEncryptor {
private static final String ALGO = "AES";
private final static byte[] keyValue =new byte[] { 'T', 'h', 'e', 'B', 'e', 's', 't','S', 'e', 'c', 'r','e', 't', 'K', 'e', 'y' };
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());
byte[] encryptedValue = Base64.encodeBase64(encVal);
String encryptedPass = new String (encryptedValue);
return encryptedPass;
}
public static String decrypt(String encryptedData) throws Exception {
Key key = generateKey();
Cipher c = Cipher.getInstance(ALGO);
c.init(Cipher.DECRYPT_MODE, key);
Base64.decodeBase64(encryptedData);
byte[] decordedValue = Base64.decodeBase64(encryptedData);
byte[] decValue = c.doFinal(decordedValue);
String decryptedValue = new String(decValue);
return decryptedValue;
}
private static Key generateKey() throws Exception {
Key key = new SecretKeySpec(keyValue, ALGO);
return key;
}
}
1st run :
argument passed to encrypt : somepassword|1364311519852
encrypted string : 5pQ1kIC+8d81AD7zbLOZA==(encrypted string)
decrypted string : somepassword|1364311519852
2nd run :
argument passed to encrypt : somepassword|1364311695048
encrypted string : 5pQ1kIC+8d81AD7zbLOZA==(same encrypted string as before)
decrypted string : somepassword|1364311695048
有人可以帮助为什么会这样吗?