I am trying to implement El-Gamal Encryption algorithm in a system. I just learned about the algorithm and looked up the internet for codes to get an idea to implement it. I found the following code on the internet and gives me this weird error:
Exception in thread "main" java.lang.IllegalArgumentException:'min' may not be greater than 'max'
at org.bouncycastle.util.BigIntegers.createRandomInRange(Unknown Source)
at org.bouncycastle.crypto.generators.DHKeyGeneratorHelper.calculatePrivate(Unknown Source)
at org.bouncycastle.crypto.generators.ElGamalKeyPairGenerator.generateKeyPair(Unknown Source)
at org.bouncycastle.jcajce.provider.asymmetric.elgamal.KeyPairGeneratorSpi.generateKeyPair(Unknown Source)
at ElGamal.main(ElGamal.java:20)
Here is the code segment:
import java.security.Key;
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;
import java.security.Security;
import javax.crypto.Cipher;
public class ElGamal {
public static void main(String[] args) throws Exception {
Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
byte[] input = "abcdefgh".getBytes();
Cipher cipher = Cipher.getInstance("ElGamal/None/NoPadding", "BC");
KeyPairGenerator generator = KeyPairGenerator.getInstance("ElGamal", "BC");
SecureRandom random = new SecureRandom();
generator.initialize(128, random);
KeyPair pair = generator.generateKeyPair();
Key pubKey = pair.getPublic();
Key privKey = pair.getPrivate();
cipher.init(Cipher.ENCRYPT_MODE, pubKey, random);
byte[] cipherText = cipher.doFinal(input);
System.out.println("cipher: " + new String(cipherText));
cipher.init(Cipher.DECRYPT_MODE, privKey);
byte[] plainText = cipher.doFinal(cipherText);
System.out.println("plain : " + new String(plainText));
}
}
I am very new to to cryptography and this is probably silly . I am sorry if the question is too silly. And thanks in advance.