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

时间:2018-04-23 07:07:09

标签: java encryption cryptography

我正在尝试使用以下方法加密字符串。这种方法之前成功运作。在Windows操作系统安全补丁之后我现在得到" java.security.InvalidKeyException:非法密钥大小"

我还添加了local_policy& C:\ Program Files(x86)\ Java \ jdk1.7.0_67 \ jre \ lib \ security下的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);
    }

例外:

  

java.security.InvalidKeyException:非法密钥大小   javax.crypto.Cipher.a(未知来源)at   javax.crypto.Cipher.a(未知来源)at   javax.crypto.Cipher.a(未知来源)at   javax.crypto.Cipher.init(未知来源)at   javax.crypto.Cipher.init(未知来源)

1 个答案:

答案 0 :(得分:2)

您需要将政策文件设为Sub Copy_Sheets_To_consolidated() Application.ScreenUpdating = False Dim i As Long Dim Sh1 As String Sh1 = "consolidated" Dim ans As String Dim Lastrow As Long Dim Lastrowa As Long Dim Lastrowd As Long Sheets(Sh1).Activate Lastrow = 6 Lastrowd = 6 For i = 1 To Sheets.Count If Sheets(i).Name <> Sh1 Then ans = Sheets(i).Name Lastrowa = Sheets(i).Cells(Rows.Count, "A").End(xlUp).Row Sheets(i).Range("A6:N" & Lastrowa).Copy Sheets(Sh1).Range("A" & Lastrow) Lastrowd = Sheets(Sh1).Cells(Rows.Count, "A").End(xlUp).Row Sheets(Sh1).Range("D" & Lastrow & ":D" & Lastrowd).Value = ans Lastrow = Sheets(Sh1).Cells(Rows.Count, "A").End(xlUp).Row + 1 End If Next Application.ScreenUpdating = True End Sub ,而不是jre/lib/security/