You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
3.6 KiB

3 years ago
package com.eco.plugin.xx.grsso.utils;
import com.fr.json.JSONObject;
import com.fr.log.FineLoggerFactory;
import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;
import javax.crypto.Cipher;
import java.io.IOException;
import java.net.URLEncoder;
import java.security.*;
import java.security.interfaces.RSAPrivateKey;
import java.security.interfaces.RSAPublicKey;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.HashMap;
import java.util.Map;
public class EncryptUtils {
/**
* base64加密
* @param key
* @return
*/
public static String base64Encode(byte[] key){
return (new BASE64Encoder()).encodeBuffer(key);
}
/**
* base64解密
* @param key
* @return
*/
public static byte[] base64DecodeB(String key){
byte[] result = null;
try {
result = (new BASE64Decoder()).decodeBuffer(key);
} catch (IOException e) {
FineLoggerFactory.getLogger().info("FRLOG:BASE64解密异常:"+e.getMessage());
}
return result;
}
/**
* 随机生成RSA密钥对
* @throws NoSuchAlgorithmException
* @return privateKey,publicKey
*/
public static Map<String,String> genRSAKeyPair() throws NoSuchAlgorithmException {
// KeyPairGenerator类用于生成公钥和私钥对,基于RSA算法生成对象
KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance("RSA");
// 初始化密钥对生成器,密钥大小为96-1024位
keyPairGen.initialize(1024,new SecureRandom());
// 生成一个密钥对,保存在keyPair中
KeyPair keyPair = keyPairGen.generateKeyPair();
RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate(); // 得到私钥
RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic(); // 得到公钥
String publicKeyString = base64Encode(publicKey.getEncoded());
// 得到私钥字符串
String privateKeyString = base64Encode(privateKey.getEncoded());
// 将公钥和私钥保存到Map
Map<String,String> result = new HashMap<String,String>();
result.put("publicKey",publicKeyString);
result.put("privateKey",privateKeyString);
return result;
}
/**
* rsa 加密
* @param str
* @param publicKey
* @return
* @throws Exception
*/
public static String rsaEncrypt(String str,String publicKey) throws Exception {
//base64编码的公钥
byte[] decoded = base64DecodeB(publicKey);
RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded));
//RSA加密
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.ENCRYPT_MODE, pubKey);
String outStr = base64Encode(cipher.doFinal(str.getBytes("UTF-8")));
return outStr;
}
/**
* rsa解密
* @param str
* @param privateKey
* @return
* @throws Exception
*/
public static String rsaDecrypt(String str,String privateKey) throws Exception {
//64位解码加密后的字符串
byte[] inputByte = base64DecodeB(str);
//base64编码的私钥
byte[] decoded = base64DecodeB(privateKey);
RSAPrivateKey priKey = (RSAPrivateKey) KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded));
//RSA解密
Cipher cipher = Cipher.getInstance("RSA");
cipher.init(Cipher.DECRYPT_MODE, priKey);
String outStr = new String(cipher.doFinal(inputByte));
return outStr;
}
}