bcgit / bc-java

Bouncy Castle Java Distribution (Mirror)
https://www.bouncycastle.org/java.html
MIT License
2.33k stars 1.14k forks source link

Exception on version bcprov-jdk15to18 1.75 but not on version bcprov-jdk15on 1.70 #1858

Closed Blacktoviche closed 1 month ago

Blacktoviche commented 1 month ago

This code working perfect on version bcprov-jdk15on 1.70 but on version bcprov-jdk15to18 1.75 it produce an exception.

`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));

    // Decrypt the message
    Cipher cipher2 = Cipher.getInstance("ECIES", "BC");
    cipher2.init(Cipher.DECRYPT_MODE, privateKey);
    byte[] decryptedMessage = cipher2.doFinal(encryptedMessage);

    System.out.println("Decrypted Message: " + new String(decryptedMessage));
}

}`

The exception is :

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)

The old version is full of vulnerabilities that's why I must upgrade to the new version.

Any idea why this is not working on the new version?

Blacktoviche commented 1 month ago

Using the version 1.77 solved the exception.