生成MD5哈希

达米安

我有一个发送给我的密码,该密码使用基于数字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的长度是基于ID 1的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
    }
伊帕

问题在于字符串是十六进制的。我建议您使用apache commons-code程序包。那里有一个用于散列的实用程序类。

http://commons.apache.org/proper/commons-codec/apidocs/org/apache/commons/codec/digest/DigestUtils.html

然后,您可以使用以下代码:

String md5 = DigestUtils.md5Hex(id);
// or
byte[] md5 = DigestUtils.md5(id);

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章