Java加密解密快速入门下篇【包括MD5、BASE64、DES、RSA等算法】_JAVA_编程开发_程序员俱乐部

中国优秀的程序员网站程序员频道CXYCLUB技术地图
热搜:
更多>>
 
您所在的位置: 程序员俱乐部 > 编程开发 > JAVA > Java加密解密快速入门下篇【包括MD5、BASE64、DES、RSA等算法】

Java加密解密快速入门下篇【包括MD5、BASE64、DES、RSA等算法】

 2014/8/14 13:01:04  Hello_Nick_Xu  程序员俱乐部  我要评论(0)
  • 摘要:在上一篇博客中已经简要的介绍了MD5、BASE64、DES、RSA等算法在Java中的具体应用。现在可以考虑对这些代码封装成一个工具类EncryptUtil,然后再补充一下CommonsCodec对BASE64的扩展支持!<一>.EncryptUtil工具类:1.使用commons-logging记录异常日志。2.提取常量字段、公共字段。3.提取公共方法:Java代码//创建密钥createSecretKey(Stringkey):Key//加密解密processCipher
  • 标签:Java ASE 算法 加密解密

在上一篇博客中已经简要的介绍了MD5、BASE64、DES、RSA等算法在Java中的具体应用。现在可以考虑对这些代码封装成一个工具类EncryptUtil吻,然后再补充一下Commons Codec对BASE64的扩展支持!微笑

?

<一>. EncryptUtil工具类:

?1. 使用commons-logging记录异常日志。

?2. 提取常量字段、公共字段。

?3. 提取公共方法:?

Java代码?
    class="dp-j" style="margin-bottom: 1px; padding-top: 2px; padding-bottom: 2px; border: 1px solid #d1d7dc; color: #2b91af;">
  1. //创建密钥??
  2. createSecretKey(String?key):Key???
  3. //加密解密??
  4. processCipher(byte[]?processData,?Key?key,?int?opsMode,?String?algorithm):byte[]??

? 4.?EncryptUtil类的完整代码:大笑?

