AES加密Java密钥长度无效 [英] AES Encryption Java Invalid Key length

查看:142
本文介绍了AES加密Java密钥长度无效的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试创建一个AES加密方法,但由于某种原因我不断获得

I am trying to create an AES encryption method, but for some reason I keep getting


java。 security.InvalidKeyException:密钥长度不是128/192/256位

这是代码: p>

Here is the code:

public static SecretKey getSecretKey(char[] password, byte[] salt) throws NoSuchAlgorithmException, InvalidKeySpecException{
    SecretKeyFactory factory = SecretKeyFactory.getInstance("PBEWithMD5AndDES");
    // NOTE: last argument is the key length, and it is 256
    KeySpec spec = new PBEKeySpec(password, salt, 1024, 256);
    SecretKey tmp = factory.generateSecret(spec);
    SecretKey secret = new SecretKeySpec(tmp.getEncoded(), "AES");
    return(secret);
}


public static byte[] encrypt(char[] password, byte[] salt, String text) throws NoSuchAlgorithmException, InvalidKeySpecException, NoSuchPaddingException, InvalidKeyException, InvalidParameterSpecException, IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException{
    SecretKey secret = getSecretKey(password, salt);

    Cipher cipher = Cipher.getInstance("AES");

    // NOTE: This is where the Exception is being thrown
    cipher.init(Cipher.ENCRYPT_MODE, secret);
    byte[] ciphertext = cipher.doFinal(text.getBytes("UTF-8"));
    return(ciphertext);
}

任何人都可以看到我做错了什么?我认为它可能与SecretKeyFactory算法有关,但这是唯一可以找到的,在我正在开发的最终系统上得到支持。任何帮助将不胜感激。谢谢。

Can anyone see what I am doing wrong? I am thinking it may have something to do with the SecretKeyFactory algorithm, but that is the only one I can find that is supported on the end system I am developing against. Any help would be appreciated. Thanks.

推荐答案

为了更强大的密钥强度加密,您需要下载Java加密扩展(JCE)无限强度管理策略文件。

For a stronger key strength encryption you would need to download Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files.

http:// java。 sun.com/javase/downloads/index.jsp (检查其他下载)。

http://java.sun.com/javase/downloads/index.jsp (Check Other Downloads).

这篇关于AES加密Java密钥长度无效的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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