ECC和RSA加解密使用
package com.dk.core;
import javax.crypto.Cipher;
import java.security.*;
import java.security.interfaces.ECPrivateKey;
import java.security.interfaces.ECPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;
/**
-
ecc 加密/解密
*/
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) {
PublicKey publicKey = keyPair.getPublic();
byte[] bytes = publicKey.getEncoded();
return byte2Base64(bytes);
}private static String byte2Base64(byte[] bytes) {
return Base64.getUrlEncoder().encodeToString(bytes);
}//获取私钥(Base64编码)
public static String getPrivateKey(KeyPair keyPair) {
PrivateKey privateKey = keyPair.getPrivate();
byte[] bytes = privateKey.getEncoded();
return byte2Base64(bytes);
}//将Base64编码后的公钥转换成PublicKey对象
public static ECPublicKey string2PublicKey(String pubStr) throws Exception {
byte[] keyBytes = base642Byte(pubStr);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(“EC”, “BC”);
ECPublicKey publicKey = (ECPublicKey) keyFactory.generatePublic(keySpec);
return publicKey;
}
//将Base64编码后的公钥转换成PublicKey对象
public static PublicKey string2RSAPublicKey(String pubStr) throws Exception {
byte[] keyBytes = base642Byte(pubStr);
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(“RSA”, “BC”);
PublicKey publicKey = keyFactory.generatePublic(keySpec);
return publicKey;
}
private static byte[] base642Byte(String pubStr) {
return Base64.getUrlDecoder().decode(pubStr);
}//将Base64编码后的私钥转换成PrivateKey对象
public static ECPrivateKey string2PrivateKey(String priStr) throws Exception {
byte[] keyBytes = base642Byte(priStr);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(“EC”, “BC”);
ECPrivateKey privateKey = (ECPrivateKey) keyFactory.generatePrivate(keySpec);
return privateKey;
}
//将Base64编码后的私钥转换成PrivateKey对象
public static PrivateKey string2RSAPrivateKey(String priStr) throws Exception {
byte[] keyBytes = base642Byte(priStr);
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(keyBytes);
KeyFactory keyFactory = KeyFactory.getInstance(“RSA”, “BC”);
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
return privateKey;
}
//ECIES公钥加密
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;
}
//RSA公钥加密
public static byte[] publicEncryptByRSA(byte[] content, PublicKey publicKey) throws Exception {
Cipher cipher = Cipher.getInstance(“RSA”, “BC”);
cipher.init(Cipher.ENCRYPT_MODE, publicKey);
byte[] bytes = cipher.doFinal(content);
return bytes;
}
//ECIES私钥解密
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;
}
//RSA私钥解密
public static byte[] privateDecryptByRSA(byte[] content, PrivateKey privateKey) throws Exception {
Cipher cipher = Cipher.getInstance(“RSA”, “BC”);
cipher.init(Cipher.DECRYPT_MODE, privateKey);
byte[] bytes = cipher.doFinal(content);
return bytes;
}
public static KeyPair getKeyPair(String algorithm,String provider) throws Exception{
KeyPairGenerator instance = KeyPairGenerator.getInstance(algorithm, provider);
instance.initialize(2048,new SecureRandom());
return instance.generateKeyPair();
}public static void main(String[] args) throws Exception {
KeyPair keyPair = ECCUtil.getKeyPair(“RSA”,“BC”);
String privateKey = ECCUtil.getPrivateKey(keyPair);
String publicKey = ECCUtil.getPublicKey(keyPair);
System.out.println(“privateKey:”+privateKey);
System.out.println(“publicKey:”+publicKey);
String str= “test!!!!”;
byte[] bytes = publicEncryptByRSA(str.getBytes(“utf-8”), ECCUtil.string2RSAPublicKey(publicKey));
byte[] bytes1 = privateDecryptByRSA(bytes, ECCUtil.string2RSAPrivateKey(privateKey));
System.out.println(new String(bytes1,“utf-8”));
}
}