【问题标题】:InvalidKeyException : Illegal Key Size - Java code throwing exception for encryption class - how to fix?InvalidKeyException : Illegal Key Size - Java 代码抛出加密类异常 - 如何修复?
【发布时间】:2011-09-15 20:49:53
【问题描述】:

我一直在尝试获取一些用于加密 Paypal 按钮的有效 Java 代码。这不是一件容易的事!即使我从 Paypal 获得一些代码,我也会遇到错误..ugh..

这就是我目前所拥有的,我认为最终会奏效的。

我从 Paypal 的网站下载了 Java.zip 文件。其中有两个类 - ClientSide.java 和 ButtonEncryption.java

问题 - 我收到了 InvalidKeyException : Illegal key size 错误。

问题
1)我该如何解决这个问题? 2) 哪行代码抛出错误?

C:\jakarta-tomcat\webapps\PlanB\WEB-INF\classes>java palmb.servlets.paypal.ButtonEncryption
java.io.IOException: exception decrypting data - java.security.InvalidKeyException: Illegal key size
        at org.bouncycastle.jce.provider.JDKPKCS12KeyStore.cryptData(Unknown Source)
        at org.bouncycastle.jce.provider.JDKPKCS12KeyStore.engineLoad(Unknown Source)
        at java.security.KeyStore.load(Unknown Source)
        at palmb.servlets.paypal.ClientSide.getButtonEncryptionValue(ClientSide.java:63)
        at palmb.servlets.paypal.ButtonEncryption.main(ButtonEncryption.java:81)


客户端类

package palmb.servlets.paypal;

import java.io.ByteArrayOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.security.InvalidAlgorithmParameterException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.PrivateKey;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertStore;
import java.security.cert.CertStoreException;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.CollectionCertStoreParameters;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.Enumeration;

import org.bouncycastle.cms.CMSEnvelopedData;
import org.bouncycastle.cms.CMSEnvelopedDataGenerator;
import org.bouncycastle.cms.CMSException;
import org.bouncycastle.cms.CMSProcessableByteArray;
import org.bouncycastle.cms.CMSSignedData;
import org.bouncycastle.cms.CMSSignedDataGenerator;
import org.bouncycastle.openssl.PEMReader;
import org.bouncycastle.util.encoders.Base64;

/**
 */
public class ClientSide 
{
    private String  keyPath;
    private String  certPath;
    private String  paypalCertPath;
    private String  keyPass;

    public ClientSide( String keyPath, String certPath, String paypalCertPath, String keyPass )
    {
        this.keyPath = keyPath;
        this.certPath = certPath;
        this.paypalCertPath = paypalCertPath;
        this.keyPass = keyPass;
    }   

    public String getButtonEncryptionValue(String _data, String _privateKeyPath, String _certPath, String _payPalCertPath,
                                            String _keyPass) throws IOException,CertificateException,KeyStoreException,
                                            UnrecoverableKeyException,InvalidAlgorithmParameterException,NoSuchAlgorithmException,
                                            NoSuchProviderException,CertStoreException,CMSException {
        _data = _data.replace(',', '\n');
        CertificateFactory cf = CertificateFactory.getInstance("X509", "BC");

        // Read the Private Key
        KeyStore ks = KeyStore.getInstance("PKCS12", "BC");
        ks.load( new FileInputStream(_privateKeyPath), _keyPass.toCharArray() );

        String keyAlias = null;
        Enumeration aliases = ks.aliases();
        while (aliases.hasMoreElements()) {
            keyAlias = (String) aliases.nextElement();
        }

        PrivateKey privateKey = (PrivateKey) ks.getKey( keyAlias, _keyPass.toCharArray() );

        // Read the Certificate
        X509Certificate certificate = (X509Certificate) cf.generateCertificate( new FileInputStream(_certPath) );

        // Read the PayPal Cert
        X509Certificate payPalCert = (X509Certificate) cf.generateCertificate( new FileInputStream(_payPalCertPath) );

        // Create the Data
        byte[] data = _data.getBytes();

        // Sign the Data with my signing only key pair
        CMSSignedDataGenerator signedGenerator = new CMSSignedDataGenerator();

        signedGenerator.addSigner( privateKey, certificate, CMSSignedDataGenerator.DIGEST_SHA1 );

        ArrayList certList = new ArrayList();
        certList.add(certificate);
        CertStore certStore = CertStore.getInstance( "Collection", new CollectionCertStoreParameters(certList) );
        signedGenerator.addCertificatesAndCRLs(certStore);

        CMSProcessableByteArray cmsByteArray = new CMSProcessableByteArray(data);
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        cmsByteArray.write(baos);
        System.out.println( "CMSProcessableByteArray contains [" + baos.toString() + "]" );

        CMSSignedData signedData = signedGenerator.generate(cmsByteArray, true, "BC");

        byte[] signed = signedData.getEncoded();

        CMSEnvelopedDataGenerator envGenerator = new CMSEnvelopedDataGenerator();
        envGenerator.addKeyTransRecipient(payPalCert);
        CMSEnvelopedData envData = envGenerator.generate( new CMSProcessableByteArray(signed),
                CMSEnvelopedDataGenerator.DES_EDE3_CBC, "BC" );

        byte[] pkcs7Bytes = envData.getEncoded();


        return new String( DERtoPEM(pkcs7Bytes, "PKCS7") );

    }

