1:RsaUtil.JAVA
import org.Apache.commons.codec.binary.Base64;import sun.misc.BASE64Decoder;import sun.misc.BASE64Encoder;import javax.crypto.Cipher;import javax.crypto.NoSuchPaddingException;import java.io.ByteArrayOutputStream;import java.io.IOException;import java.security.*;import java.security.interfaces.RSAPrivateKey;import java.security.interfaces.RSAPublicKey;import java.security.spec.InvalidKeySpecException;import java.security.spec.PKCS8EncodedKeySpec;import java.security.spec.X509EncodedKeySpec;import java.util.HashMap;import java.util.Map;public class RsaUtil {public static final String KEY_ALGORITHM = "RSA";//public static final String SIGNATURE_ALGORITHM = "MD5withRSA";private static final String PUBLIC_KEY = "FrankPublic";private static final String PRIVATE_KEY = "FrankPrivateKey";/*** RSA最大加密明文大小*/private static final int MAX_ENCRYPT_BLOCK = 117;/*** RSA最大解密密文大小*/private static final int MAX_DECRYPT_BLOCK = 128;//获得公钥public static String getPublicKey(Map<String, Object> keyMap) throws Exception {//获得map中的公钥对象 转为key对象Key key = (Key) keyMap.get(PUBLIC_KEY);//byte[] publicKey = key.getEncoded();//编码返回字符串return encryptBASE64(key.getEncoded());}//获得私钥public static String getPrivateKey(Map<String, Object> keyMap) throws Exception {//获得map中的私钥对象 转为key对象Key key = (Key) keyMap.get(PRIVATE_KEY);//byte[] privateKey = key.getEncoded();//编码返回字符串return encryptBASE64(key.getEncoded());}//解码返回bytepublic static byte[] decryptBASE64(String key) throws Exception {return (new BASE64Decoder()).decodeBuffer(key);}//编码返回字符串public static String encryptBASE64(byte[] key) throws Exception {return (new BASE64Encoder()).encodeBuffer(key);}//map对象中存放公私钥public static Map<String, Object> initKey() throws Exception {//获得对象 KeyPairGenerator 参数 RSA 1024个字节KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);keyPairGen.initialize(1024);//通过对象 KeyPairGenerator 获取对象KeyPairKeyPair keyPair = keyPairGen.generateKeyPair();//通过对象 KeyPair 获取RSA公私钥对象RSAPublicKey RSAPrivateKeyRSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();//公私钥对象存入map中Map<String, Object> keyMap = new HashMap<String, Object>(2);keyMap.put(PUBLIC_KEY, publicKey);keyMap.put(PRIVATE_KEY, privateKey);return keyMap;}/*** 获取私钥** @param privateKey 私钥字符串* @return*/public static PrivateKey getPrivateKey() throws Exception {String privateKey="XXX";KeyFactory keyFactory = KeyFactory.getInstance("RSA");byte[] decodedKey = Base64.decodeBase64(privateKey.getBytes());PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(decodedKey);return keyFactory.generatePrivate(keySpec);}/*** 获取公钥** @param publicKey 公钥字符串* @return*/public static PublicKey getPublicKey() throws Exception {String publicKey="XXX";KeyFactory keyFactory = KeyFactory.getInstance("RSA");byte[] decodedKey = Base64.decodeBase64(publicKey.getBytes());X509EncodedKeySpec keySpec = new X509EncodedKeySpec(decodedKey);return keyFactory.generatePublic(keySpec);}/*** RSA加密** @param data待加密数据* @return*/public static String encrypt(String data) throws Exception {Cipher cipher = Cipher.getInstance("RSA");cipher.init(Cipher.ENCRYPT_MODE, getPublicKey());int inputLen = data.getBytes().length;ByteArrayOutputStream out = new ByteArrayOutputStream();int offset = 0;byte[] cache;int i = 0;// 对数据分段加密while (inputLen - offset > 0) {if (inputLen - offset > MAX_ENCRYPT_BLOCK) {cache = cipher.doFinal(data.getBytes(), offset, MAX_ENCRYPT_BLOCK);} else {cache = cipher.doFinal(data.getBytes(), offset, inputLen - offset);}out.write(cache, 0, cache.length);i++;offset = i * MAX_ENCRYPT_BLOCK;}byte[] encryptedData = https://www.isolves.com/it/cxkf/yy/JAVA/2022-03-17/out.toByteArray();out.close();// 获取加密内容使用base64进行编码,并以UTF-8为标准转化成字符串// 加密后的字符串return new String(Base64.encodeBase64String(encryptedData));}/*** RSA解密** @param data待解密数据* @return*/public static String decrypt(String data) throws Exception {Cipher cipher = Cipher.getInstance("RSA");cipher.init(Cipher.DECRYPT_MODE, getPrivateKey());byte[] dataBytes = Base64.decodeBase64(data);int inputLen = dataBytes.length;ByteArrayOutputStream out = new ByteArrayOutputStream();int offset = 0;byte[] cache;int i = 0;// 对数据分段解密while (inputLen - offset > 0) {if (inputLen - offset > MAX_DECRYPT_BLOCK) {cache = cipher.doFinal(dataBytes, offset, MAX_DECRYPT_BLOCK);} else {cache = cipher.doFinal(dataBytes, offset, inputLen - offset);}out.write(cache, 0, cache.length);i++;offset = i * MAX_DECRYPT_BLOCK;}byte[] decryptedData = out.toByteArray();out.close();// 解密后的内容return new String(decryptedData, "UTF-8");}}
推荐阅读
- JavaScript 继承全解析
- JAVA三大框架
- JavaScript面向对象—继承的实现
- JavaScript 变量的秘密,你知道吗
- 如何解决高 Java CPU 使用率问题
- java实现、配图解,附源码 十大经典排序算法
- 最近我面了12个人,发现这个JAVA基础题都答得不好
- Java 面向对象进阶内容
- 人工智能文档编写器:使用AI生成Javadocs等文档的插件扩展
- JavaScript的ID生成器-Nano ID