|
1.简介:ECC 是 Elliptic Curves Cryptography 的缩写,属于公开密钥算法,意为椭圆曲线编码编码学。ECC的主要优势是在某些情况下它比其他的方法使用更小的密钥——比如RSA加密算法——提供相当的或更高等级的安全,可以用较少的计算能力提供比RSA加密算法更高的安全强度,有效地解决了“提高安全强度必须增加密钥长度”的工程实现问题。ECC的另一个优势是可以定义群之间的双线性映射,基于Weil对或是Tate对;双线性映射已经在密码学中发现了大量的应用,例如基于身份的加密。不过一个缺点是加密和解密操作的实现比其他机制花费的时间长。
2.原理: 椭圆曲线 K=kG,其中K,G为Ep(a,b)上的点,k为小于n的整数,n是点G的阶,设K为公钥,k为私钥,G为基点。
加密过程: A选定一条椭圆曲线Ep(a,b),并取曲线上一点作为基点G ;A选择一个私钥k,并生成公钥K=kG ,A将Ep(a,b)和k,G发送给B,B收到后将明文编码到Ep(a,b)上一点M,并产生一个随机数r ;B计算点C1=M+rK,C2=rG ;B将C1,C2传给A ;A计算C1-kC2=M+rkG-krG=M ;A对M解码得到明文;攻击者只能得到Ep(a,b),G,K,C1,C2,没有k就无法得到M。
3.代码:
- package tttt;
- import java.security.KeyFactory;
- import java.security.KeyPair;
- import java.security.KeyPairGenerator;
- import java.security.PrivateKey;
- import java.security.PublicKey;
- import java.security.SecureRandom;
- import java.security.Security;
- import java.security.spec.PKCS8EncodedKeySpec;
- import java.security.spec.X509EncodedKeySpec;
-
- import javax.crypto.Cipher;
-
- import org.bouncycastle.jce.interfaces.ECPrivateKey;
- import org.bouncycastle.jce.interfaces.ECPublicKey;
- import org.springframework.util.Base64Utils;
-
-
- public class test {
- static {
- Security.addProvider(new org.bouncycastle.jce.provider.BouncyCastleProvider());
- }
-
- //生成秘钥对
- public static KeyPair getKeyPair() throws Exception {
- KeyPairGenerator keyPairGenerator = KeyPairGenerator.getInstance("EC", "BC");
- keyPairGenerator.initialize(256, new SecureRandom());
- KeyPair keyPair = keyPairGenerator.generateKeyPair();
- return keyPair;
- }
-
- //获取公钥(Base64编码)
- public static String getPublicKey(KeyPair keyPair){
- ECPublicKey publicKey = (ECPublicKey) keyPair.getPublic();
- byte[] bytes = publicKey.getEncoded();
- return Base64Utils.encodeToString(bytes);
- }
-
- //获取私钥(Base64编码)
- public static String getPrivateKey(KeyPair keyPair){
- ECPrivateKey privateKey = (ECPrivateKey) keyPair.getPrivate();
- byte[] bytes = privateKey.getEncoded();
- return Base64Utils.encodeToString(bytes);
- }
-
- //将Base64编码后的公钥转换成PublicKey对象
- public static ECPublicKey string2PublicKey(String pubStr) throws Exception{
- byte[] keyBytes = Base64Utils.decodeFromString(pubStr);
- X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
- KeyFactory keyFactory = KeyFactory.getInstance("EC", "BC");
- ECPublicKey publicKey = (ECPublicKey) keyFactory.generatePublic(keySpec);
- return publicKey;
- }
-
- //将Base64编码后的私钥转换成PrivateKey对象
- public static ECPrivateKey string2PrivateKey(String priStr) throws Exception{
- byte[] keyBytes = Base64Utils.decodeFromString(priStr);
- PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
- KeyFactory keyFactory = KeyFactory.getInstance("EC", "BC");
- ECPrivateKey privateKey = (ECPrivateKey) keyFactory.generatePrivate(keySpec);
- return privateKey;
- }
-
- //公钥加密
- public static byte[] publicEncrypt(byte[] content, PublicKey publicKey) throws Exception{
- Cipher cipher = Cipher.getInstance("ECIES", "BC");
- cipher.init(Cipher.ENCRYPT_MODE, publicKey);
- byte[] bytes = cipher.doFinal(content);
- return bytes;
- }
-
- //私钥解密
- public static byte[] privateDecrypt(byte[] content, PrivateKey privateKey) throws Exception{
- Cipher cipher = Cipher.getInstance("ECIES", "BC");
- cipher.init(Cipher.DECRYPT_MODE, privateKey);
- byte[] bytes = cipher.doFinal(content);
- return bytes;
- }
-
- public static void main(String[] args) throws Exception {
- KeyPair keyPair = test.getKeyPair();
- String publicKeyStr = test.getPublicKey(keyPair);
- String privateKeyStr = test.getPrivateKey(keyPair);
- System.out.println("ECC公钥Base64编码:" + publicKeyStr);
- System.out.println("ECC私钥Base64编码:" + privateKeyStr);
-
- ECPublicKey publicKey = string2PublicKey(publicKeyStr);
- ECPrivateKey privateKey = string2PrivateKey(privateKeyStr);
-
- byte[] publicEncrypt = publicEncrypt("Hello World!".getBytes(), publicKey);
- String encodeToString = Base64Utils.encodeToString(publicEncrypt);
- System.out.println(encodeToString);
- byte[] privateDecrypt = privateDecrypt(publicEncrypt, privateKey);
- System.out.println(new String(privateDecrypt));
- }
- }
复制代码
|
|