javax.crypto.badpaddingexception:给定的最后一个块没有正确填充-解密文本时

twh00eeo  于 2021-07-09  发布在  Java
关注(0)|答案(0)|浏览(202)

当dycrypt纯文本时,抛出badpaddingexception javax.crypto.badpaddingexception:given final block not properly padded com.sun.crypto.provider.ciphercore.dofinal(ciphercore)。java:811)

private static final String ENCRYPTION_KEY = "harshitorld!@#$%"; 
private static byte[] randomBytesToDecrypt;
private static final String ALGORITHM = "Rijndael";
private static final String UNICODE_FORMAT = "UTF8";
private static String encrypt(String value) {
    try {
        Key key = generateKey();
        Cipher c = Cipher.getInstance(ALGORITHM);
        c.init(Cipher.ENCRYPT_MODE, key);
        byte[] encValue = c.doFinal(value.getBytes(UNICODE_FORMAT));
        String encryptedValue = new BASE64Encoder().encode(encValue);
        return encryptedValue;
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}

public static String decrypt(String encryptedValue) {
    try {
        Key key = generateKey();
        Cipher c = Cipher.getInstance(ALGORITHM);
        c.init(Cipher.DECRYPT_MODE, key);
        byte[] decordedValue = new BASE64Decoder().decodeBuffer(encryptedValue);
        byte[] decValue = c.doFinal(decordedValue);//////////LINE 50
        String decryptedValue = new String(decValue,UNICODE_FORMAT);
        return decryptedValue;
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return null;
}

private static Key generateKey() throws Exception {
    byte[] keyAsBytes;
    keyAsBytes = ENCRYPTION_KEY.getBytes(UNICODE_FORMAT);
    Key key = new SecretKeySpec(keyAsBytes, ALGORITHM);
    return key;
}

public static String encryptText(String normalText) {
    return encrypt(normalText);
}

public static String decryptText(String encryptedText) {
    return decrypt(encryptedText);
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题