Java使用RSA的公钥加密,私钥解密;私钥加密,公钥解密
Java使用RSA的公钥加密,私钥解密;私钥加密,公钥解密
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.system.common.util.RedisUtil;import javax.crypto.Cipher;
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;@Slf4j
public class RSAEncrypt {public static void main(String[] args) throws Exception {String point = "xxx";
// Map<String, String> keyMap = genKeyPair(point);//生成公钥和私钥Map<String, String> keyMap = new HashMap<>();//genKeyPair();keyMap.put("publicKey", "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDOUDwJVY1JMY4oSHM1+VKeYZ5T2LjQ4wvENnt0TlRoOYDrUen4Nm3GbVKiGTot76gu7xYL1X9PQvDnYLpUVu0mA2oLrXWZj2ByTW83Ehdc5Y9aLXtNzmm4e6PXtuEtXk2sqUZz+XtBBAUMAne4J9G9DAZVPLFxUUJyzVb9cnK6NQIDAQAB");keyMap.put("privateKey", "MIICdwIBADANBgkqhkiG9w0BAQEFAASCAmEwggJdAgEAAoGBAM5QPAlVjUkxjihIczX5Up5hnlPYuNDjC8Q2e3ROVGg5gOtR6fg2bcZtUqIZOi3vqC7vFgvVf09C8OdgulRW7SYDagutdZmPYHJNbzcSF1zlj1ote03Oabh7o9e24S1eTaypRnP5e0EEBQwCd7gn0b0MBlU8sXFRQnLNVv1ycro1AgMBAAECgYAKkJlCcRsXEG6TKYKc1POiIKWW7ZYpPDcyCQgxYIF6BNfRNRSiHUdpzddZbalJCOi33o5mdLxcNrVXY+CmyPzDyeyNWWX8UcL2Wud8vRlWU7kQ+YcCVyS/nqRLBpHb0QgW7bqzb7fRpnmqhfj+A9hzRaoKxsZ8EWQfvN5UcdmQgQJBAPyTXzHcicK6gsgaXVo8awXsKxT6/bVAq7+FO/F4ckflS3oyABFNnqVRTC3nuQsU6nq3fu1kDwa03NcAa5zZaykCQQDRHEw4N0pnGKdecKTjBlD95B9WI0KCMWHpSOTIZJUIEvKXANX5BFaGHY01BNxDmwVcuecHgG2XH/WyIzVeJEQtAkEA8WM/NX4aQvrRhsB7u4PGnPBq9DA0TQeznOSOt2ZvgfrIOc6TdfYCyuh5r92oYcjpl8LLEcHxAm3UKb8DGfJIkQJAAjyYQB2vSQ0FdUglK1x870pKX4R/CJ94maMy90XEJlL1j1Ht9/zo5ARa509G/94fn49JflYMVgp8eUxRHNGsfQJBAKm39ZUaFyuDSpRINHZNHfldasmy9hLyXdTb3sLFj/bPaq0MyORAZPqq6XCu+nnIhVKVyADbXb+8T5kn70lzIbg=");String publicKey = keyMap.get("publicKey");String privateKey = keyMap.get("privateKey");System.out.println(publicKey);System.out.println(privateKey);
// //加密字符串String message = "我是测试";System.out.println("前端请求的原数据\n"+message);String messageEn = publicKeyEncrypt(message, publicKey,point);System.out.println("前端请求的加密:\n" + messageEn);String messageDe = privateKeyDecrypt(messageEn, privateKey,point);System.out.println("后端解密出来的数据:\n" + messageDe);System.out.println("=====================");//前端数据展示处理
// //私钥加密,公钥解密String s = privateKeyEncrypt(messageDe, privateKey,point);System.out.println("后端返回的加密数据\n"+s);
// s = "M3sQjI8vSpWzyuiyNakHaMRb0QgKSt9aHoAYj1G6cLenPJ235IqGwsipO/tU8WlObn35aUM0vXKCmeLO0E1Qip7FKoYB4OYhD74anwXZFwb7cTXBiazvP5FaN0Fb7vJRWrApfU0JgSqifRCfjNza1pSTxLc8kD3oZ+an88nzVzw=";
// String s = "azwa1HWeAjiP2Y2rIJot/cgi9Li31Rs92ycpv/vBYf3ydprwEJii7ZNYo98mGLWh20JZ0ur43FwhYWQw/uZCPHhGMCg7yK3CIqpEMSzzUxXLVxMf8qPmF7wzbNQ6LG+YVDwGHMr+R3r4sKYpw41ZxQzEFd61c7tNqZ9oaouBL3A=";String s1 = publicKeyDecrypt(s, publicKey,point);System.out.println("前端解密出来显示的数据\n"+s1);}/*** 随机生成密钥对** @throws NoSuchAlgorithmException* @param point*/public static Map<String, String> genKeyPair(String point) throws NoSuchAlgorithmException {log.info("{}|开始生成公私钥",point);// 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 = new String(Base64.encodeBase64(publicKey.getEncoded()));// 得到私钥字符串String privateKeyString = new String(Base64.encodeBase64((privateKey.getEncoded())));// 将公钥和私钥保存到MapMap<String, String> map = new HashMap<>();map.put("publicKey", publicKeyString);map.put("privateKey", privateKeyString);log.info("{}|生成的公私钥|map:{}",point,map);return map;}/*** RSA公钥加密** @param str 加密字符串* @param publicKey 公钥* @return 密文* @throws Exception 加密过程中的异常信息*/public static String publicKeyEncrypt(String str, String publicKey,String point) throws Exception {log.info("{}|RSA公钥加密前的数据|str:{}|publicKey:{}",point,str,publicKey);//base64编码的公钥byte[] decoded = Base64.decodeBase64(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 = Base64.encodeBase64String(cipher.doFinal(str.getBytes("UTF-8")));log.info("{}|公钥加密后的数据|outStr:{}",point,outStr);return outStr;}/*** RSA私钥解密** @param str 加密字符串* @param privateKey 私钥* @param point* @return 铭文* @throws Exception 解密过程中的异常信息*/public static String privateKeyDecrypt(String str, String privateKey, String point) throws Exception {log.info("{}|RSA私钥解密前的数据|str:{}|privateKey:{}",point,str,privateKey);//64位解码加密后的字符串byte[] inputByte = Base64.decodeBase64(str.getBytes("UTF-8"));//base64编码的私钥byte[] decoded = Base64.decodeBase64(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));log.info("{}|RSA私钥解密后的数据|outStr:{}",point,outStr);return outStr;}/*** RSA私钥加密** @param str* @param privateKey* @return* @throws Exception*/public static String privateKeyEncrypt(String str, String privateKey,String point) throws Exception {log.info("{}|RSA私钥加密前的数据|str:{}|publicKey:{}",point,str,privateKey);//base64编码的公钥byte[] decoded = Base64.decodeBase64(privateKey);PrivateKey priKey = KeyFactory.getInstance("RSA").generatePrivate(new PKCS8EncodedKeySpec(decoded));//RSA加密Cipher cipher = Cipher.getInstance("RSA");cipher.init(Cipher.ENCRYPT_MODE, priKey);String outStr = Base64.encodeBase64String(cipher.doFinal(str.getBytes()));log.info("{}|RSA私钥加密后的数据|outStr:{}",point,outStr);return outStr;}/*** RSA公钥解密** @param str* @param publicKey* @return* @throws Exception*/public static String publicKeyDecrypt(String str, String publicKey,String point) throws Exception {log.info("{}|RSA公钥解密前的数据|str:{}|publicKey:{}",point,str,publicKey);//64位解码加密后的字符串byte[] inputByte = Base64.decodeBase64(str.getBytes("UTF-8"));//base64编码的私钥byte[] decoded = Base64.decodeBase64(publicKey);PublicKey pubKey = KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded));//RSA解密Cipher cipher = Cipher.getInstance("RSA");cipher.init(Cipher.DECRYPT_MODE, pubKey);String outStr = new String(cipher.doFinal(inputByte));log.info("{}|RSA公钥解密后的数据|outStr:{}",point,outStr);return outStr;}}
通过RSA进行前端与后端的交互,
前端获取到公钥
后端获取到私钥
前端发起请求,通过公钥加密,后端用私钥解密
后端通知前端,通过私钥加密,前端通过公钥解密
前端通过公钥解密的逻辑没有测试过,找到的资源地址为:
https://github.com/kjur/jsrsasign/blob/master/src/rsasign-1.2.js#L234
错误信息: Data must not be longer than 128 bytes
当加解密的数据长度过长,需要对数据进行分割
加密则117字节,解密则128字节
/*** RSA公钥加密** @param str 加密字符串* @param publicKey 公钥* @return 密文* @throws Exception 加密过程中的异常信息*/public static String publicKeyEncrypt(String str, String publicKey, String point) throws Exception {log.info("{}|RSA公钥加密前的数据|str:{}|publicKey:{}", point, str, publicKey);//base64编码的公钥byte[] decoded = Base64.decodeBase64(publicKey);RSAPublicKey pubKey = (RSAPublicKey) KeyFactory.getInstance("RSA").generatePublic(new X509EncodedKeySpec(decoded));//RSA加密Cipher cipher = Cipher.getInstance("RSA");cipher.init(Cipher.ENCRYPT_MODE, pubKey);//当长度过长的时候,需要分割后加密 117个字节byte[] resultBytes = getMaxResultEncrypt(str, point, cipher);String outStr = Base64.encodeBase64String(resultBytes);log.info("{}|公钥加密后的数据|outStr:{}", point, outStr);return outStr;}private static byte[] getMaxResultEncrypt(String str, String point, Cipher cipher) throws IllegalBlockSizeException, BadPaddingException {byte[] inputArray = str.getBytes();int inputLength = inputArray.length;log.info("{}|加密字节数|inputLength:{}", point, inputLength);// 最大加密字节数,超出最大字节数需要分组加密int MAX_ENCRYPT_BLOCK = 117;// 标识int offSet = 0;byte[] resultBytes = {};byte[] cache = {};while (inputLength - offSet > 0) {if (inputLength - offSet > MAX_ENCRYPT_BLOCK) {cache = cipher.doFinal(inputArray, offSet, MAX_ENCRYPT_BLOCK);offSet += MAX_ENCRYPT_BLOCK;} else {cache = cipher.doFinal(inputArray, offSet, inputLength - offSet);offSet = inputLength;}resultBytes = Arrays.copyOf(resultBytes, resultBytes.length + cache.length);System.arraycopy(cache, 0, resultBytes, resultBytes.length - cache.length, cache.length);}return resultBytes;}/*** RSA私钥解密** @param str 加密字符串* @param privateKey 私钥* @param point* @return 铭文* @throws Exception 解密过程中的异常信息*/public static String privateKeyDecrypt(String str, String privateKey, String point) throws Exception {log.info("{}|RSA私钥解密前的数据|str:{}|privateKey:{}", point, str, privateKey);//64位解码加密后的字符串byte[] inputByte = Base64.decodeBase64(str.getBytes("UTF-8"));//base64编码的私钥byte[] decoded = Base64.decodeBase64(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));//当长度过长的时候,需要分割后解密 128个字节String outStr = new String(getMaxResultDecrypt(str, point, cipher));log.info("{}|RSA私钥解密后的数据|outStr:{}", point, outStr);return outStr;}private static byte[] getMaxResultDecrypt(String str, String point, Cipher cipher) throws IllegalBlockSizeException, BadPaddingException, UnsupportedEncodingException {byte[] inputArray = Base64.decodeBase64(str.getBytes("UTF-8"));int inputLength = inputArray.length;log.info("{}|解密字节数|inputLength:{}", point, inputLength);// 最大解密字节数,超出最大字节数需要分组加密int MAX_ENCRYPT_BLOCK = 128;// 标识int offSet = 0;byte[] resultBytes = {};byte[] cache = {};while (inputLength - offSet > 0) {if (inputLength - offSet > MAX_ENCRYPT_BLOCK) {cache = cipher.doFinal(inputArray, offSet, MAX_ENCRYPT_BLOCK);offSet += MAX_ENCRYPT_BLOCK;} else {cache = cipher.doFinal(inputArray, offSet, inputLength - offSet);offSet = inputLength;}resultBytes = Arrays.copyOf(resultBytes, resultBytes.length + cache.length);System.arraycopy(cache, 0, resultBytes, resultBytes.length - cache.length, cache.length);}return resultBytes;}
Java使用RSA的公钥加密,私钥解密;私钥加密,公钥解密相关推荐
- 分享一个RSA加解密工具类,公钥加密私钥解密、私钥加密公钥解密、私钥签名公钥验签、生成公钥私钥
测试: public static void main(String[] args) {try {//生成公钥私钥Map<String, Object> map = RSAUtil.ini ...
- RSA不对称加密,公钥加密私钥解密,私钥加密公钥解密
RSA算法是第一个能同时用于加密和数字签名的算法,也易于理解和操作. RSA是被研究得最广泛的公钥算法,从提出到现在已近二十年,经历了各种攻击的考验,逐渐为人们接受,普遍认为是目前最优秀的公钥方案之一 ...
- 公钥加密私钥解密私钥加密公钥解密
公钥加密体制 1.公钥加密体制用于保密性时,就是公钥加密,私钥解密. 因为公钥是可以公开了, 那么任何人都可以使用公钥对信息进行加密,但是只有持有私钥的人才能正确解密.这样就保证了信息的保密性,因为只 ...
- 非对称加密 公钥解密_了解非对称公钥加密
非对称加密 公钥解密 Asymmetric cryptography, also called public key cryptography, is an essential element of ...
- oracle 加密怎么解密,oracle加密encrypt,解密decrypt,
oracle加密encrypt,解密decrypt, 目录 oracle加密encrypt,解密decrypt加密 解密 oracle加密encrypt,解密decrypt 有的oracle版本没有加 ...
- oracle加密 解密,oracle加密encrypt,解密decrypt
本文将为您描述oracle加密encrypt,解密decrypt,教程操作步骤:oracle加密encrypt,解密decrypt 目录 oracle加密encrypt,解密decrypt 加密 解密 ...
- C#实现RSA公钥加密私钥解密、私钥加密公钥解密以及Pcks12、X509证书加解密、签名验签
RSA的私钥签名公钥验签可以见 http://blog.csdn.net/starfd/article/details/51917916,所以这里就没提供对应代码,具体代码如下: using Org. ...
- RSA双向加解密(公钥加密-私钥解密;私钥加密-公钥解密)
非对称加密算法中,提供一个公钥一个私钥.一般情况下,采用公钥加密.私钥解密的方式. 假设有这样一个场景:服务A与服务B需要通信,通信内容为了安全需要进行加密传输,并且服务A与服务B不能互相持有对方的钥 ...
- openssl公钥加密私钥解密和私钥加密公钥解密
最近在弄音视频上云,参考了腾讯云中SecretId和SecretKey,直观理解SecretKey是私钥,用于签名,然后公钥验证签名,个人理解SecretId在腾讯云系统里面有一条记录,此记录存放着公 ...
最新文章
- Java中的内存分配
- 清除string内容_python爬取哔哩哔哩网页弹幕内容,并将爬取的内容以五角星的形式显示出来...
- 驱动程序和应用程序之间的体系结构不匹配_修复Win10上的黑屏问题全攻略,并不高深,一看就会...
- cf12E Start of the season(构造,,,)
- oracle异常策略,oracle segmentation fault错误
- 漫谈程序员系列 薪资,你是我不能言说的伤
- 植物大战僵尸 修改存档和金钱
- Redis为什么是单线程?高并发响应快?
- 关于战棋对战化的设想和实现
- 微信公众号的Api 一些说明 关于 WeixinJSBridge API
- 安卓海外SDK接入问题
- 我也写写致青春观后感
- linux arm fpu初始化,如何确定Linux主板是否使用硬件FPU?
- 百事起诉可口可乐广告不当
- 写在入职两周年纪念日
- 怎样判断一个P2P平台是否靠谱?
- Hadoop fs 常用命令
- 第 46 届 ICPC 国际大学生程序设计竞赛亚洲区域赛(上海)DEGHI
- 易经卦象基本类别与其类象运用思路
- 【合集】Jerry Wang 2018~2021 四年期间的 SAP 技术文章合集