Java:密码包(加密和解密)。无效键错误 [英] Java: Cipher package (encrypt and decrypt). invalid key error

查看:238
本文介绍了Java:密码包(加密和解密)。无效键错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用静态方法来使用javax.crypto加密和解密消息。我有两个使用ecipher和dcipher的静态方法,以便做它们被忽略的事情,我需要初始化一些变量(也是静态的)。但是当我尝试使用它,我得到InvalidKeyException与我给ecipher.init(...)的参数。我找不到为什么这里是代码:

i am doing a class with static methods to encrypt and decrypt a message using javax.crypto. I have 2 static methods that use ecipher and dcipher in order to do what they are supossed to do i need to initialize some variables (which are static also). But when i try to use it i get InvalidKeyException with the parameters i give to ecipher.init(...). I can't find why. Here is the code:

    private static byte[] raw = {-31,   17,   7,  -34,  59, -61, -60,  -16, 
                              26,   87, -35,  114,   0, -53,  99, -116, 
                             -82, -122,  68,   47,  -3, -17, -21,  -82, 
                             -50,  126, 119, -106, -119, -5, 109,   98};
    private static SecretKeySpec skeySpec;
    private static Cipher ecipher;
    private static Cipher dcipher;

    static {
        try {
            skeySpec = new SecretKeySpec(raw, "AES");
            // Instantiate the cipher
            ecipher = Cipher.getInstance("AES");
            dcipher = Cipher.getInstance("AES");
            ecipher.init(Cipher.ENCRYPT_MODE, skeySpec);
            dcipher.init(Cipher.DECRYPT_MODE, skeySpec);
        } catch (NoSuchAlgorithmException e) {
            throw new UnhandledException("No existe el algoritmo deseado", e);
        } catch (NoSuchPaddingException e) {
            throw new UnhandledException("No existe el padding deseado", e);
        } catch (InvalidKeyException e) {
            throw new UnhandledException("Clave invalida", e);
        }
    }


推荐答案

-256(和AES-192)需要无限力量管辖政策文件(最后一次下载 http://java.sun.com/javase/downloads/index.jsp )为JRE安装。没有这种支持将导致InvalidKeyException当尝试使用192或256位密钥,如您的类。

AES-256 (and AES-192) requires the Unlimited Strength Jurisdiction Policy Files (one fo the last downloads at http://java.sun.com/javase/downloads/index.jsp) to be installed for the JRE. Not having this support will result in the InvalidKeyException when attempting to use 192 or 256 bit keys, as in your class.

AES无限制强度的最大允许密钥大小是记录在 JCA Java指南参考指南6 ,而这恰好是128位。

The maximum allowed key size for AES without unlimited strength is documented in the JCA Reference Guide for Java 6, and by this happens to be 128-bits.

这篇关于Java:密码包(加密和解密)。无效键错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