亲宝软件园·资讯

展开

java通过AES生成公钥加密数据ECC加密公钥

ZhangJiQun_MXP 人气:0

成功

本文通过java语言实现ECC+AES混合加密。

ECC加密算法具有密钥分配与管理简单,安全强度高等优点,AES的加密算法具有速度快,强度高,便于实现等优点。Ecc椭圆曲线算法对AES公钥进行加密管理,AES主要为我们数据进行加密。

通过集成AES加密算法和ECC加密算法的优点,实现了加密速度快和安全方便管理密钥的优点,有效地解决了密码体制中速度和安全性不能兼顾的问题。JDK中自带了椭圆曲线的签名,但是没有实现椭圆曲线的加密解密。通过引入bouncycastle库实现实现椭圆曲线的加密解密。

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密钥

通过集成AES加密算法和ECC加密算法的优点,实现了加密速度快和安全方便管理密钥的优点,有效地解决了密码体制中速度和安全性不能兼顾的问题.

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

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

通过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);
    }
}

进行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));
    }
}

加载全部内容

相关教程
猜你喜欢
用户评论