很简单的一个需求,ipad端给密码RSA加密,传到java后台,解密。RSA加密算法是基于一个密钥对的,分为公钥和私钥,一般情况公钥加密,私钥解密,但也可私钥加密,公钥解密。还可以验签,就是先用私钥对数据进行加密,然后对加密后的数据进行签名,得到一个签名值。然后再用公钥先验签,证明是对应私钥加密过的数据才解密。主要是为了防止来源不确定的数据。     根据上面的介绍,大家也都知道,RSA算法的关键就是密钥对,我和IOS的同事各自找了RSA的算法实现代码,都能正常根据密钥对加解密。问题是我们各自使用对方的密钥对就不能加解密成功。IOS同事也是一个新手。连RSA算法是个什么概念都没搞清楚,我也懂点IOS。所以就陪着他一起看代码,找资料。看到底什么原因引起的密钥对不能共用。后来找到下面这篇文章: Java中使用OpenSSL生成的RSA公私钥进行数据加解密 原来在用mac 系统中自带的openssl生成的密钥对文件是X509编码格式的。而我们JAVA所需的私钥文件是PKCS#8编码格式的。。所以要将在mac 系统中生成的私钥文件转下码就行了。转码方式参考上面链接。附下java代码: 

Java代码  

1. import
2. import
3. import
4. import
5. import
6. import
7. import
8. import
9. import
10. import
11. import
12. import
13. import
14. import
15. import
16.   
17. import
18. import
19. import
20. import
21.   
22. import
23. import
24.   
25. public class
26. private static final
27. "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQChDzcjw/rWgFwnxunbKp7/4e8w" + "\r"
28. "/UmXx2jk6qEEn69t6N2R1i/LmcyDT1xr/T2AHGOiXNQ5V8W4iCaaeNawi7aJaRht" + "\r"
29. "Vx1uOH/2U378fscEESEG8XDqll0GCfB1/TjKI2aitVSzXOtRs8kYgGU78f7VmDNg" + "\r"
30. "XIlk3gdhnzh+uoEQywIDAQAB" + "\r";    
31.         
32. private static final
33. "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAKEPNyPD+taAXCfG" + "\r"
34. "6dsqnv/h7zD9SZfHaOTqoQSfr23o3ZHWL8uZzINPXGv9PYAcY6Jc1DlXxbiIJpp4" + "\r"
35. "1rCLtolpGG1XHW44f/ZTfvx+xwQRIQbxcOqWXQYJ8HX9OMojZqK1VLNc61GzyRiA" + "\r"
36. "ZTvx/tWYM2BciWTeB2GfOH66gRDLAgMBAAECgYBp4qTvoJKynuT3SbDJY/XwaEtm" + "\r"
37. "u768SF9P0GlXrtwYuDWjAVue0VhBI9WxMWZTaVafkcP8hxX4QZqPh84td0zjcq3j" + "\r"
38. "DLOegAFJkIorGzq5FyK7ydBoU1TLjFV459c8dTZMTu+LgsOTD11/V/Jr4NJxIudo" + "\r"
39. "MBQ3c4cHmOoYv4uzkQJBANR+7Fc3e6oZgqTOesqPSPqljbsdF9E4x4eDFuOecCkJ" + "\r"
40. "DvVLOOoAzvtHfAiUp+H3fk4hXRpALiNBEHiIdhIuX2UCQQDCCHiPHFd4gC58yyCM" + "\r"
41. "6Leqkmoa+6YpfRb3oxykLBXcWx7DtbX+ayKy5OQmnkEG+MW8XB8wAdiUl0/tb6cQ" + "\r"
42. "FaRvAkBhvP94Hk0DMDinFVHlWYJ3xy4pongSA8vCyMj+aSGtvjzjFnZXK4gIjBjA" + "\r"
43. "2Z9ekDfIOBBawqp2DLdGuX2VXz8BAkByMuIh+KBSv76cnEDwLhfLQJlKgEnvqTvX" + "\r"
44. "TB0TUw8avlaBAXW34/5sI+NUB1hmbgyTK/T/IFcEPXpBWLGO+e3pAkAGWLpnH0Zh" + "\r"
45. "Fae7oAqkMAd3xCNY6ec180tAe57hZ6kS+SYLKwb4gGzYaCxc22vMtYksXHtUeamo" + "\r"
46. "1NMLzI2ZfUoX" + "\r";    
47.     
48. /** 
49.      * 私钥 
50.      */
51. private
52.     
53. /** 
54.      * 公钥 
55.      */
56. private
57.         
58. /** 
59.      * 字节数据转字符串专用集合 
60.      */
61. private static final char[] HEX_CHAR= {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};    
62.         
63.     
64. /** 
65.      * 获取私钥 
66.      * @return 当前的私钥对象 
67.      */
68. public
69. return
70.     }    
71.     
72. /** 
73.      * 获取公钥 
74.      * @return 当前的公钥对象 
75.      */
76. public
77. return
78.     }    
79.     
80. /** 
81.      * 随机生成密钥对 
82.      */
83. public void
84. null;    
85. try
86. "RSA");    
87. catch
88.             e.printStackTrace();    
89.         }    
90. 1024, new
91.         KeyPair keyPair= keyPairGen.generateKeyPair();    
92. this.privateKey= (RSAPrivateKey) keyPair.getPrivate();    
93. this.publicKey= (RSAPublicKey) keyPair.getPublic();    
94.     }    
95.     
96. /** 
97.      * 从文件中输入流中加载公钥 
98.      * @param in 公钥输入流 
99.      * @throws Exception 加载公钥时产生的异常 
100.      */
101. public void loadPublicKey(InputStream in) throws
102. try
103. new BufferedReader(new
104. null;    
105. new
106. while((readLine= br.readLine())!=null){    
107. if(readLine.charAt(0)=='-'){    
108. continue;    
109. else{    
110.                     sb.append(readLine);    
111. '\r');    
112.                 }    
113.             }    
114.             loadPublicKey(sb.toString());    
115. catch
116. throw new Exception("公钥数据流读取错误");    
117. catch
118. throw new Exception("公钥输入流为空");    
119.         }    
120.     }    
121.     
122.     
123. /** 
124.      * 从字符串中加载公钥 
125.      * @param publicKeyStr 公钥数据字符串 
126.      * @throws Exception 加载公钥时产生的异常 
127.      */
128. public void loadPublicKey(String publicKeyStr) throws
129. try
130. new
131. byte[] buffer= base64Decoder.decodeBuffer(publicKeyStr);  
132. "RSA");    
133. new
134. this.publicKey= (RSAPublicKey) keyFactory.generatePublic(keySpec);    
135. catch
136. throw new Exception("无此算法");    
137. catch
138. throw new Exception("公钥非法");    
139. catch
140. throw new Exception("公钥数据内容读取错误");    
141. catch
142. throw new Exception("公钥数据为空");    
143.         }    
144.     }    
145.     
146. /** 
147.      * 从文件中加载私钥 
148.      * @param keyFileName 私钥文件名 
149.      * @return 是否成功 
150.      * @throws Exception  
151.      */
152. public void loadPrivateKey(InputStream in) throws
153. try
154. new BufferedReader(new
155. null;    
156. new
157. while((readLine= br.readLine())!=null){    
158. if(readLine.charAt(0)=='-'){    
159. continue;    
160. else{    
161.                     sb.append(readLine);    
162. '\r');    
163.                 }    
164.             }    
165.             loadPrivateKey(sb.toString());    
166. catch
167. throw new Exception("私钥数据读取错误");    
168. catch
169. throw new Exception("私钥输入流为空");    
170.         }    
171.     }    
172.     
173. public void loadPrivateKey(String privateKeyStr) throws
174. try
175. new
176. byte[] buffer= base64Decoder.decodeBuffer(privateKeyStr);    
177. new
178. "RSA");    
179. this.privateKey= (RSAPrivateKey) keyFactory.generatePrivate(keySpec);    
180. catch
181. throw new Exception("无此算法");    
182. catch
183.             e.printStackTrace();  
184. throw new Exception("私钥非法");    
185. catch
186. throw new Exception("私钥数据内容读取错误");    
187. catch
188. throw new Exception("私钥数据为空");    
189.         }    
190.     }    
191.     
192. /** 
193.      * 加密过程 
194.      * @param publicKey 公钥 
195.      * @param plainTextData 明文数据 
196.      * @return 
197.      * @throws Exception 加密过程中的异常信息 
198.      */
199. public byte[] encrypt(RSAPublicKey publicKey, byte[] plainTextData) throws
200. if(publicKey== null){    
201. throw new Exception("加密公钥为空, 请设置");    
202.         }    
203. null;    
204. try
205. "RSA");//, new BouncyCastleProvider());  
206.             cipher.init(Cipher.ENCRYPT_MODE, publicKey);    
207. byte[] output= cipher.doFinal(plainTextData);    
208. return
209. catch
210. throw new Exception("无此加密算法");    
211. catch
212.             e.printStackTrace();    
213. return null;    
214. catch
215. throw new Exception("加密公钥非法,请检查");    
216. catch
217. throw new Exception("明文长度非法");    
218. catch
219. throw new Exception("明文数据已损坏");    
220.         }    
221.     }    
222.     
223. /** 
224.      * 解密过程 
225.      * @param privateKey 私钥 
226.      * @param cipherData 密文数据 
227.      * @return 明文 
228.      * @throws Exception 解密过程中的异常信息 
229.      */
230. public byte[] decrypt(RSAPrivateKey privateKey, byte[] cipherData) throws
231. if (privateKey== null){    
232. throw new Exception("解密私钥为空, 请设置");    
233.         }    
234. null;    
235. try
236. "RSA");//, new BouncyCastleProvider());  
237.             cipher.init(Cipher.DECRYPT_MODE, privateKey);    
238. byte[] output= cipher.doFinal(cipherData);    
239. return
240. catch
241. throw new Exception("无此解密算法");    
242. catch
243.             e.printStackTrace();    
244. return null;    
245. catch
246. throw new Exception("解密私钥非法,请检查");    
247. catch
248. throw new Exception("密文长度非法");    
249. catch
250. throw new Exception("密文数据已损坏");    
251.         }           
252.     }    
253.     
254.