2016-04-09 14 views
2

böyle algoritmayı oluşturulamıyor:şifre, Aşağıdaki kod ile algoritmayı <code>AES128_CBC</code> kullanarak bir dosya şifrelemek için <code>BountyCastle</code> kullanıyorum

static { 
    Provider provider = Security.getProvider (BouncyCastleProvider.PROVIDER_NAME); 
    if (provider == null) { 
     Security.addProvider (new BouncyCastleProvider()); 
    } 
} 

public static void main (String[] args) throws IOException, CertificateException, UnrecoverableKeyException, KeyStoreException, 
               NoSuchAlgorithmException, InvalidAlgorithmParameterException, NoSuchProviderException, 
               CertStoreException, CMSException, NoSuchPaddingException, InvalidKeyException, 
               ShortBufferException, IllegalBlockSizeException, BadPaddingException { 

    File f    = new File ("ToBeEncrypted.txt"); 
    byte[] buffer  = new byte [(int)f.length()]; 
    DataInputStream in = new DataInputStream (new FileInputStream (f)); 
    in.readFully (buffer); 
    in.close(); 

    X509Certificate cert = ReadX509.read (new FileInputStream ("test.cer")); 

    CMSEnvelopedDataGenerator gen = new CMSEnvelopedDataGenerator(); 
    RecipientInfoGenerator recipientGenerator = new JceKeyTransRecipientInfoGenerator (cert).setProvider ("BC"); 
    gen.addRecipientInfoGenerator (recipientGenerator); 

    OutputEncryptor outputEncryptor = new JceCMSContentEncryptorBuilder (CMSAlgorithm.AES128_CBC).build(); 
    CMSEnvelopedData envData  = gen.generate (new CMSProcessableByteArray (buffer), outputEncryptor); 

    byte[] pkcs7envelopedData = envData.getEncoded(); 

    FileOutputStream envfos = new FileOutputStream ("ToBeDecrypted.pk7"); 
    envfos.write (pkcs7envelopedData); 
    envfos.close(); 
} 

Ancak, ben on line aşağıdaki istisna sahip tutmak

CMSEnvelopedData envData = gen.generate (new CMSProcessableByteArray (buffer), outputEncryptor);:

Exception in thread "main" org.bouncycastle.cms.CMSException: exception wrapping content key: cannot create cipher: No such algorithm: 1.2.840.10040.4.1 
    at org.bouncycastle.cms.KeyTransRecipientInfoGenerator.generate(Unknown Source) 
    at org.bouncycastle.cms.CMSEnvelopedDataGenerator.doGenerate(Unknown Source) 
    at org.bouncycastle.cms.CMSEnvelopedDataGenerator.generate(Unknown Source) 
    at com.crypto.tests.EncryptDocument.main(EncryptDocument.java:74) 

Caused by: org.bouncycastle.operator.OperatorCreationException: cannot create cipher: No such algorithm: 1.2.840.10040.4.1 
    at org.bouncycastle.operator.jcajce.OperatorHelper.createAsymmetricWrapper(Unknown Source) 
    at org.bouncycastle.operator.jcajce.JceAsymmetricKeyWrapper.generateWrappedKey(Unknown Source) 
... 4 more 

Caused by: java.security.NoSuchAlgorithmException: No such algorithm: 1.2.840.10040.4.1 
    at javax.crypto.Cipher.getInstance(Cipher.java:688) 
    at javax.crypto.Cipher.getInstance(Cipher.java:596) 
    at org.bouncycastle.jcajce.util.NamedJcaJceHelper.createCipher(Unknown Source) 
    ... 6 more 

Herhangi bir fikir?

cevap

3

DSA algoritması veya DSA anahtarı ile şifreleyemezsiniz (1.2.840.10040.4.1, DSA'dır). DSA, Dijital İmzası Algoritması anlamına gelir. Bunun yerine RSA kullanmayı deneyin.

+0

Teşekkür ederiz. Yani temel olarak söylediklerim kullandığım sertifikanın DSA tarafından oluşturulduğu mu? – Copernic

+1

@Copernic Bu durum * çok iyi olabilir evet. RSA daha iyi bir fikir olurdu. –

+0

Oh .. Anlam veriyor! Teşekkürler @Maarten! – Copernic

İlgili konular