在路上

 找回密码
 立即注册
在路上 站点首页 学习 查看内容

常用加解密工具类(MD5、SHA、DES、AES、RSA)

2016-12-20 13:15| 发布者: zhangjf| 查看: 455| 评论: 0

摘要: 加解密工具类,实现了常用的加解密类。包括单向加密:MD5、SHA;对称加密:DES、AES;非对称加密:RSA 完整代码见:https://git.oschina.net/bayern.com/SecureUtils.git 同时提供ant打包脚本。 下面 ...
加解密工具类,实现了常用的加解密类。包括单向加密:MD5、SHA;对称加密:DES、AES;非对称加密:RSA
完整代码见:https://git.oschina.net/bayern.com/SecureUtils.git 同时提供ant打包脚本。

下面展示部分关键代码
  1. MD5 单向加密:
  2. /**
  3. * 返回MD5单向加密后的十六进制字符串
  4. * @param data
  5. * @return
  6. * @throws Exception
  7. */
  8. public String getEncryptForHex(byte[] data) throws Exception
  9. {
  10. byte[] digestData = encrypt(data);
  11. StringBuffer hex = new StringBuffer();
  12. for(int i = 0; i < digestData.length; i++)
  13. {
  14. int h = ((int)digestData[i]) & 0XFF;
  15. if(h < 16)
  16. {
  17. hex.append("0");
  18. }
  19. hex.append(Integer.toHexString(h));
  20. }
  21. return hex.toString();
  22. }
  23. DES 对称加密类:
  24. @Override
  25. public byte[] encrypt(byte[] data) throws Exception
  26. {
  27. if(secretKey == null || "".equals(secretKey))
  28. {
  29. throw new Exception("scretKey need to exists");
  30. }
  31. SecretKey md5Key = getKey(secretKey);
  32. Cipher cipher = Cipher.getInstance(ALGORITHM);
  33. cipher.init(Cipher.ENCRYPT_MODE, md5Key);
  34. return cipher.doFinal(data);
  35. }
  36. @Override
  37. public byte[] decrypt(byte[] data) throws Exception
  38. {
  39. if(secretKey == null || "".equals(secretKey))
  40. {
  41. throw new Exception("scretKey need to exists");
  42. }
  43. SecretKey md5Key = getKey(secretKey);
  44. Cipher cipher = Cipher.getInstance(ALGORITHM);
  45. cipher.init(Cipher.DECRYPT_MODE, md5Key);
  46. return cipher.doFinal(data);
  47. }
  48. RSA 非对称加密。私钥加密 & 私钥解密 & 私钥签名
  49. @Override
  50. public byte[] encrypt(byte[] data) throws Exception
  51. {
  52. PrivateKey rsaPrivateKey = getRSAPrivateKey();
  53. Cipher cipher = Cipher.getInstance(ALGORITHM);
  54. cipher.init(Cipher.ENCRYPT_MODE, rsaPrivateKey);
  55. return cipher.doFinal(data);
  56. }
  57. @Override
  58. public byte[] decrypt(byte[] data) throws Exception
  59. {
  60. PrivateKey rsaPrivateKey = getRSAPrivateKey();
  61. Cipher cipher = Cipher.getInstance(ALGORITHM);
  62. cipher.init(Cipher.DECRYPT_MODE, rsaPrivateKey);
  63. return cipher.update(data);
  64. }
  65. /**
  66. * 使用私钥 对数据进行签名
  67. * @param data
  68. * @return
  69. * @throws Exception
  70. */
  71. public String sign(byte[] data) throws Exception
  72. {
  73. PrivateKey rsaPrivateKey = getRSAPrivateKey();
  74. Signature signature = Signature.getInstance(SIGN_ALGORITHM);
  75. signature.initSign(rsaPrivateKey);
  76. signature.update(data);
  77. return encoder(signature.sign());
  78. }
  79. RSA 非对称加密。公钥加密 & 公钥解密 & 公钥校验签名
  80. @Override
  81. public byte[] encrypt(byte[] data) throws Exception
  82. {
  83. if(publicKey == null || "".equals(publicKey))
  84. {
  85. throw new Exception("publicKey is need exists");
  86. }
  87. PublicKey rsaPublicKey = getRSAPublicKey(publicKey);
  88. Cipher cipher = Cipher.getInstance(ALGORITHM);
  89. cipher.init(Cipher.ENCRYPT_MODE, rsaPublicKey);
  90. return cipher.doFinal(data);
  91. }
  92. @Override
  93. public byte[] decrypt(byte[] data) throws Exception
  94. {
  95. if(publicKey == null || "".equals(publicKey))
  96. {
  97. throw new Exception("publicKey is need exists");
  98. }
  99. PublicKey rsaPublicKey = getRSAPublicKey(publicKey);
  100. Cipher cipher = Cipher.getInstance(ALGORITHM);
  101. cipher.init(Cipher.DECRYPT_MODE, rsaPublicKey);
  102. return cipher.doFinal(data);
  103. }
  104. /**
  105. * 使用公钥校验签名
  106. * @param data
  107. * @param sign
  108. * @return
  109. * @throws Exception
  110. */
  111. public boolean verifySign(byte[] data, String sign) throws Exception
  112. {
  113. if(publicKey == null || "".equals(publicKey))
  114. {
  115. throw new Exception("publicKey is need exists");
  116. }
  117. PublicKey rsaPublicKey = getRSAPublicKey(publicKey);
  118. Signature signature = Signature.getInstance(SIGN_ALGORITHM);
  119. signature.initVerify(rsaPublicKey);
  120. signature.update(data);
  121. return signature.verify(decoder(sign));
  122. }
复制代码

最新评论

小黑屋|在路上 ( 蜀ICP备15035742号-1 

;

GMT+8, 2025-7-8 10:17

Copyright 2015-2025 djqfx

返回顶部