Java代码?
  1. /*?
  2. ?*?Copyright?(c)?2014,?Nick?Xu,?All?rights?reserved.?
  3. ?*/??
  4. package?com.excelsoft.common.crypto;??
  5. ??
  6. import?java.io.IOException;??
  7. import?java.security.Key;??
  8. import?java.security.KeyPair;??
  9. import?java.security.KeyPairGenerator;??
  10. import?java.security.MessageDigest;??
  11. import?java.security.NoSuchAlgorithmException;??
  12. import?java.security.PrivateKey;??
  13. import?java.security.PublicKey;??
  14. import?java.security.SecureRandom;??
  15. import?java.security.Signature;??
  16. ??
  17. import?javax.crypto.Cipher;??
  18. import?javax.crypto.SecretKey;??
  19. import?javax.crypto.SecretKeyFactory;??
  20. import?javax.crypto.spec.DESKeySpec;??
  21. ??
  22. import?org.apache.commons.logging.Log;??
  23. import?org.apache.commons.logging.LogFactory;??
  24. ??
  25. import?sun.misc.BASE64Decoder;??
  26. import?sun.misc.BASE64Encoder;??
  27. ??
  28. /**?
  29. ?*?功能简述:?加密解密工具类,对MD5/BASE64/DES/RSA等算法提供了包装.?
  30. ?*?@author?Nick?Xu?
  31. ?*?@version?1.0?
  32. ?*/??
  33. public?class?EncryptUtil?{??
  34. ????private?static?Log?logger?=?LogFactory.getLog(EncryptUtil.class);??
  35. ??????
  36. ????private?static?final?int?KEY_SIZE?=?1024;??
  37. ????private?static?final?String??MD5_ALGORITHM=?"md5";??
  38. ????private?static?final?String??DES_ALGORITHM=?"des";??
  39. ????private?static?final?String??RSA_ALGORITHM=?"rsa";??
  40. ????private?static?final?String??SIGNATURE_ALGORITHM=?"MD5withRSA";??
  41. ??????
  42. ????private?static?MessageDigest?md5;??
  43. ????private?static?BASE64Encoder?encoder;??
  44. ????private?static?BASE64Decoder?decoder;??
  45. ????private?static?SecureRandom?random;??
  46. ????private?static?KeyPair?keyPair;??
  47. ??????
  48. ????private?EncryptUtil()?{??
  49. ????}??
  50. ??????
  51. ????static?{??
  52. ????????try?{??
  53. ????????????md5?=?MessageDigest.getInstance(MD5_ALGORITHM);??
  54. ??????????????
  55. ????????????KeyPairGenerator?keyPairGenerator?=?KeyPairGenerator.getInstance(RSA_ALGORITHM);??
  56. ????????????keyPairGenerator.initialize(KEY_SIZE);??
  57. ????????????keyPair?=?keyPairGenerator.generateKeyPair();??
  58. ????????}??
  59. ????????catch?(NoSuchAlgorithmException?e)?{??
  60. ????????????//?Exception?handler??
  61. ????????????logger.error(e);??
  62. ????????}??
  63. ????????encoder?=?new?BASE64Encoder();??
  64. ????????decoder?=?new?BASE64Decoder();??
  65. ????????random?=?new?SecureRandom();??
  66. ????}??
  67. ??????
  68. ????/**?
  69. ?????*?功能简述:?使用md5进行单向加密.?
  70. ?????*/??
  71. ????public?static?String?encryptMD5(String?plainText)?{??
  72. ????????byte[]?cipherData?=?md5.digest(plainText.getBytes());??
  73. ????????StringBuilder?builder?=?new?StringBuilder();??
  74. ????????for(byte?cipher?:?cipherData)?{??
  75. ????????????String?toHexStr?=?Integer.toHexString(cipher?&?0xff);??
  76. ????????????builder.append(toHexStr.length()?==?1???"0"?+?toHexStr?:?toHexStr);??
  77. ????????}??
  78. ????????return?builder.toString();??
  79. ????}??
  80. ??????
  81. ????/**?
  82. ?????*?功能简述:?使用BASE64进行加密.?
  83. ?????*?@param?plainData?明文数据?
  84. ?????*?@return?加密之后的文本内容?
  85. ?????*/??
  86. ????public?static?String?encryptBASE64(byte[]?plainData)?{??
  87. ????????return?encoder.encode(plainData);??
  88. ????}??
  89. ??????
  90. ????/**?
  91. ?????*?功能简述:?使用BASE64进行解密.?
  92. ?????*?@param?cipherText?密文文本?
  93. ?????*?@return?解密之后的数据?
  94. ?????*/??
  95. ????public?static?byte[]?decryptBASE64(String?cipherText)?{??
  96. ????????byte[]?plainData?=?null;??
  97. ????????try?{??
  98. ????????????plainData?=??decoder.decodeBuffer(cipherText);??
  99. ????????}??
  100. ????????catch?(IOException?e)?{??
  101. ????????????//?Exception?handler??
  102. ????????????logger.error(e);??
  103. ????????}??
  104. ????????return?plainData;??
  105. ????}??
  106. ??????
  107. ????/**?
  108. ?????*?功能简述:?使用DES算法进行加密.?
  109. ?????*?@param?plainData?明文数据?
  110. ?????*?@param?key???加密密钥?
  111. ?????*?@return???
  112. ?????*/??
  113. ????public?static?byte[]?encryptDES(byte[]?plainData,?String?key)?{??
  114. ????????return?processCipher(plainData,?createSecretKey(key),?Cipher.ENCRYPT_MODE,?DES_ALGORITHM);??
  115. ????}??
  116. ??????
  117. ????/**?
  118. ?????*?功能简述:?使用DES算法进行解密.?
  119. ?????*?@param?cipherData????密文数据?
  120. ?????*?@param?key???解密密钥?
  121. ?????*?@return?
  122. ?????*/??
  123. ????public?static?byte[]?decryptDES(byte[]?cipherData,?String?key)?{??
  124. ????????return?processCipher(cipherData,?createSecretKey(key),?Cipher.DECRYPT_MODE,?DES_ALGORITHM);??
  125. ????}??
  126. ??????
  127. ????/**?
  128. ?????*?功能简述:?根据key创建密钥SecretKey.?
  129. ?????*?@param?key??
  130. ?????*?@return?
  131. ?????*/??
  132. ????private?static?SecretKey?createSecretKey(String?key)?{??
  133. ????????SecretKey?secretKey?=?null;??
  134. ????????try?{??
  135. ????????????DESKeySpec?keySpec?=?new?DESKeySpec(key.getBytes());??
  136. ????????????SecretKeyFactory?keyFactory?=?SecretKeyFactory.getInstance(DES_ALGORITHM);??
  137. ????????????secretKey?=?keyFactory.generateSecret(keySpec);??
  138. ????????}??
  139. ????????catch?(Exception?e)?{??
  140. ????????????//?Exception?handler??
  141. ????????????logger.error(e);??
  142. ????????}??
  143. ????????return?secretKey;??
  144. ????}??
  145. ??????
  146. ????/**?
  147. ?????*?功能简述:?加密/解密处理流程.?
  148. ?????*?@param?processData???待处理的数据?
  149. ?????*?@param?key???提供的密钥?
  150. ?????*?@param?opsMode???工作模式?
  151. ?????*?@param?algorithm???使用的算法?
  152. ?????*?@return???
  153. ?????*/??
  154. ????private?static?byte[]?processCipher(byte[]?processData,?Key?key,?int?opsMode,?String?algorithm)?{??
  155. ????????try{???
  156. ????????????Cipher?cipher?=?Cipher.getInstance(algorithm);??
  157. ????????????cipher.init(opsMode,?key,?random);??
  158. ????????????return?cipher.doFinal(processData);??
  159. ????????}??
  160. ????????catch?(Exception?e)?{??
  161. ????????????//?Exception?handler??
  162. ????????????logger.error(e);??
  163. ????????}??
  164. ????????return?null;??
  165. ????}??
  166. ??????
  167. ????/**?
  168. ?????*?功能简述:?创建私钥,用于RSA非对称加密.?
  169. ?????*?@return?
  170. ?????*/??
  171. ????public?static?PrivateKey?createPrivateKey()?{??
  172. ????????return?keyPair.getPrivate();??
  173. ????}??
  174. ??????
  175. ????/**?
  176. ?????*?功能简述:?创建公钥,用于RSA非对称加密.?
  177. ?????*?@return?
  178. ?????*/??
  179. ????public?static?PublicKey?createPublicKey()?{??
  180. ????????return?keyPair.getPublic();??
  181. ????}??
  182. ??????
  183. ????/**?
  184. ?????*?功能简述:?使用RSA算法加密.?
  185. ?????*?@param?plainData?明文数据?
  186. ?????*?@param?key???密钥?
  187. ?????*?@return?
  188. ?????*/??
  189. ????public?static?byte[]?encryptRSA(byte[]?plainData,?Key?key)?{??
  190. ????????return?processCipher(plainData,?key,?Cipher.ENCRYPT_MODE,?RSA_ALGORITHM);??
  191. ????}??
  192. ??????
  193. ????/**?
  194. ?????*?功能简述:?使用RSA算法解密.?
  195. ?????*?@param?cipherData????密文数据?
  196. ?????*?@param?key???密钥?
  197. ?????*?@return?
  198. ?????*/??
  199. ????public?static?byte[]?decryptRSA(byte[]?cipherData,?Key?key)?{??
  200. ????????return?processCipher(cipherData,?key,?Cipher.DECRYPT_MODE,?RSA_ALGORITHM);??
  201. ????}??
  202. ??????
  203. ????/**?
  204. ?????*?功能简述:?使用私钥对加密数据创建数字签名.?
  205. ?????*?@param?cipherData?????已经加密过的数据?
  206. ?????*?@param?privateKey????私钥?
  207. ?????*?@return?
  208. ?????*/??
  209. ????public?static?byte[]?createSignature(byte[]?cipherData,?PrivateKey?privateKey)?{??
  210. ????????try?{??
  211. ????????????Signature?signature??=?Signature.getInstance(SIGNATURE_ALGORITHM);??
  212. ????????????signature.initSign(privateKey);??
  213. ????????????signature.update(cipherData);??
  214. ????????????return?signature.sign();??
  215. ????????}??
  216. ????????catch?(Exception?e)?{??
  217. ????????????//?Exception?handler??
  218. ????????????logger.error(e);???
  219. ????????}??
  220. ????????return?null;??
  221. ????}??
  222. ??????
  223. ????/**?
  224. ?????*?功能简述:?使用公钥对数字签名进行验证.?
  225. ?????*?@param?signData??数字签名?
  226. ?????*?@param?publicKey?公钥?
  227. ?????*?@return?
  228. ?????*/??
  229. ????public?static?boolean?verifySignature(byte[]?cipherData,?byte[]?signData,?PublicKey?publicKey)?{??
  230. ????????try?{??
  231. ????????????Signature?signature??=?Signature.getInstance(SIGNATURE_ALGORITHM);??
  232. ????????????signature.initVerify(publicKey);??
  233. ????????????signature.update(cipherData);??
  234. ????????????return?signature.verify(signData);??
  235. ????????}??
  236. ????????catch?(Exception?e)?{??
  237. ????????????//?Exception?handler??
  238. ????????????logger.error(e);??
  239. ????????}??
  240. ????????return?false;??
  241. ????}??
  242. }??

