0

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.

Monowar Anjum
  • 41
  • 1
  • 7
  • Have you tried adjusting the key size? This might be a bug in BC. Which version do you use and have you tried another? – Artjom B. Mar 26 '16 at 12:03
  • I tried, but didn't work. But I found on the internet that El-Gamal works on the key size of minimum 160. So corrected the key size later to 512. @Aritjom.B – Monowar Anjum Mar 28 '16 at 14:30

1 Answers1

2

All right folks. I found the problem finally. There is nothing wrong with the code except the key size. It's a mainly dependency issue.

  1. First change the key size to 160 or greater in the code.

  2. Download and extract bouncyCastle API jar files from their site. Follow the procedure listed here : http://www.bouncycastle.org/wiki/display/JA1/Provider+Installation

I used the static installation method.

  1. Download Java Cryptography Extension (JCE) Unlimited Strength Jurisdiction Policy Files for your JDK. For version 8 go to this link and follow the instructions in read me file after downloading and extracting. http://www.oracle.com/technetwork/java/javase/downloads/jce8-download-2133166.html

    1. Locate the jre\lib\security directory for the Java instance that the Atom is using.For example, this location might be: C:\Program Files\Java\jre8\lib\security.

    2. Remove the following .jar files from this directory: local_policy.jar and US_export_policy.jar.

    3. Replace these two files with the .jar files included in the JCE Unlimited Strength Jurisdiction Policy Files download.

    4. Reboot your machine and you are good to go.

P.S. During step (3.2) you might want to make a copy of your local policy files first in case something wrong happens . If still things don't work you might wanna follow the same procedure for the JRE folder in the JDK folder of your PC. That should make it work.

Artjom B.
  • 61,146
  • 24
  • 125
  • 222
Monowar Anjum
  • 41
  • 1
  • 7