首页 文章

java.security.InvalidKeyException:非法密钥大小 - Windows操作系统安全补丁后

提问于
浏览
0

我正在尝试使用以下方法加密字符串 . 这种方法之前成功运作 . 在Windows操作系统安全补丁之后我现在得到“java.security.InvalidKeyException:Illegal key size”

我还在C:\ Program Files(x86)\ Java \ jdk1.7.0_67 \ jre \ lib \ security下添加了local_policy和US_export_policy jar文件 . 我的操作系统是Windows7 64位 .

它在我运行Java main方法时工作,但是当我运行tomcat应用程序时,我得到以下异常 .

public static String encryptData() throws Exception{
        byte[] saltBytes = "myegaes256encryption".getBytes("UTF-8");
        SecretKeyFactory secretKeyFactory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA1");
        PBEKeySpec keySpec = new PBEKeySpec("myeg@1234".toCharArray(), saltBytes, 65536, 256);
        IvParameterSpec ivParameterSpec = new IvParameterSpec("myegaes256ivspec".getBytes("UTF-8"));
        SecretKey secretKey = secretKeyFactory.generateSecret(keySpec);
        SecretKeySpec secretKeySpec = new SecretKeySpec(secretKey.getEncoded(), "AES");

        //Encryption starts here
        Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
        cipher.init(Cipher.ENCRYPT_MODE, secretKeySpec, ivParameterSpec);

        byte[] encryptedBytes = cipher.doFinal("DD3E5B7BB7D3036FE7CB557B4FEEB05F".getBytes("UTF-8"));
        return new Base64().encodeAsString(encryptedBytes);
    }

Exception:

java.security.InvalidKeyException:javax.crypto.Cipher.a(未知来源)javax.crypto.Cipher.a(未知来源)javax.crypto.Cipher.a(未知来源)javax.crypto上的非法密钥大小.cipher.init(未知来源),位于javax.crypto.Cipher.init(未知来源)

1 回答

  • 2

    您需要将策略文件放到 jre/lib/security/ ,而不是 jdk/lib/

相关问题