?

<二>. Commons Codec对BASE64的扩展支持:

? ? ? JDK提供了对BASE64的标准支持,每隔76个字符进行换行\r\n,并且包含+、=、/等特殊字符不适合作为url参数传递。因此通常都会使用Commons Codec来进行BASE64的加密和解密。下载commons-codec-1.8.jar并添加到lib下面,注意选择高版本,低版本有些方法不支持。天真

?1.?是否换行:

Java代码?
  1. byte[]?cipherData?=?Base64.encodeBase64(plainText.getBytes());?//默认不换行????
  2. byte[]?cipherData?=?Base64.encodeBase64(plainText.getBytes(),?false);?//取消换行??
  3. byte[]?cipherData?=?Base64.encodeBase64Chunked(plainText.getBytes());?//进行换行??
  4. String?cipherText?=?Base64.encodeBase64String(plainText.getBytes());?//转为字符串??

? 2.?安全的url:转换+为-、/为_、将多余的=去掉

Java代码?
  1. byte[]?cipherData?=?Base64.encodeBase64(plainText.getBytes(),?false,?true);??
  2. byte[]?cipherData?=?Base64.encodeBase64URLSafe(plainText.getBytes());??
  3. String?cipherText?=?Base64.encodeBase64URLSafeString(plainText.getBytes());??

?

上一篇: 金山快盘授权API开发 下一篇: 没有下一篇了!
  • 相关文章
发表评论
用户名: 匿名