2012-11-03 40 views
8

におけるCMSは、私は、暗号化し、PKCS7形式で復号化するためにはBouncyCastleを使用したいです。私はハードウェアトークンを持っています。私は私のハードドライブにJKSファイルに鍵ペアを使用する場合には、正常に動作が、私はトークン で鍵ペアを使用するときに機能しません。暗号化と復号化 - Javaの

Exception in thread "main" org.bouncycastle.cms.CMSException: cannot create cipher: No such algorithm: 2.16.840.1.101.3.4.1.2 
    at org.bouncycastle.cms.jcajce.EnvelopedDataHelper.createCipher(Unknown Source) 
    at org.bouncycastle.cms.jcajce.EnvelopedDataHelper$1.doInJCE(Unknown Source) 
    at org.bouncycastle.cms.jcajce.EnvelopedDataHelper.execute(Unknown Source) 
    at org.bouncycastle.cms.jcajce.EnvelopedDataHelper.createContentCipher(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 pktb.PKTB.CmsDecrypt(PKTB.java:288) 
    at pktb.PKTB.main(PKTB.java:419) 
Caused by: java.security.NoSuchAlgorithmException: No such algorithm: 2.16.840.1.101.3.4.1.2 
    at javax.crypto.Cipher.getInstance(DashoA13*..) 
    at javax.crypto.Cipher.getInstance(DashoA13*..) 
    at org.bouncycastle.jcajce.NamedJcaJceHelper.createCipher(Unknown Source) 
    ... 10 more 
Java Result: 1 

が、これは私の暗号化コードです:これは私の例外で

public byte[] CmsEncrypt(byte[] message, KeyContainer keyContainer) throws NoSuchAlgorithmException, NoSuchProviderException, CMSException, IOException 
{ 
    Security.addProvider(new BouncyCastleProvider()); 
    X509Certificate cert = (X509Certificate) keyContainer.certificate; 
    CMSEnvelopedDataGenerator gen = new CMSEnvelopedDataGenerator(); 
    gen.addKeyTransRecipient(cert); 
    CMSProcessable data = new CMSProcessableByteArray(message); 
    CMSEnvelopedData enveloped = gen.generate(data, 
    CMSEnvelopedDataGenerator.AES128_CBC, "BC"); 

    return enveloped.getEncoded(); 

} 

、これが私の解読コードです:

public byte[] CmsDecrypt(byte[] cipher, KeyContainer keyContainer) throws CMSException, IOException, NoSuchProviderException 
    { 
     Security.addProvider(new BouncyCastleProvider()); 
     byte[] contents=null; 
     CMSEnvelopedDataParser envelopedDataParser = new CMSEnvelopedDataParser(new ByteArrayInputStream(cipher)); 
     PrivateKey key = keyContainer.privateKey; 
     X509Certificate cert = keyContainer.certificate; 
     CMSEnvelopedData enveloped = new CMSEnvelopedData(cipher); 
     Collection recip = enveloped.getRecipientInfos().getRecipients(); 
     KeyTransRecipientInformation rinfo = (KeyTransRecipientInformation) recip 
        .iterator().next(); 
     if(keyContainer.provider.equals("Software")) 
      contents = rinfo.getContent(
       new JceKeyTransEnvelopedRecipient(key).setProvider("BC")); 
     else 
      contents = rinfo.getContent(
       new JceKeyTransEnvelopedRecipient(key).setProvider("SunPKCS11-" + keyContainer.provider)); 
     System.out.println(new String(contents)); 
     return contents; 

    } 

は私がcmsSignため、このトークンプロバイダーを使用すると言わなければなりませんとcmsVerifyそれは正常に動作するので、私は問題がプロバイダのためではないと思う。

+2

他の読者には、2.16.840.1.101.3.4.1.2が[128-bit AES CBC-mode](http://www.alvestrand.no/objectid/2.16.840.1)のOIDです。 .101.3.4.1.2.html)。 –

+1

PKCS#11トークンプロバイダが128ビットAESをサポートしていますか?対称アルゴリズムを有効にするためにトークンの設定を変更する必要はありますか? –

+0

あなたは[メーリングリスト弾む城](http://www.bouncycastle.org/mailing_lists.html)でこれを尋ねたほうが良いです。 –

答えて

1

あなたは、ハードウェアトークンから秘密鍵と公開鍵を抽出し、その後はBouncyCastle PKCS7を使用してデータを暗号化および復号化するために、これらの抽出された公開鍵と秘密鍵を使用するPKCS#11を使用することができます。どのトークンを使用していますか?また、私はハードウェアトークンからキーを抽出するコードを見つけることができません。ハードウェアトークンからキーを抽出するには、以下のリンクの解答を参照してください。 Click here