我有被髮送到我這是使用AES基於數字ID生成MD5哈希
從請求中的MD5哈希加密的密碼,我可以在我與其他屬性數據庫中獲取的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長度是基於1的ID 16,但是當我從這個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
}
wasnt知道這個庫,用它和它做的伎倆。歡呼傢伙 – Damien