    public static byte[] DERtoPEM(byte[] bytes, String headfoot) 
    {
        ByteArrayOutputStream pemStream = new ByteArrayOutputStream();
        PrintWriter writer = new PrintWriter(pemStream);

        byte[] stringBytes = Base64.encode(bytes);

        System.out.println("Converting " + stringBytes.length + " bytes");

        String encoded = new String(stringBytes);

        if (headfoot != null) {
            writer.print("-----BEGIN " + headfoot + "-----\n");
        }

        // write 64 chars per line till done
        int i = 0;
        while ((i + 1) * 64 < encoded.length()) {
            writer.print(encoded.substring(i * 64, (i + 1) * 64));
            writer.print("\n");
            i++;
        }
        if (encoded.length() % 64 != 0) {
            writer.print(encoded.substring(i * 64)); // write remainder
            writer.print("\n");
        }
        if (headfoot != null) {
            writer.print("-----END " + headfoot + "-----\n");
        }
        writer.flush();
        return pemStream.toByteArray();
    }

}


ButtonEncryption 类

package palmb.servlets.paypal;

//import com.paypal.crypto.sample.*;

import palmb.servlets.paypal.ClientSide;

import java.io.*;
import java.security.InvalidAlgorithmParameterException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.Security;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertStoreException;
import java.security.cert.CertificateException;
import org.bouncycastle.cms.CMSException;

/**
 */
public class ButtonEncryption {


    //path to public cert
    private static String certPath = "C:/jakarta-tomcat/webapps/PlanB/Certs/public-cert.pem";

    //path to private key in PKCS12 format
    private static String keyPath = "C:/jakarta-tomcat/webapps/PlanB/Certs/my_pkcs12.p12";

    //path to Paypal's public cert
    private static String paypalCertPath = "C:/jakarta-tomcat/webapps/PlanB/Certs/paypal_cert_pem.txt";

    //private key password
    private static String keyPass = "password"; //will be replaced with actual password when compiled and executed

    //the button command, properties/parameters
    private static String cmdText = "cmd=_xclick\nbusiness=buyer@hotmail.com\nitem_name=vase\nitemprice=25.00";  //cmd=_xclick,business=sample@paypal.com,amount=1.00,currency_code=USD

    //output file for form code
    private static String output = "test.html";


    public static void main(String[] args) 
    {
        Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider()); 


        String stage = "sandbox";

