跳到主要内容

cannot handle supplied parameter spec must be passed IES parameters

这个问题是因为,你没有在初始化公钥和私钥的时候增加IES parameters ,对其增加即可。

这里给出一个加解密的案例, IESParameterSpec 则是你需要修改的。

Security.addProvider(new BouncyCastleProvider());

// 获取 secp521r1 曲线参数
ECNamedCurveParameterSpec ecSpec = ECNamedCurveTable.getParameterSpec("secp521r1");

// 使用 secp521r1 曲线参数生成密钥对
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("EC", "BC");
keyPairGenerator.initialize(ecSpec);
KeyPair keyPair = keyPairGenerator.generateKeyPair();

// 模拟要加密的数据
String input = "Hello, this is a test message.";

// 使用公钥进行加密
IESParameterSpec params = new IESParameterSpec(null, null, 128, 128, null);
Cipher encryptCipher = Cipher.getInstance("ECIES", "BC");
encryptCipher.init(Cipher.ENCRYPT_MODE, keyPair.getPublic(), params);

byte[] encryptedBytes = encryptCipher.doFinal(input.getBytes());

// 使用私钥进行解密
Cipher decryptCipher = Cipher.getInstance("ECIES", "BC");
decryptCipher.init(Cipher.DECRYPT_MODE, keyPair.getPrivate(), params);

byte[] decryptedBytes = decryptCipher.doFinal(encryptedBytes);

String decryptedString = new String(decryptedBytes);
System.out.println("Decrypted string: " + decryptedString);

另外,你可以在他们的官方仓库找到unit test尝试 https://github.com/bcgit/bc-java/blob/main/prov/src/test/java/org/bouncycastle/jce/provider/test/ECIESTest.java