转自 :http://blog.sina.com.cn/s/blog_57432f380101s2l7.html
JRE8 + jce_policy-8, Bouncy Castle 1.50环境下调试通过
import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.Security;
import javax.crypto.Cipher;
import javax.xml.bind.annotation.adapters.HexBinaryAdapter;
import org.bouncycastle.jce.interfaces.ECPrivateKey;
import org.bouncycastle.jce.interfaces.ECPublicKey;
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import org.bouncycastle.math.ec.ECCurve;
import org.bouncycastle.math.ec.ECPoint;
public class TestECC {
public static void main(String[] args) throws Exception {
byte[] plainText = "Hello World!".getBytes();
byte[] cipherText = null;
Security.addProvider(new BouncyCastleProvider());
//生成公钥和私钥
KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("ECIES", "BC");
KeyPair keyPair = keyPairGenerator.generateKeyPair();
ECPublicKey ecPublicKey = (ECPublicKey) keyPair.getPublic();
ECPrivateKey ecPrivateKey = (ECPrivateKey) keyPair.getPrivate();
//打印密钥信息
ECCurve ecCurve = ecPublicKey.getParameters().getCurve();
System.out.println("椭圆曲线参数a = " + ecCurve.getA().toBigInteger());
System.out.println("椭圆曲线参数b = " + ecCurve.getB().toBigInteger());
System.out.println("椭圆曲线参数q = " + ((ECCurve.Fp) ecCurve).getQ());
ECPoint basePoint = ecPublicKey.getParameters().getG();
System.out.println("基点橫坐标 "
+ basePoint.getAffineXCoord().toBigInteger());
System.out.println("基点纵坐标 "
+ basePoint.getAffineYCoord().toBigInteger());
System.out.println("公钥横坐标 "
+ ecPublicKey.getQ().getAffineXCoord().toBigInteger());
System.out.println("公钥纵坐标 "
+ ecPublicKey.getQ().getAffineYCoord().toBigInteger());
System.out.println("私钥 " + ecPrivateKey.getD());
Cipher cipher = Cipher.getInstance("ECIESwithDESede/NONE/PKCS7Padding", "BC");
// 加密
cipher.init(Cipher.ENCRYPT_MODE, ecPublicKey);
cipherText = cipher.doFinal(plainText);
System.out.println("密文: " + new HexBinaryAdapter().marshal(cipherText));
// 解密
cipher.init(Cipher.DECRYPT_MODE, ecPrivateKey);
plainText = cipher.doFinal(cipherText);
// 打印解密后的明文
System.out.println("解密后的明文: " + new String(plainText));
}
}