0

ドキュメントを復号化しようとすると問題が発生します。これを行うために公開/秘密ペア キーを使用しています。これを行うためにトークンを使用しています。

これは私が得ているエラーです:

java.security.ProviderException: java.security.KeyException: An internal error occurred.

at sun.security.mscapi.RSACipher.doFinal(RSACipher.java:297)
at sun.security.mscapi.RSACipher.engineDoFinal(RSACipher.java:321)
at javax.crypto.Cipher.doFinal(Cipher.java:2087)
at org.bouncycastle.operator.jcajce.JceAsymmetricKeyUnwrapper.generateUnwrappedKey(Unknown Source)
at org.bouncycastle.cms.jcajce.JceKeyTransRecipient.extractSecretKey(Unknown Source)
at org.bouncycastle.cms.jcajce.JceKeyTransEnvelopedRecipient.getRecipientOperator(Unknown Source)
at org.bouncycastle.cms.KeyTransRecipientInformation.getRecipientOperator(Unknown Source)
at org.bouncycastle.cms.RecipientInformation.getContentStream(Unknown Source)
at org.bouncycastle.cms.RecipientInformation.getContent(Unknown Source)
at ec.gov.informatica.firmadigital.cms.CMSEncryption.decrypt(CMSEncryption.java:198)
at ec.mil.gestordocumental.security.test.encryption.DecryptFileWithPublicCertificateToken.mainTest(DecryptFileWithPublicCertificateToken.java:110)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:44)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:41)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:76)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:193)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:52)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:191)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:42)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:184)
at org.junit.runners.ParentRunner.run(ParentRunner.java:236)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)
Caused by: java.security.KeyException: An internal error occurred.

at sun.security.mscapi.RSACipher.encryptDecrypt(Native Method)
at sun.security.mscapi.RSACipher.doFinal(RSACipher.java:289)
... 32

そして、これは私が解読するために使用しているコードです:

public static byte[] decrypt(byte[] encrypted, X509Certificate cert, PrivateKey privateKey, Provider provider) {
    try {
        CMSEnvelopedData enveloped = new CMSEnvelopedData(encrypted);

        RecipientInformationStore recipients = enveloped.getRecipientInfos();
        X509CollectionStoreParameters s = new X509CollectionStoreParameters(Collections.singleton(new JcaX509CertificateHolder(cert)));

        X509StoreCertCollection s1 = new X509StoreCertCollection();
        s1.engineInit(s);

        Iterator it = recipients.getRecipients().iterator();

        RecipientInformation recipient = null;

        while (it.hasNext()) {
            recipient = (RecipientInformation) it.next();

            if (recipient instanceof KeyTransRecipientInformation) {
                Collection matches = s1.engineGetMatches(recipient.getRID());

                if (!matches.isEmpty()) {
                      JceKeyTransEnvelopedRecipient ter = null;

                      if ("sun.security.mscapi.RSAPrivateKey".equals(privateKey.getClass().getCanonicalName() ) ) {
                            ter = new JceKeyTransEnvelopedRecipient(privateKey);
                            ter.setProvider( "SunMSCAPI" );
                            ter.setContentProvider(BouncyCastleProvider.PROVIDER_NAME);
                        } else {
                            ter = new JceKeyTransEnvelopedRecipient(privateKey);
                            ter.setProvider(BouncyCastleProvider.PROVIDER_NAME);
                        } 

                    return recipient.getContent(ter);
                }
            } else {
                throw new RuntimeException("Wrong type of RecipientInformation: " + recipient.getClass());
            }
            recipient=null;
        }

        if (recipient == null) {
            throw new RuntimeException("Could not find a matching recipient"); 
        }

    } catch (CMSException e) {
        throw new RuntimeException(e); // FIXME
    } catch (CertificateEncodingException e) {
        throw new RuntimeException(e);
    }
}

それが何であるかを助けてください。

どうもありがとう。

4

1 に答える 1

0

MSCAPI と PKCS#11 の両方を使用して、復号化に関して同じ問題が発生しています。SunPKCS11 に実装されている P11RSAChiper は、ラップ/アンラップ メソッドを考慮せず、この目的のために暗号化/復号化を使用することがわかりました。スマートカード プロファイル。

于 2014-04-06T12:05:58.927 に答える