RSACoder.java 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. /*
  2. * FileName:RSACoder.java
  3. * <p>
  4. * Copyright (c) 2017-2020, <a href="http://www.webcsn.com">hermit (794890569@qq.com)</a>.
  5. * <p>
  6. * Licensed under the GNU General Public License, Version 3 (the "License");
  7. * you may not use this file except in compliance with the License.
  8. * You may obtain a copy of the License at
  9. * <p>
  10. * http://www.gnu.org/licenses/gpl-3.0.html
  11. * <p>
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. *
  18. */
  19. package cn.com.lzt.common.util.coder;
  20. import cn.com.lzt.common.util.security.SecurityCoder;
  21. import javax.crypto.Cipher;
  22. import java.security.*;
  23. import java.security.interfaces.RSAPrivateKey;
  24. import java.security.interfaces.RSAPublicKey;
  25. import java.security.spec.PKCS8EncodedKeySpec;
  26. import java.security.spec.X509EncodedKeySpec;
  27. import java.util.HashMap;
  28. import java.util.Map;
  29. /**
  30. * RSA安全编码组件
  31. *
  32. * @author ShenHuaJie
  33. * @version 1.0
  34. * @since 1.0
  35. */
  36. public abstract class RSACoder extends SecurityCoder {
  37. /**
  38. * 数字签名 密钥算法
  39. */
  40. public static final String KEY_ALGORITHM = "RSA";
  41. /**
  42. * 数字签名 签名/验证算法
  43. */
  44. public static final String SIGNATURE_ALGORITHM = "SHA1withRSA";
  45. /**
  46. * 公钥
  47. */
  48. private static final String PUBLIC_KEY = "RSAPublicKey";
  49. /**
  50. * 私钥
  51. */
  52. private static final String PRIVATE_KEY = "RSAPrivateKey";
  53. /**
  54. * RSA密钥长度 默认1024位, 密钥长度必须是64的倍数, 范围在512至65536位之间。
  55. */
  56. private static final int KEY_SIZE = 512;
  57. /**
  58. * 签名
  59. *
  60. * @param data 待签名数据
  61. * @param privateKey 私钥
  62. * @return byte[] 数字签名
  63. * @throws Exception
  64. */
  65. public static byte[] sign(byte[] data, byte[] privateKey) throws Exception {
  66. // 转换私钥材料
  67. PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(privateKey);
  68. // 实例化密钥工厂
  69. KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
  70. // 取私钥匙对象
  71. PrivateKey priKey = keyFactory.generatePrivate(pkcs8KeySpec);
  72. // 实例化Signature
  73. Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
  74. // 初始化Signature
  75. signature.initSign(priKey);
  76. // 更新
  77. signature.update(data);
  78. // 签名
  79. return signature.sign();
  80. }
  81. /**
  82. * 校验
  83. *
  84. * @param data 待校验数据
  85. * @param publicKey 公钥
  86. * @param sign 数字签名
  87. * @return boolean 校验成功返回true 失败返回false
  88. * @throws Exception
  89. */
  90. public static boolean verify(byte[] data, byte[] publicKey, byte[] sign) throws Exception {
  91. // 转换公钥材料
  92. X509EncodedKeySpec keySpec = new X509EncodedKeySpec(publicKey);
  93. // 实例化密钥工厂
  94. KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
  95. // 生成公钥
  96. PublicKey pubKey = keyFactory.generatePublic(keySpec);
  97. // 实例化Signature
  98. Signature signature = Signature.getInstance(SIGNATURE_ALGORITHM);
  99. // 初始化Signature
  100. signature.initVerify(pubKey);
  101. // 更新
  102. signature.update(data);
  103. // 验证
  104. return signature.verify(sign);
  105. }
  106. /**
  107. * 私钥解密
  108. *
  109. * @param data 待解密数据
  110. * @param key 私钥
  111. * @return byte[] 解密数据
  112. * @throws Exception
  113. */
  114. public static byte[] decryptByPrivateKey(byte[] data, byte[] key) throws Exception {
  115. // 取得私钥
  116. PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(key);
  117. KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
  118. // 生成私钥
  119. PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec);
  120. // 对数据解密
  121. Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
  122. cipher.init(Cipher.DECRYPT_MODE, privateKey);
  123. return cipher.doFinal(data);
  124. }
  125. /**
  126. * 公钥解密
  127. *
  128. * @param data 待解密数据
  129. * @param key 公钥
  130. * @return byte[] 解密数据
  131. * @throws Exception
  132. */
  133. public static byte[] decryptByPublicKey(byte[] data, byte[] key) throws Exception {
  134. // 取得公钥
  135. X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(key);
  136. KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
  137. // 生成公钥
  138. PublicKey publicKey = keyFactory.generatePublic(x509KeySpec);
  139. // 对数据解密
  140. Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
  141. cipher.init(Cipher.DECRYPT_MODE, publicKey);
  142. return cipher.doFinal(data);
  143. }
  144. /**
  145. * 公钥加密
  146. *
  147. * @param data 待加密数据
  148. * @param key 公钥
  149. * @return byte[] 加密数据
  150. * @throws Exception
  151. */
  152. public static byte[] encryptByPublicKey(byte[] data, byte[] key) throws Exception {
  153. // 取得公钥
  154. X509EncodedKeySpec x509KeySpec = new X509EncodedKeySpec(key);
  155. KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
  156. PublicKey publicKey = keyFactory.generatePublic(x509KeySpec);
  157. // 对数据加密
  158. Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
  159. cipher.init(Cipher.ENCRYPT_MODE, publicKey);
  160. return cipher.doFinal(data);
  161. }
  162. /**
  163. * 私钥加密
  164. *
  165. * @param data 待加密数据
  166. * @param key 私钥
  167. * @return byte[] 加密数据
  168. * @throws Exception
  169. */
  170. public static byte[] encryptByPrivateKey(byte[] data, byte[] key) throws Exception {
  171. // 取得私钥
  172. PKCS8EncodedKeySpec pkcs8KeySpec = new PKCS8EncodedKeySpec(key);
  173. KeyFactory keyFactory = KeyFactory.getInstance(KEY_ALGORITHM);
  174. // 生成私钥
  175. PrivateKey privateKey = keyFactory.generatePrivate(pkcs8KeySpec);
  176. // 对数据加密
  177. Cipher cipher = Cipher.getInstance(keyFactory.getAlgorithm());
  178. cipher.init(Cipher.ENCRYPT_MODE, privateKey);
  179. return cipher.doFinal(data);
  180. }
  181. /**
  182. * 取得私钥
  183. *
  184. * @param keyMap
  185. * @return
  186. * @throws Exception
  187. */
  188. public static byte[] getPrivateKey(Map<String, Object> keyMap) throws Exception {
  189. Key key = (Key) keyMap.get(PRIVATE_KEY);
  190. return key.getEncoded();
  191. }
  192. /**
  193. * 取得公钥
  194. *
  195. * @param keyMap
  196. * @return
  197. * @throws Exception
  198. */
  199. public static byte[] getPublicKey(Map<String, Object> keyMap) throws Exception {
  200. Key key = (Key) keyMap.get(PUBLIC_KEY);
  201. return key.getEncoded();
  202. }
  203. /**
  204. * 初始化密钥
  205. *
  206. * @return Map 密钥对儿 Map
  207. * @throws Exception
  208. */
  209. public static Map<String, Object> initKey() throws Exception {
  210. // 实例化密钥对儿生成器
  211. KeyPairGenerator keyPairGen = KeyPairGenerator.getInstance(KEY_ALGORITHM);
  212. // 初始化密钥对儿生成器
  213. keyPairGen.initialize(KEY_SIZE);
  214. // 生成密钥对儿
  215. KeyPair keyPair = keyPairGen.generateKeyPair();
  216. // 公钥
  217. RSAPublicKey publicKey = (RSAPublicKey) keyPair.getPublic();
  218. // 私钥
  219. RSAPrivateKey privateKey = (RSAPrivateKey) keyPair.getPrivate();
  220. // 封装密钥
  221. Map<String, Object> keyMap = new HashMap<String, Object>(2);
  222. keyMap.put(PUBLIC_KEY, publicKey);
  223. keyMap.put(PRIVATE_KEY, privateKey);
  224. return keyMap;
  225. }
  226. }