错误消息:使用填充密码解密时,输入长度必须是16的倍数

时间:2019-04-18 14:28:04

标签: java hex aes

此代码是AES算法的一部分,进行加密和解密,我有此方法将加密后的值返回为[Hex],当我将加密后的值作为解密方法的输入时,出现此错误: [使用填充密码解密时,输入长度必须是16的倍数],请提出任何修正建议?

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());
    // simply store the encoded byte array here
    byte[] bytes = Base64.getEncoder().encode(encVal);

    // loop over the bytes and append each byte as hex string
    StringBuilder sb = new StringBuilder(bytes.length * 2);
    for(byte b : bytes)
       sb.append(String.format("%02x", b));
    return sb.toString();
    }

public static String decrypt(String encryptedData) throws Exception {
        Key key = generateKey();
        Cipher c = Cipher.getInstance(ALGO);
        c.init(Cipher.DECRYPT_MODE, key);
        byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedData);
        byte[] decValue = c.doFinal(decordedValue);
        String decryptedValue = new String(decValue);
        return decryptedValue;
    }

0 个答案:

没有答案