AES加密抛出"非法密钥大小错误"

时间:2014-06-10 10:15:59

标签: java encryption aes java-7

我正在尝试使用AES进行加密。以下是我的代码段:

private final static String ENCRYPTION_ALGORITHM = "AES";
private final static String CIPHER_TRANSFORMATION = "AES/CBC/PKCS5Padding";
private final static String ENCODING = "UTF-8";
private final static String KEY_STRING = "C0BAE23DF8B51807B3E17D21925FADF273A70181E1D81B8EDE6C76A5C1F1716E";

public static String encryptData(String plainText) {

    String keyString = KEY_STRING;
    String encryptedValue = null;

    byte[] keyValue = DatatypeConverter.parseHexBinary(keyString);
    Key key = new SecretKeySpec(keyValue, ENCRYPTION_ALGORITHM);
    Cipher cipher;
    byte[] encVal;

    try {
        cipher = Cipher.getInstance(CIPHER_TRANSFORMATION);
        cipher.init(Cipher.ENCRYPT_MODE, key, new IvParameterSpec(new byte[64]));
        encVal = cipher.doFinal(plainText.getBytes());
        encryptedValue = (new BASE64Encoder()).encode(encVal);
    } catch (NoSuchAlgorithmException | NoSuchPaddingException | InvalidKeyException | IllegalBlockSizeException | BadPaddingException | InvalidAlgorithmParameterException e) {
        System.out.println("Error in EncryptData.encryptData(): " + e.getMessage());
        encryptedValue = null;
    }


    return encryptedValue;
}

以下是我在eclipse中运行时遇到的错误:

Error in EncryptData.encryptData(): Illegal key size

任何建议或我做错了什么?

1 个答案:

答案 0 :(得分:2)

您是否更新了允许256位加密的策略? 检查Java密码学扩展(JCE)无限强度管辖权政策文件6'来自http://www.oracle.com/technetwork/java/javase/downloads/index.html