        try 
        {
            ClientSide client_side = new ClientSide( keyPath, certPath, paypalCertPath, keyPass );

            String result = client_side.getButtonEncryptionValue( cmdText, keyPath, certPath, paypalCertPath, keyPass );

            File outputFile = new File( output );
            if ( outputFile.exists() )
                outputFile.delete();

            if ( result != null && result != "")
            {
                try {        
                    OutputStream fout= new FileOutputStream( output );
                    OutputStream bout= new BufferedOutputStream(fout);
                    OutputStreamWriter out = new OutputStreamWriter(bout, "US-ASCII");

                    out.write( "<form action=\"https://www." );
                    out.write( stage );
                    out.write( "paypal.com/cgi-bin/webscr\" method=\"post\">" );  
                    out.write( "<input type=\"hidden\" name=\"cmd\" value=\"_s-xclick\">" );  ;
                    out.write( "<input type=\"image\" src=\"https://www." );
                    out.write( stage );
                    out.write( "paypal.com/en_US/i/btn/x-click-but23.gif\" border=\"0\" name=\"submit\" " );
                    out.write( "alt=\"Make payments with PayPal - it's fast, free and secure!\">" );
                    out.write( "<input type=\"hidden\" name=\"encrypted\" value=\"" );
                    out.write( result );
                    out.write( "\">" );
                    out.write( "</form>");

                    out.flush();  // Don't forget to flush!
                    out.close();
                  }
                  catch (UnsupportedEncodingException e) {
                    System.out.println(
                     "This VM does not support the ASCII character set."
                    );
                  }
                  catch (IOException e) {
                    System.out.println(e.getMessage());        
                  }
            }
        } 
        catch (NoSuchAlgorithmException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        catch (NoSuchProviderException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        catch (IOException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        catch (CMSException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        catch (CertificateException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        catch (KeyStoreException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        catch (UnrecoverableKeyException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        catch (InvalidAlgorithmParameterException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } 
        catch (CertStoreException e) 
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}


已编辑:关于密钥/证书的信息

我通过以下命令使用 OpenSSL 生成了私钥和公共证书。
私钥
openssl genrsa -out private-key.pem 1024
公共证书
openssl req -new -key private-key.pem -x509 -days 1095 -out public-cert.pem
创建的 PKCS12 文件
openssl pkcs12 -export -in public-cert.pem -inkey private-key.pem -out my_pkcs12.p12


此外,我必须从 Paypal 网站下载 Paypal 公共证书。


已编辑 - 添加编译警告 - BouncyCastle

C:\jakarta-tomcat\webapps\PlanB\WEB-INF\classes>javac .\palmb\servlets\paypal\ClientSide.java -Xlint
.\palmb\servlets\paypal\ClientSide.java:85: warning: [deprecation] addSigner(java.security.PrivateKey,java.security.cert.X509Certificate,java.lang.String) in org.bouncycastle.cms.CMSSignedDataGenerator has been deprecated
                signedGenerator.addSigner( privateKey, certificate, CMSSignedDat
aGenerator.DIGEST_SHA1 );
                               ^
.\palmb\servlets\paypal\ClientSide.java:88: warning: [unchecked] unchecked call
to add(E) as a member of the raw type java.util.ArrayList
                certList.add(certificate);
                            ^
.\palmb\servlets\paypal\ClientSide.java:90: warning: [deprecation] addCertificatesAndCRLs(java.security.cert.CertStore) in org.bouncycastle.cms.CMSSignedGenerat
or has been deprecated
                signedGenerator.addCertificatesAndCRLs(certStore);
                               ^
.\palmb\servlets\paypal\ClientSide.java:97: warning: [deprecation] generate(org.
bouncycastle.cms.CMSProcessable,boolean,java.lang.String) in org.bouncycastle.cm
s.CMSSignedDataGenerator has been deprecated
                CMSSignedData signedData = signedGenerator.generate(cmsByteArray, true, "BC");
                                                          ^
.\palmb\servlets\paypal\ClientSide.java:102: warning: [deprecation] addKeyTransR
ecipient(java.security.cert.X509Certificate) in org.bouncycastle.cms.CMSEnvelope
dGenerator has been deprecated
                envGenerator.addKeyTransRecipient(payPalCert);
                            ^
.\palmb\servlets\paypal\ClientSide.java:103: warning: [deprecation] generate(org.bouncycastle.cms.CMSProcessable,java.lang.String,java.lang.String) in org.bouncycastle.cms.CMSEnvelopedDataGenerator has been deprecated
                CMSEnvelopedData envData = envGenerator.generate( new CMSProcess
ableByteArray(signed),
                                                       ^
6 warnings


JCE 策略文件安装步骤

这些是我安装 JCE 无限强度策略文件所采取的步骤:
1) 前往 Oracle 上的 Java JCE Download 页面。
2) 从 zip 中提取文件。
3) 将 local_policy.jar 和 US_export_policy.jar 文件放在 C:\Java\jdk1.6.0_22\jre\lib\security 文件夹中。
注意:C:\Java\jdk1.6.0_22 设置为 %JAVA_HOME%
4) 更新了系统类路径以包含 jar 的位置。
注意: JDK 1.6 附带的安全文件夹中还有其他文件,包括:java.policy、java.security、javaws.policy、trusted.libraries - 但这些可能与 JCE 文件无关,对吧?


2011 年 6 月 23 日编辑 - 进一步配置后的结果

我在http://www.bouncycastle.org/specifications.html#install去了Bouncy Castle页面
向下滚动到5.0 Bouncy Castle Provider,然后阅读5.1 Example下的信息。它提到将 Bouncy Castle Provider 的参数添加到 java.security 文件中。我的文件位于 C:\Java\jdk1.6.0_22\jre\lib\security 下。

我在我的文件中添加了以下行 - security.provider.10=org.bouncycastle.jce.provider.BouncyCastleProvider

此外,我发现我没有将 Bouncy Castle jar 添加到类路径中,所以我继续这样做了。

现在在进行这些更改、重新编译并尝试执行 ClientSide.java 之后,我遇到了同样的异常:但也许重点应该放在异常的部分,它说明了有关 bouncycastle 提供程序 -

at org.bouncycastle.jce.provider.JDKPKCS12KeyStore.cryptData(Unknown Source)
at org.bouncycastle.jce.provider.JDKPKCS12KeyStore.engineLoad(Unknown Source)

@PeteyB - 我确定我正确安装了策略文件。根据我在这里所说的,您还有什么可以建议我尝试的吗?你能看看 Bouncy Castle 网站@http://www.bouncycastle.org/specifications.html#install 看看我是否遗漏了什么吗?

【问题讨论】:

  • 此异常是否与 Java 加密扩展 (JCE) 无限强度管辖策略文件有关?我下载了 Java 版本 1.6 的那些 - 并将 jar 放入我电脑上的 \jre\lib\security 文件夹中。
  • 你的公司叫“Plan B”吗?我也想选择那个名字,直到我看到它已经被使用了。
  • @katura - “此异常是否与 Java 加密扩展 (JCE) 无限强度管辖策略文件有关?”是的,这与它有很大关系。它解决了你的问题吗?
  • @Vineet Reynolds - 当我编译并收到 InvalidKeyException : Illegal key size error 时,我已经将策略文件/jars 放入了“security”文件夹中。我现在能做什么?我的类路径是否必须使用安全文件夹的路径进行更新?
  • @katura,不,不需要这样做;它已经在类路径中。我们需要更多信息 - 密钥库中的条目类型,以及证书中使用的密钥的类型和大小。

标签: java security exception encryption paypal


【解决方案1】:

当您尝试从此处的“C:/jakarta-tomcat/webapps/PlanB/Certs/my_pkcs12.p12”加载密钥库时,似乎会引发错误:

ks.load( new FileInputStream(_privateKeyPath), _keyPass.toCharArray() ); 

您是否尝试在文件路径中将“/”替换为“\\”?如果这没有帮助,它可能与 Java 的 Unlimited Strength Jurisdiction Policy Files 有关。您可以通过编写一个执行 AES 加密的小程序来检查这一点。尝试使用 128 位密钥进行加密,如果可行,请尝试使用 256 位密钥,看看是否失败。

执行 AES 加密的代码:

import java.io.UnsupportedEncodingException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;

import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.KeyGenerator;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.SecretKey;
import javax.crypto.spec.IvParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class Test 
{
    final String ALGORITHM = "AES";                       //symmetric algorithm for data encryption
    final String PADDING_MODE = "/CBC/PKCS5Padding";      //Padding for symmetric algorithm
    final String CHAR_ENCODING = "UTF-8";                 //character encoding
    //final String CRYPTO_PROVIDER = "SunMSCAPI";             //provider for the crypto

    int AES_KEY_SIZE = 256;  //symmetric key size (128, 192, 256) if using 256 you must have the Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files  installed

    private String doCrypto(String plainText) throws NoSuchAlgorithmException, NoSuchProviderException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, InvalidAlgorithmParameterException, UnsupportedEncodingException
    {
        byte[] dataToEncrypt = plainText.getBytes(CHAR_ENCODING);

        //get the symmetric key generator
        KeyGenerator keyGen = KeyGenerator.getInstance(ALGORITHM);
        keyGen.init(AES_KEY_SIZE); //set the key size

        //generate the key
        SecretKey skey = keyGen.generateKey();

        //convert to binary
        byte[] rawAesKey = skey.getEncoded();

        //initialize the secret key with the appropriate algorithm
        SecretKeySpec skeySpec = new SecretKeySpec(rawAesKey, ALGORITHM);

        //get an instance of the symmetric cipher
        Cipher aesCipher = Cipher.getInstance(ALGORITHM + PADDING_MODE);

        //set it to encrypt mode, with the generated key
        aesCipher.init(Cipher.ENCRYPT_MODE, skeySpec);

        //get the initialization vector being used (to be returned)
        byte[] aesIV = aesCipher.getIV();

        //encrypt the data
        byte[] encryptedData = aesCipher.doFinal(dataToEncrypt);    

        //initialize the secret key with the appropriate algorithm
        SecretKeySpec skeySpecDec = new SecretKeySpec(rawAesKey, ALGORITHM);

        //get an instance of the symmetric cipher
        Cipher aesCipherDec = Cipher.getInstance(ALGORITHM +PADDING_MODE);

        //set it to decrypt mode with the AES key, and IV
        aesCipherDec.init(Cipher.DECRYPT_MODE, skeySpecDec, new IvParameterSpec(aesIV));

        //decrypt and return the data
        byte[] decryptedData = aesCipherDec.doFinal(encryptedData);

        return new String(decryptedData, CHAR_ENCODING);
    }

    public static void main(String[] args)
    {
        String text = "Lets encrypt me";

        Test test = new Test();

        try {
            System.out.println(test.doCrypto(text));
        } catch (InvalidKeyException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchProviderException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (NoSuchPaddingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IllegalBlockSizeException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (BadPaddingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (InvalidAlgorithmParameterException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (UnsupportedEncodingException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

这段代码对你有用吗?

您可能还想尝试在此行中指定您的充气城堡供应商:

Cipher.getInstance(ALGORITHM +PADDING_MODE, "YOUR PROVIDER");

看看这是否可能是与充气城堡相关的错误。

【讨论】:

  • 我用路径尝试了你的建议,使用“\\”而不是“/”。没用:(然后我尝试使用 128 和 256 加密创建 RSA 密钥。成功生成文件后,我无法使用 private-key.pem 创建 public-cert.pem,因为出现错误: 3576:error:04075070:rsa routines: RSA_sign:digest too big for rsa key:.\crypto\rsa\rsa_sign.c:119: 3576:error:0D0C3006:asn1 encoding routines: ASN1_item_sign:EVP lib:.\crypto\ asn1\a_sign.c:279: 请求错误。不过 1024 位似乎没问题...
  • @Petey B - 我真的对证书和密钥一无所知 - 所以我很难理解 AES 加密和代码 sn-p 的使用。对不起。
  • @katura,我添加了使用 256 位密钥的 AES 加密的完整代码。看看它是否对你有用。
  • @Petey - 谢谢。我运行了代码,这是我得到的异常: java.security.InvalidKeyException: Illegal key size or default parameters at javax.crypto.Cipher.a(DashoA13*..) at javax.crypto.Cipher.a(DashoA13*. .) 在 javax.crypto.Cipher.a(DashoA13*..) 在 javax.crypto.Cipher.init(DashoA13*..) 在 javax.crypto.Cipher.init(DashoA13*..) 在 palmb.servlets.paypal .Test.doCrypto(Test.java:48) at palmb.servlets.paypal.Test.main(Test.java:78)
  • @katura,将“AES_KEY_SIZE = 256”更改为“AES_KEY_SIZE = 128”,看看它是否有效。如果是这样,那么您没有正确安装无限强度策略,这就是导致您的问题出现错误的原因。
【解决方案2】:

所以问题一定出在您的 JCE Unlimited Strength 安装上。

确保覆盖 JDK 的 jdk1.6.0_25\jre\lib\security\ 和 JRE 的 lib\security\ 文件夹中的 local_policy.jarUS_export_policy.jar

在我的情况下,我会将新的 .jars 放入:

C:\Program Files\Java\jdk1.6.0_25\jre\lib\security

C:\Program Files\Java\jre6\lib\security


如果您正在运行 Java 8 并遇到此问题。以下步骤应该会有所帮助!

转到您的 JRE 安装(例如 - jre1.8.0_181\lib\security\policy\unlimited)复制 local_policy.jar 并将其替换为 JDK 安装目录中的“local_policy.jar”(例如 - jdk1.8.0_141\jre\lib\security)。

【讨论】:

  • 我删除了 C:\Java\jdk1.6.0_22\jre\lib\security 文件夹中的策略文件,将新文件放入并将两个文件都放在 C:\Java\jre6 \lib\security 文件夹按照您的指示进行 - 不幸的是,异常仍在出现:( 这可能与 Bouncy Castle jars 有什么关系吗?
  • @Petey - 我还能做些什么吗?
  • @Petey - 我用 128 运行了 AES 代码,它有效。但是 192 和 256 抛出了异常:InvalidKeyException - Illegal key size or default parameters
  • 这绝对是因为政策文件,毫无疑问。我在这个确切的问题上花了至少 5 个小时。
  • 非常重要的部分“在您的 JRE 的 lib\security\ 文件夹中”。这对我有帮助。干杯。
【解决方案3】:

如果您在使用 256 位密钥而不是 128 位密钥运行我的 AES 加密程序时仍然收到 InvalidKeyException,这是因为您没有正确安装新策略 JAR 文件,与 BouncyCastle 无关(这也受到这些政策文件的限制)。尝试卸载,然后重新安装 java,然后用新的无限强度的 jar 替换旧的 jar。除此之外,我没有想法,祝你好运。

如果您在 winzip 中打开 lib/security/local_policy.jar 和 US_export_policy.jar 文件并在记事本中查看包含的 *.policy 文件并确保它们看起来像这样,您可以看到策略文件本身:

default_local.policy:

    // Country-specific policy file for countries with no limits on crypto strength.
grant {
    // There is no restriction to any algorithms.
    permission javax.crypto.CryptoAllPermission; 
};

default_US_export.policy:

// Manufacturing policy file.
grant {
    // There is no restriction to any algorithms.
    permission javax.crypto.CryptoAllPermission; 
};

【讨论】:

  • 从 2 个 jar 文件中提取文件并将提取的文件保留在安全文件夹中对我有什么好处吗?
  • @katura,不,把它们放在包装好的罐子里,不过我认为如果你想尝试它不会有什么坏处。
【解决方案4】:

在您的客户端代码中添加以下代码:

static {
    Security.insertProviderAt(new BouncyCastleProvider(),1);
 }

这样就不需要在 java.security 文件中添加任何条目了。

【讨论】:

【解决方案5】:

我遇到了同样的问题。尝试先在 java 安全文件夹中添加US_export_policy.jarlocal_policy.jar,但问题仍然存在。然后在tomcat setenv.shfile 中的java_opts 中添加以下内容,它就可以工作了。

-Djdk.tls.ephemeralDHKeySize=2048

请查看此link 了解更多信息

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-01-28
    • 1970-01-01
    • 1970-01-01
    • 2017-04-26
    • 1970-01-01
    • 1970-01-01
    • 2012-11-28
    • 2011-02-07
    相关资源
    最近更新 更多