今天让大家脱坑的是JAVA实现的RSA算法,代码网上当的,像我这样的菜鸡,只能搬砖,来个代码,修修补补,报错百度找教程。
1、软件环境: eclipse+jdk8.0
2、网上下载jar包
2.1.1下载链接:http://hc.apache.org/downloads.cgi
2.1.2下载文档的具体截图:
2.2下载链接:http://commons.apache.org/io/download_io.cgi然后进入镜像的链接是这样子的http://mirrors.hust.edu.cn/apache/commons/io/binaries/
2.2.2下载文档的具体截图:1:正儿八经的截图地址
2.2.3进入镜像后的截图
3、将下载好的jar包解压缩,打开eclipse,导入jar包:菜鸡做法,请轻喷!
3.1在你的项目文件上右击,新建文件夹,取个名儿
3.2把你辛苦下载好的jar包文件复制粘贴在文件夹下
3.3右击这个取啥名儿的文件夹-》构建路径-》add to buildpath
4、网上当的代码,写的很棒棒,让我这个菜鸡膜拜一下。
在此贴上大神的文档链接:https://blog.csdn.net/cz0217/article/details/78426733
RSAUtils,应该很详细了吧23333333333333333333
1 package com;
2
3 import org.apache.commons.codec.binary.Base64;
4 import org.apache.commons.io.IOUtils;
5
6 import javax.crypto.Cipher;
7 import java.io.ByteArrayOutputStream;
8 import java.security.*;
9 import java.security.interfaces.RSAPrivateKey;
10 import java.security.interfaces.RSAPublicKey;
11 import java.security.spec.InvalidKeySpecException;
12 import java.security.spec.PKCS8EncodedKeySpec;
13 import java.security.spec.X509EncodedKeySpec;
14 import java.util.HashMap;
15 import java.util.Map;
16
17 public class RSAUtils {
18
19 public static final String CHARSET = "UTF-8";
20 public static final String RSA_ALGORITHM = "RSA";
21
22
23 public static Map<String, String> createKeys(int keySize){
24 //为RSA算法创建一个KeyPairGenerator对象
25 KeyPairGenerator kpg;
26 try{
27 kpg = KeyPairGenerator.getInstance(RSA_ALGORITHM);
28 }catch(NoSuchAlgorithmException e){
29 throw new IllegalArgumentException("No such algorithm-->[" + RSA_ALGORITHM + "]");
30 }
31
32 //初始化KeyPairGenerator对象,密钥长度
33 kpg.initialize(keySize);
34 //生成密匙对
35 KeyPair keyPair = kpg.generateKeyPair();
36 //得到公钥
37 Key publicKey = keyPair.getPublic();
38 String publicKeyStr = Base64.encodeBase64URLSafeString(publicKey.getEncoded());
39 //得到私钥
40 Key privateKey = keyPair.getPrivate();
41 String privateKeyStr = Base64.encodeBase64URLSafeString(privateKey.getEncoded());
42 Map<String, String> keyPairMap = new HashMap<String, String>();
43 keyPairMap.put("publicKey", publicKeyStr);
44 keyPairMap.put("privateKey", privateKeyStr);
45
46 return keyPairMap;
47 }
48
49 /**
50 * 得到公钥
51 * @param publicKey 密钥字符串(经过base64编码)
52 * @throws Exception
53 */
54 public static RSAPublicKey getPublicKey(String publicKey) throws NoSuchAlgorithmException, InvalidKeySpecException {
55 //通过X509编码的Key指令获得公钥对象
56 KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALGORITHM);
57 X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(Base64.decodeBase64(publicKey));
58 RSAPublicKey key = (RSAPublicKey) keyFactory.generatePublic(x509KeySpec);
59 return key;
60 }
61
62 /**
63 * 得到私钥
64 * @param privateKey 密钥字符串(经过base64编码)
65 * @throws Exception
66 */
67 public static RSAPrivateKey getPrivateKey(String privateKey) throws NoSuchAlgorithmException, InvalidKeySpecException {
68 //通过PKCS#8编码的Key指令获得私钥对象
69 KeyFactory keyFactory = KeyFactory.getInstance(RSA_ALGORITHM);
70 PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(Base64.decodeBase64(privateKey));
71 RSAPrivateKey key = (RSAPrivateKey) keyFactory.generatePrivate(pkcs8KeySpec);
72 return key;
73 }
74
75 /**
76 * 公钥加密
77 * @param data
78 * @param publicKey
79 * @return
80 */
81 public static String publicEncrypt(String data, RSAPublicKey publicKey){
82 try{
83 Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
84 cipher.init(Cipher.ENCRYPT_MODE, publicKey);
85 return Base64.encodeBase64URLSafeString(rsaSplitCodec(cipher, Cipher.ENCRYPT_MODE, data.getBytes(CHARSET), publicKey.getModulus().bitLength()));
86 }catch(Exception e){
87 throw new RuntimeException("加密字符串[" + data + "]时遇到异常", e);
88 }
89 }
90
91 /**
92 * 私钥解密
93 * @param data
94 * @param privateKey
95 * @return
96 */
97
98 public static String privateDecrypt(String data, RSAPrivateKey privateKey){
99 try{
100 Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
101 cipher.init(Cipher.DECRYPT_MODE, privateKey);
102 return new String(rsaSplitCodec(cipher, Cipher.DECRYPT_MODE, Base64.decodeBase64(data), privateKey.getModulus().bitLength()), CHARSET);
103 }catch(Exception e){
104 throw new RuntimeException("解密字符串[" + data + "]时遇到异常", e);
105 }
106 }
107
108 /**
109 * 私钥加密
110 * @param data
111 * @param privateKey
112 * @return
113 */
114
115 public static String privateEncrypt(String data, RSAPrivateKey privateKey){
116 try{
117 Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
118 cipher.init(Cipher.ENCRYPT_MODE, privateKey);
119 return Base64.encodeBase64URLSafeString(rsaSplitCodec(cipher, Cipher.ENCRYPT_MODE, data.getBytes(CHARSET), privateKey.getModulus().bitLength()));
120 }catch(Exception e){
121 throw new RuntimeException("加密字符串[" + data + "]时遇到异常", e);
122 }
123 }
124
125 /**
126 * 公钥解密
127 * @param data
128 * @param publicKey
129 * @return
130 */
131
132 public static String publicDecrypt(String data, RSAPublicKey publicKey){
133 try{
134 Cipher cipher = Cipher.getInstance(RSA_ALGORITHM);
135 cipher.init(Cipher.DECRYPT_MODE, publicKey);
136 return new String(rsaSplitCodec(cipher, Cipher.DECRYPT_MODE, Base64.decodeBase64(data), publicKey.getModulus().bitLength()), CHARSET);
137 }catch(Exception e){
138 throw new RuntimeException("解密字符串[" + data + "]时遇到异常", e);
139 }
140 }
141
142 private static byte[] rsaSplitCodec(Cipher cipher, int opmode, byte[] datas, int keySize){
143 int maxBlock = 0;
144 if(opmode == Cipher.DECRYPT_MODE){
145 maxBlock = keySize / 8;
146 }else{
147 maxBlock = keySize / 8 - 11;
148 }
149 ByteArrayOutputStream out = new ByteArrayOutputStream();
150 int offSet = 0;
151 byte[] buff;
152 int i = 0;
153 try{
154 while(datas.length > offSet){
155 if(datas.length-offSet > maxBlock){
156 buff = cipher.doFinal(datas, offSet, maxBlock);
157 }else{
158 buff = cipher.doFinal(datas, offSet, datas.length-offSet);
159 }
160 out.write(buff, 0, buff.length);
161 i++;
162 offSet = i * maxBlock;
163 }
164 }catch(Exception e){
165 throw new RuntimeException("加解密阀值为["+maxBlock+"]的数据时发生异常", e);
166 }
167 byte[] resultDatas = out.toByteArray();
168 IOUtils.closeQuietly(out);
169 return resultDatas;
170 }
171
172
173
174 public static void main (String[] args) throws Exception {
175 Map<String, String> keyMap = RSAUtils.createKeys(1024);
176 String publicKey = keyMap.get("publicKey");
177 String privateKey = keyMap.get("privateKey");
178 System.out.println("公钥: \n\r" + publicKey);
179 System.out.println("私钥: \n\r" + privateKey);
180
181 System.out.println("公钥加密——私钥解密");
182 String str = "\n" +
183 "成长带走的不只是时光\n" +
184 "还带走了当初那些不害怕失去的勇气\n" +
185 "让自己忙一点,忙到没时间去思考无关紧要的事,很多事就能悄悄淡忘了\n" +
186 "时间不一定能证明很多东西\n" +
187 "但是一定能看透很多东西\n" +
188 "坚信自己的选择,不动摇,使劲跑,明天会更好";
189 System.out.println("\r明文:\r\n" + str);
190 System.out.println("\r明文大小:\r\n" + str.getBytes().length);
191 String encodedData = RSAUtils.publicEncrypt(str, RSAUtils.getPublicKey(publicKey));
192 System.out.println("密文:\r\n" + encodedData);
193 String decodedData = RSAUtils.privateDecrypt(encodedData, RSAUtils.getPrivateKey(privateKey));
194 System.out.println("解密后文字: \r\n" + decodedData);
195
196
197 }
198 }
代码运行截图在这里:
稍微皮一下:
今天是2018年5月27日,首先呢恭喜小哥哥拿到了蓝桥杯国二,祝福啊,来个撒花233333333333333333---
/更新一下,2021年5月8号,他不再是的小哥哥,祝他找的女朋友哪里都好,就是不爱他,祝他爸妈能找到令他们满意的儿媳,恋爱5年多,他爸妈装死,真的谈恋爱可以,结婚我不行,垃圾,不要在垃圾堆里找垃圾,也希望看到这里的某位小朋友,珍惜校园爱情,经历过社会的毒打,可就没有那么坦诚的爱情啦,好好对她
//再次更新,感谢下方评论的好友们,希望大家都能顺顺利利的,找到共度一生的好伙伴呀,我很好,大家不用担心,若有什么疑惑,可以私信我的知乎,id为是我的小芒果呀
ps.会不会被打,溜了溜了