java 几种加密 解密方法。

  1. import java.security.MessageDigest;
  2. import javax.crypto.Cipher;
  3. import javax.crypto.SecretKey;
  4. import javax.crypto.spec.SecretKeySpec;
  5. import sun.misc.BASE64Decoder;
  6. import sun.misc.BASE64Encoder;
  7. /**
  8. * Java 加解密工具类
  9. *
  10. * @author systemcookie@gmail.com
  11. *
  12. */
  13. public class EncryptUtil {
  14. private static final String UTF8 = "utf-8";
  15. //定义 加密算法,可用 DES,DESede,Blowfish
  16. private static final String ALGORITHM_DESEDE = "DESede";
  17. /**
  18. * MD5数字签名
  19. *
  20. * @param src
  21. * @return
  22. * @throws Exception
  23. */
  24. public String md5Digest(String src) throws Exception {
  25. // 定义数字签名方法, 可用:MD5, SHA-1
  26. MessageDigest md = MessageDigest.getInstance("MD5");
  27. byte[] b = md.digest(src.getBytes(UTF8));
  28. return this.byte2HexStr(b);
  29. }
  30. /**
  31. * BASE64 加密
  32. *
  33. * @param src
  34. * @return
  35. * @throws Exception
  36. */
  37. public String base64Encoder(String src) throws Exception {
  38. BASE64Encoder encoder = new BASE64Encoder();
  39. return encoder.encode(src.getBytes(UTF8));
  40. }
  41. /**
  42. * BASE64解密
  43. *
  44. * @param dest
  45. * @return
  46. * @throws Exception
  47. */
  48. public String base64Decoder(String dest) throws Exception {
  49. BASE64Decoder decoder = new BASE64Decoder();
  50. return new String(decoder.decodeBuffer(dest), UTF8);
  51. }
  52. /**
  53. * 3DES加密
  54. *
  55. * @param src
  56. * @param key
  57. * @return
  58. * @throws Exception
  59. */
  60. public String desedeEncoder(String src, String key) throws Exception {
  61. SecretKey secretKey = new SecretKeySpec(build3DesKey(key), ALGORITHM_DESEDE);
  62. Cipher cipher = Cipher.getInstance(ALGORITHM_DESEDE);
  63. cipher.init(Cipher.ENCRYPT_MODE, secretKey);
  64. byte[] b = cipher.doFinal(src.getBytes(UTF8));
  65. return byte2HexStr(b);
  66. }
  67. /**
  68. * 3DES解密
  69. *
  70. * @param dest
  71. * @param key
  72. * @return
  73. * @throws Exception
  74. */
  75. public String desedeDecoder(String dest, String key) throws Exception {
  76. SecretKey secretKey = new SecretKeySpec(build3DesKey(key), ALGORITHM_DESEDE);
  77. Cipher cipher = Cipher.getInstance(ALGORITHM_DESEDE);
  78. cipher.init(Cipher.DECRYPT_MODE, secretKey);
  79. byte[] b = cipher.doFinal(str2ByteArray(dest));
  80. return new String(b, UTF8);
  81. }
  82. /**
  83. * 字节数组转化为大写16进制字符串
  84. *
  85. * @param b
  86. * @return
  87. */
  88. private String byte2HexStr(byte[] b) {
  89. StringBuilder sb = new StringBuilder();
  90. for (int i = 0; i < b.length; i++) {
  91. String s = Integer.toHexString(b[i] & 0xFF);
  92. if (s.length() == 1) {
  93. sb.append("0");
  94. }
  95. sb.append(s.toUpperCase());
  96. }
  97. return sb.toString();
  98. }
  99. /**
  100. * 字符串转字节数组
  101. *
  102. * @param s
  103. * @return
  104. */
  105. private byte[] str2ByteArray(String s) {
  106. int byteArrayLength = s.length()/2;
  107. byte[] b = new byte[byteArrayLength];
  108. for (int i = 0; i < byteArrayLength; i++) {
  109. byte b0 = (byte) Integer.valueOf(s.substring(i*2, i*2+2), 16).intValue();
  110. b[i] = b0;
  111. }
  112. return b;
  113. }
  114. /**
  115. * 构造3DES加解密方法key
  116. *
  117. * @param keyStr
  118. * @return
  119. * @throws Exception
  120. */
  121. private byte[] build3DesKey(String keyStr) throws Exception {
  122. byte[] key = new byte[24];
  123. byte[] temp = keyStr.getBytes(UTF8);
  124. if (key.length > temp.length) {
  125. System.arraycopy(temp, 0, key, 0, temp.length);
  126. } else {
  127. System.arraycopy(temp, 0, key, 0, key.length);
  128. }
  129. return key;
  130. }
  131. }

转载于:https://my.oschina.net/u/175660/blog/62852

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值