开启成长之旅!这是我参加「日新方案 12 月更文应战」的第25天,点击查看活动概况​​​

成功:

java实现ecc加密:通过AES生成公钥加密数据,ECC加密公钥
java实现ecc加密:通过AES生成公钥加密数据,ECC加密公钥

本文经过。java言语完成ECC+AES混合加密。ECC加密算法具有密钥分配与办理简略,安全强度高等长处,AES的加密算法具有速度快,强度高,便于完成等长处。Ecc椭圆曲线算法对AES公钥进行加密办理,AES主要为咱们数据进行加密。经过集成AES加密算法和ECC加密算法的长处,完成了加密速度快和安全便利办理密钥的长处,有效地处理了暗码体系中速度和安全性不能统筹的问题。JDK中自带了椭圆曲线的签名,可是没有完成椭圆曲线的加密解密。经过引进bouncycastle库完成完成椭圆曲线的加密解密。

  1. 去JDK的下载页面,下载

www.oracle.com/technetwork…
这个东西。这个是为了解除默认JDK中的加密强度的约束。不使用这个可能会报错。
下载下来今后,需要将local_policy.jar 和 US_export_policy.jar替换掉D:\Program Files\Java\jdk1.8.0_91\jre\lib\security下面的相同的两个jar包。

目录

经过AES获取公钥和私钥

进行ECC加密


基于AES的加密算法具有速度快,强度高,便于完成等长处

ECC加密算法具有密钥分配与办理简略,安全强度高等长处

选用AES加密算法加密大数据块, 而用ECC加密算法办理AES密钥

经过集成AES加密算法和ECC加密算法的长处,完成了加密速度快和安全便利办理密钥的长处,有效地处理了暗码体系中速度和安全性不能统筹的问题.

AES加密标准又称为高级加密标准Rijndael加密法,是美国国家标准技术研究所NIST旨在取代DES的21世纪的加密标准。AES的基本要求是,选用对称分组暗码体系,密钥长度可以为128、192或256位,分组长度128位,算法应易在各种硬件和软件上完成。

AES属于对称加密算法;加密、解密使用相同的密钥,AES加解密过程如下图所示:

java实现ecc加密:通过AES生成公钥加密数据,ECC加密公钥
java实现ecc加密:通过AES生成公钥加密数据,ECC加密公钥​修改

经过AES获取公钥和私钥

package utils;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.io.IOException;
public class AESUtil {
    //生成AES秘钥,然后Base64编码
    public static String genKeyAES() throws Exception{
        KeyGenerator keyGen = KeyGenerator.getInstance("AES");
        keyGen.init(128);
        SecretKey key = keyGen.generateKey();
        String base64Str = byte2Base64(key.getEncoded());
        return base64Str;
    }
    //将Base64编码后的AES秘钥转换成SecretKey目标
    public static SecretKey loadKeyAES(String base64Key) throws Exception{
        byte[] bytes = base642Byte(base64Key);
        SecretKeySpec key = new SecretKeySpec(bytes, "AES");
        return key;
    }
    //加密
    public static byte[] encryptAES(byte[] source, SecretKey key) throws Exception{
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.ENCRYPT_MODE, key);
        return cipher.doFinal(source);
    }
    //解密
    public static byte[] decryptAES(byte[] source, SecretKey key) throws Exception{
        Cipher cipher = Cipher.getInstance("AES");
        cipher.init(Cipher.DECRYPT_MODE, key);
        return cipher.doFinal(source);
    }
    //字节数组转Base64编码
    public static String byte2Base64(byte[] bytes){
        BASE64Encoder encoder = new BASE64Encoder();
        return encoder.encode(bytes);
    }
    //Base64编码转字节数组
    public static byte[] base642Byte(String base64Key) throws IOException {
        BASE64Decoder decoder = new BASE64Decoder();
        return decoder.decodeBuffer(base64Key);
    }
}

java实现ecc加密:通过AES生成公钥加密数据,ECC加密公钥

进行ECC加密

package utils;
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;
public class ECCUtil {
    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 AESUtil.byte2Base64(bytes);
    }
    //获取私钥(Base64编码)
    public static String getPrivateKey(KeyPair keyPair){
        ECPrivateKey privateKey = (ECPrivateKey) keyPair.getPrivate();
        byte[] bytes = privateKey.getEncoded();
        return AESUtil.byte2Base64(bytes);
    }
    //将Base64编码后的公钥转换成PublicKey目标
    public static ECPublicKey string2PublicKey(String pubStr) throws Exception{
        byte[] keyBytes = AESUtil.base642Byte(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 = AESUtil.base642Byte(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 = ECCUtil.getKeyPair();
        String publicKeyStr = ECCUtil.getPublicKey(keyPair);
        String privateKeyStr = ECCUtil.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);
        byte[] privateDecrypt = privateDecrypt(publicEncrypt, privateKey);
        System.out.println(new String(privateDecrypt));
    }
}

java实现ecc加密:通过AES生成公钥加密数据,ECC加密公钥