我正在尝试在系统中实现 El-Gamal 加密算法。我刚刚了解了该算法,并在互联网上查找了代码以获得实现它的想法。我在互联网上找到了以下代码并给了我这个奇怪的错误:
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)
这是代码段:
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));
}
}
我对密码学很陌生,这可能很愚蠢。如果这个问题太愚蠢,我很抱歉。并提前感谢。