简体   繁体   English

在 Java 中使用 BouncyCastle 使用 ECIES 进行加密

[英]Using BouncyCastle to encrypt with ECIES in Java

I am trying to encrypt some content using ECC algorithm using BouncyCastle in java.我正在尝试使用 Java 中的 BouncyCastle 使用 ECC 算法加密一些内容。 But I am getting exception of BouncyCastle library saying cannot cast JCEECPublicKey to IESKey .但是我得到了 BouncyCastle 库的例外,说不能将JCEECPublicKeyIESKey Which I understood that the public key generated by KeyPairGenerator is JCEECPublicKey which cannot be used in java Cipher.init method.据我了解, KeyPairGenerator生成的公钥是JCEECPublicKey ,不能在 java Cipher.init方法中使用。 Can someone tell me how can convert it in Public key or X509 spec so that I can use it in encryption.有人可以告诉我如何将其转换为公钥或 X509 规范,以便我可以在加密中使用它。

Here is the code which I tried这是我试过的代码

// add instance of provider class
Security.addProvider(new BouncyCastleProvider());

// initializing parameter specs secp256r1/prime192v1
ECParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec("prime192v1");

// key pair generator to generate public and private key
KeyPairGenerator generator = KeyPairGenerator.getInstance("ECDH", new BouncyCastleProvider());

// initialize key pair generator
generator.initialize(ecSpec);

// Key pair to store public and private key
KeyPair keyPair = generator.generateKeyPair();

Cipher iesCipher = Cipher.getInstance("ECIES", new BouncyCastleProvider());
iesCipher.init(Cipher.ENCRYPT_MODE, keyPair.getPublic());

Also I tried to convert the public key into X509EncodedSpec but I get same exception我也尝试将公钥转换为 X509EncodedSpec 但我得到了同样的异常

X509EncodedKeySpec spec = new X509EncodedKeySpec(keyPair.getPublic().getEncoded());
KeyFactory factory = KeyFactory.getInstance("ECDH");

PublicKey publicKey = factory.generatePublic(spec);

The exception which I am getting is我得到的例外是

java.lang.ClassCastException: org.bouncycastle.jce.provider.JCEECPublicKey cannot be cast to org.bouncycastle.jce.interfaces.IESKey
    at org.bouncycastle.jce.provider.JCEIESCipher.engineGetKeySize(JCEIESCipher.java:49)
    at javax.crypto.Cipher.passCryptoPermCheck(Cipher.java:1057)
    at javax.crypto.Cipher.checkCryptoPerm(Cipher.java:1015)
    at javax.crypto.Cipher.init(Cipher.java:1229)
    at javax.crypto.Cipher.init(Cipher.java:1173)
    at com.test.EciesTest.main(EciesTest.java:45)

EDIT编辑

Based on comment the JDK version I am using is JDK 7 - Oracle Import statements I am using:根据评论,我使用的 JDK 版本是 JDK 7 - 我使用的 Oracle 导入语句:

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.Security;
import java.security.spec.ECGenParameterSpec;
import javax.crypto.Cipher;
import org.bouncycastle.jce.provider.BouncyCastleProvider;

Try the following:请尝试以下操作:

// add instance of provider class
Security.addProvider(new BouncyCastleProvider());

String name = "secp256r1";

// NOTE just "EC" also seems to work here
KeyPairGenerator kpg = KeyPairGenerator.getInstance("ECDH", BouncyCastleProvider.PROVIDER_NAME);
kpg.initialize(new ECGenParameterSpec(name));

// Key pair to store public and private key
KeyPair keyPair = kpg.generateKeyPair();

Cipher iesCipher = Cipher.getInstance("ECIES", BouncyCastleProvider.PROVIDER_NAME);
iesCipher.init(Cipher.ENCRYPT_MODE, keyPair.getPublic());

And note that in general it is best to keep to JCE classes instead of Bouncy Castle classes when trying to use Bouncy through the JCE.请注意,一般来说,当尝试通过 JCE 使用 Bouncy 时,最好保留 JCE 类而不是 Bouncy Castle 类。 In this case the problem was probably the parameters given to the key generator.在这种情况下,问题可能是给密钥生成器的参数。

In above code I used BouncyCastleProvider.PROVIDER_NAME but just "BC" would work equally well of course.在上面的代码中,我使用了BouncyCastleProvider.PROVIDER_NAME但当然只是"BC"也能很好地工作。 Re-instantiating the provider each time is not a good idea, although it should not have influenced the end result.每次都重新实例化提供者不是一个好主意,尽管它不应该影响最终结果。


Make sure you've got an up to date system to run this code.确保您有一个最新的系统来运行此代码。 This code was tested on the following system:此代码在以下系统上进行了测试:

 --- runtime information --- 
Properties:
    java.vendor                : Oracle Corporation
    java.specification.name    : Java Platform API Specification
    java.specification.version : 1.8
    java.runtime.name          : Java(TM) SE Runtime Environment
    java.runtime.version       : 1.8.0_65-b17
    java.vm.name               : Java HotSpot(TM) 64-Bit Server VM
Unlimited crypto: yes
 --- info for provider Bouncy Castle --- 
Bouncy Castle version: 1.520000
Bouncy Castle provider registered: yes

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM