垫块损坏?

时间:2018-07-24 14:22:10

标签: android cryptography

我一直在使用以下两种方法来加密和解密敏感信息。

public static String encryptSensitiveInfo(String strToEncrypt,
                                          String saltToEncrypt) throws Exception {

    String encryptedString = "";
    byte[] encryptedValue;

    Key key = new SecretKeySpec(saltToEncrypt.getBytes(), "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.ENCRYPT_MODE, key);

    encryptedValue = cipher.doFinal(strToEncrypt.getBytes());
    encryptedString = new String(Base64.encodeBase64(encryptedValue));

    encryptedValue = null;
    return encryptedString;
}



public static String decryptSensitiveInfo(String strToDecrypt,
                                          String saltToDecrypt) throws Exception {

    String decryptedString = "";
    byte[] decryptedValue;

    Key key = new SecretKeySpec(saltToDecrypt.getBytes(), "AES");
    Cipher cipher = Cipher.getInstance("AES");
    cipher.init(Cipher.DECRYPT_MODE, key);

    decryptedValue = cipher.doFinal(Base64.decodeBase64(strToDecrypt
            .getBytes()));
    decryptedString = new String(decryptedValue);

    decryptedValue = null;
    return decryptedString;
}

在解密时,我得到“填充块损坏”的执行。任何解决该问题的帮助将不胜感激。预先感谢。

1 个答案:

答案 0 :(得分:0)

您正在正确地基于密文执行64,因为密文包含随机的字节。但是,您忘记对密钥执行相同的操作(在代码中,密钥被莫名其妙地称为saltToDecrypt)。如果密钥不匹配或密文已损坏,则几乎可以肯定会遇到BadPaddingException

如果密文数量已更改,IllegalBlockSizeException的可能性就更大,并且如果密钥的大小不适合AES,则InvalidKeyException

相关问题