我正在尝试通过改编以下代码来测试 ECIES:https ://gist.github.com/amrishodiq/9821413和https://github.com/VictorThompson/t-encryption/blob/master/src/ECIESexample .java
我得到错误:
Exception in thread "main" java.security.NoSuchAlgorithmException: Cannot find any provider supporting ECIES
at javax.crypto.Cipher.getInstance(Cipher.java:540)
at Ecies.encrypt(Ecies.java:51)
at Ecies.main(Ecies.java:21)
即使 ECIES 在罐子里:
$ jar tvf bcprov-jdk15on-152.jar | grep ECIES
4214 Sun Mar 01 12:03:08 GMT 2015 org/bouncycastle/crypto/kems/ECIESKeyEncapsulation.class
1497 Sun Mar 01 12:03:08 GMT 2015 org/bouncycastle/crypto/parsers/ECIESPublicKeyParser.class
795 Sun Mar 01 12:03:08 GMT 2015 org/bouncycastle/jcajce/provider/asymmetric/ec/IESCipher$ECIES.class
1037 Sun Mar 01 12:03:08 GMT 2015 org/bouncycastle/jcajce/provider/asymmetric/ec/IESCipher$ECIESwithAES.class
1108 Sun Mar 01 12:03:08 GMT 2015 org/bouncycastle/jcajce/provider/asymmetric/ec/IESCipher$ECIESwithAESCBC.class
1046 Sun Mar 01 12:03:08 GMT 2015 org/bouncycastle/jcajce/provider/asymmetric/ec/IESCipher$ECIESwithDESede.class
1117 Sun Mar 01 12:03:08 GMT 2015 org/bouncycastle/jcajce/provider/asymmetric/ec/IESCipher$ECIESwithDESedeCBC.class
我究竟做错了什么?
Ecies.java:
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.SecureRandom;
import java.security.Signature;
import java.security.SignatureException;
import java.security.spec.ECGenParameterSpec;
import javax.crypto.Cipher;
import org.bouncycastle.jce.spec.IEKeySpec;
import org.bouncycastle.jce.spec.IESParameterSpec;
public class Ecies {
private SecureRandom random;
private int keySize;
private KeyPair akey;
private KeyPair bkey;
public static void main(String[] args) throws Exception {
Ecies ecies = new Ecies();
byte[] plaintext = "Mary had a litle lamb.".getBytes();
println("plaintext.length: " + plaintext.length);
byte[] encrypted = ecies.encrypt(plaintext);
println("encrypted.length: " + encrypted.length);
byte[] decrypted = ecies.decrypt(encrypted);
println("decrypted.length: " + decrypted.length);
println("new String(decrypted): " + new String(decrypted));
}
public static void println(String string) {
System.out.println(string);
}
public Ecies () throws Exception{
this.random = new SecureRandom();
}
public void establishKeys(String keysize) throws Exception {
ECGenParameterSpec ecGenSpec = new ECGenParameterSpec(keysize);
KeyPairGenerator keyGen = KeyPairGenerator.getInstance("EC");
keyGen.initialize(ecGenSpec, random);
this.akey = keyGen.generateKeyPair();
this.bkey = keyGen.generateKeyPair();
this.keySize = Integer.valueOf( (ecGenSpec.getName().substring(4, 7)) ).intValue();
}
public byte[] encrypt(byte[] plainText) throws Exception {
// get ECIES cipher objects
Cipher acipher = Cipher.getInstance("ECIES");
// generate derivation and encoding vectors
byte[] d = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
byte[] e = new byte[] { 8, 7, 6, 5, 4, 3, 2, 1 };
IESParameterSpec param = new IESParameterSpec(d, e, 256);
// encrypt the plaintext using the public key
acipher.init(Cipher.ENCRYPT_MODE, new IEKeySpec(akey.getPrivate(), bkey.getPublic()), param);
return acipher.doFinal(plainText);
}
public byte[] decrypt(byte[] cipherText) throws Exception {
// get ECIES cipher objects
Cipher bcipher = Cipher.getInstance("ECIES");
// generate derivation and encoding vectors
byte[] d = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
byte[] e = new byte[] { 8, 7, 6, 5, 4, 3, 2, 1 };
IESParameterSpec param = new IESParameterSpec(d, e, 256);
// decrypt the text using the private key
bcipher.init(Cipher.DECRYPT_MODE, new IEKeySpec(bkey.getPrivate(), akey.getPublic()), param);
return bcipher.doFinal(cipherText);
}
public byte[] sign(byte[] plainText) throws Exception {
Signature sig = Signature.getInstance("SHA1WithECDSA");
sig.initSign(akey.getPrivate());
sig.update(plainText);
return sig.sign();
}
public boolean verify(byte[] plainText, byte[] signature) throws Exception {
Signature sig = Signature.getInstance("SHA1WithECDSA");
sig.initVerify(akey.getPublic());
sig.update(plainText);
try {
if (sig.verify(signature)) {
return true;
} else return false;
} catch (SignatureException se) {
System.out.println( "Signature failed" );
}
return false;
}
public int getKeySize() {
return keySize;
}
}
生成文件:
all: Ecies.class
java -cp bcprov-jdk15on-152.jar:. Ecies
Ecies.class: clean
javac -cp bcprov-jdk15on-152.jar Ecies.java
clean:
rm -f Ecies.class