无法创建密码,没有这样的算法

时间:2016-04-09 15:39:44

标签: java encryption bouncycastle

我正在使用BountyCastle使用算法AES128_CBC加密文件,代码如下:

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 ();
}

但是,我在行

上一直有以下异常

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

有什么想法吗?

1 个答案:

答案 0 :(得分:2)

您无法使用DSA算法或DSA密钥进行加密(1.2.840.10040.4.1为DSA)。 DSA代表数字签名算法。请尝试使用RSA。