Reputation: 1
I'm trying to use java for encrypt/decrypt text using "secp256r1" But unfortunately I can't it always produce error "Cannot handle supplied parameter spec: must be passed IES parameters"
Here is the full code example
import org.bouncycastle.crypto.generators.ECKeyPairGenerator;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import javax.crypto.Cipher;
import java.security.Security;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.SecureRandom;
import java.security.Signature;
import java.security.spec.ECGenParameterSpec;
import java.util.Base64;
public class Generator {
public static void main(String[] args) throws Exception {
Security.addProvider(new BouncyCastleProvider());
// Create KeyPairGenerator for ECC
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("EC");
ECGenParameterSpec ecSpec = new ECGenParameterSpec("secp256r1");
keyPairGenerator.initialize(ecSpec);
// Generate Key Pair
KeyPair keyPair = keyPairGenerator.generateKeyPair();
PrivateKey privateKey = keyPair.getPrivate();
PublicKey publicKey = keyPair.getPublic();
// Output the keys
System.out.println("Private Key: " + privateKey);
System.out.println("Public Key: " + publicKey);
// Message to encrypt
String originalMessage = "This is a secret message!";
// Convert to bytes
byte[] messageBytes = originalMessage.getBytes();
// Encrypt the message
Cipher cipher = Cipher.getInstance("ECIES", "BC");
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] encryptedMessage = cipher.doFinal(messageBytes);
System.out.println("Encrypted Message: " + Base64.getEncoder().encodeToString(encryptedMessage));
byte[] encryptedBytes = Base64.getDecoder().decode(encryptedMessage);
// Decrypt the message
Cipher cipher2 = Cipher.getInstance("ECIES", "BC");
cipher2.init(Cipher.DECRYPT_MODE, privateKey);
byte[] decryptedMessage = cipher2.doFinal(encryptedBytes);
System.out.println("Decrypted Message: " + new String(decryptedMessage));
}
}
I tried to remove BC but no result the full exception :
Exception in thread "main" java.lang.IllegalArgumentException: cannot handle supplied parameter spec: must be passed IES parameters
at org.bouncycastle.jcajce.provider.asymmetric.ec.IESCipher.engineInit(Unknown Source)
at java.base/javax.crypto.Cipher.init(Cipher.java:1296)
at java.base/javax.crypto.Cipher.init(Cipher.java:1236)
at com.example.Main.main(Main.java:42)
Upvotes: 0
Views: 110
Reputation: 1
It's the version of bouncycastle.
when use bcprov-jdk15on 1.70
it works perfect
But when use bcprov-jdk15to18 1.75
it produces the exception.
I used the version 1.77 the problem has gone. It's the latest version now on the time I wrote this answer
Upvotes: 0