如何使用带有32字节密钥的AES-256加密数据 - java.security.InvalidKeyException:非法的密钥大小异常

时间:2016-02-25 14:15:06

标签: java encryption

我需要使用带有32字节(256位)密钥和16字节(128位)初始化向量(IV)的AES-256对固定和变长文本字符串执行对称密钥加密。我能够使用16字节(128位)密钥和16字节IV成功加密,我可以找到许多支持这种方法的代码片段。文档建议支持32字节的密钥,但每次尝试都会遇到“java.security.InvalidKeyException:非法的密钥大小异常'”。我使用的是Java 1.8.0_74,并且我已经验证了“local_policy.jar”的存在。和' US_export_policy.jar'文件。具体来说,我尝试使用Java Cryptography Extension(JCE)中的最新版本更新它们但没有成功。这在Java中可行吗?我是否正确定义了转换算法?我缺少什么?

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;
class Encryptiontest {
    private static void log(String u) { System.out.println(u); }
    public static void main(String[] args) {
        final String IV =       "0123456789ABCDEF"; //16-byte
        final String KEY =      "AnEncryptionKey1"; //16-byte, works fine!

        //results in java.security.InvalidKeyException: Illegal key size exception
        //final String KEY =      "AnEncryptionKey1AnEncryptionKey1"; //32-byte

        //generate a bunch of random characters to encrypt
        StringBuilder sb = new StringBuilder();
        sb.append("THIS IS THE START OF THE STRING|");
        for (int i = 0; i < 263; i++)
            sb.append((char)((int)(Math.random() * (126 - 33 + 1)) + 33));
        sb.append("|THIS IS THE END OF THE STRING");
        final String PLAIN_TEXT = sb.toString();
        log("Plain Text To Encrypt:");
        log(PLAIN_TEXT);
        log("No. characters: " + PLAIN_TEXT.length());

        log("Encrypting data using AES symmetric block cipher . . . ");
        byte[] bResult = encrypt(KEY, IV, PLAIN_TEXT);

        log("Saving encrypted data to file . . .");
        File f = new File("test_Encrypted.TXT");

        FileOutputStream fos = null;
        try {
            fos = new FileOutputStream(f);
            if (f.exists())
                f.delete();
            else
                f.createNewFile();
            fos.write(bResult);
            fos.flush();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(Encryptiontest.class.getName()).log(Level.SEVERE, null, ex);
        } catch (IOException ex) {
            Logger.getLogger(Encryptiontest.class.getName()).log(Level.SEVERE, null, ex);
        } finally {
            try {
                if (fos != null)
                    fos.close();
            } catch (IOException ex) {
                Logger.getLogger(Encryptiontest.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        System.exit(1);
    }

    static byte[] encrypt (String key, String iv, String plainText) {
        byte[] encrypted = null;
        try {
            IvParameterSpec IV = new IvParameterSpec(iv.getBytes());
            SecretKeySpec skeyspec = new SecretKeySpec(key.getBytes(), "AES");
            Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING");
            cipher.init(Cipher.ENCRYPT_MODE, skeyspec, IV);
            encrypted = cipher.doFinal(plainText.getBytes());
        } catch (NoSuchAlgorithmException |
                NoSuchPaddingException |
                InvalidKeyException |
                InvalidAlgorithmParameterException |
                IllegalBlockSizeException |
                BadPaddingException ex) {
            Logger.getLogger(Encryptiontest.class.getName()).log(Level.SEVERE, null, ex);
        }
        return encrypted;
    }
}

0 个答案:

没有答案