我有一个发送给我的密码,该密码使用基于数字 ID 的 MD5 散列的 AES 加密
从请求中,我可以使用其他属性获取数据库中的 id 所以在服务器端,我需要获取 id,基于该 id 获取 MD5 哈希,并使用 AES 算法和生成的 MD5 解密密码哈希。
我正在使用以下代码来获取 MD5 哈希
try {
byte[] bytesOfMessage = id.getBytes("UTF-8");
log.error "bytesOfMessage length: " + bytesOfMessage.length
MessageDigest md = MessageDigest.getInstance("MD5");
byte[] thedigest = md.digest(bytesOfMessage);
md5Value = new String(thedigest);
log.error "md5Value length: " + md5Value.length()
log.error "md5Value bytes length: " + md5Value.getBytes().length
} catch (UnsupportedEncodingException e) {
log.error "[getMD5EncryptionKey]UnsupportedEncodingException: " + e;
} catch (NoSuchAlgorithmException e) {
log.error "[getMD5EncryptionKey]NoSuchAlgorithmException: " + e;
}
md5Value 长度为 16,基于 id 1 但是当我从这个 md5value 获取字节时,有 34 个字节
当我使用此 MD5 哈希和 javax.crypto.Cipher 库解密密码时,我收到以下消息
java.security.InvalidKeyException:无效的 AES 密钥长度:34 字节
有什么想法我在这里做错了吗?
我用来解密消息的代码如下
try {
byte [] encryptionKeyBytes = md5EncryptionKey.getBytes("UTF-8");
Key key = new SecretKeySpec(encryptionKeyBytes, "AES");
Cipher c = Cipher.getInstance("AES");
c.init(Cipher.DECRYPT_MODE, key);
byte[] decodedValue = new Base64().decode(encryptedData);
byte[] decValue = c.doFinal(decodedValue);
String decryptedValue = new String(decValue);
return decryptedValue;
} catch (InvalidKeyException e) {
log.error "[getDecryptedValue] InvalidKeyException: " + e
} catch (IllegalBlockSizeException e) {
log.error "[getDecryptedValue] InvalidKeyException: " + e
} catch (BadPaddingException e) {
log.error "[getDecryptedValue] InvalidKeyException: " + e
} catch (NoSuchAlgorithmException e) {
log.error "[getDecryptedValue] InvalidKeyException: " + e
} catch (NoSuchPaddingException e) {
log.error "[getDecryptedValue] InvalidKeyException: " + e
} catch (Exception e) {
log.error "[getDecryptedValue] InvalidKeyException: " + e
}