SecurityUtils.java 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package com.ym.utils;
  2. import org.apache.commons.lang.RandomStringUtils;
  3. import javax.crypto.*;
  4. import javax.crypto.spec.SecretKeySpec;
  5. import java.io.IOException;
  6. import java.io.UnsupportedEncodingException;
  7. import java.security.GeneralSecurityException;
  8. import java.security.InvalidKeyException;
  9. import java.security.InvalidParameterException;
  10. import java.security.NoSuchAlgorithmException;
  11. public class SecurityUtils {
  12. public static enum HmacAlgorithm {
  13. HMAC_MD5("HmacMD5"), HMAC_SHA1("HmacSHA1"), HMAC_SHA256("HmacSHA256"), HMAC_SHA384("HmacSHA384"), HMAC_SHA512(
  14. "HmacSHA512");
  15. private String algorithm;
  16. private HmacAlgorithm(String algorithm) {
  17. this.algorithm = algorithm;
  18. }
  19. public String getAlgorithm() {
  20. return algorithm;
  21. }
  22. }
  23. //AES加密方法
  24. public static final String AES_ECB_PKCS7 = "AES/ECB/PKCS7Padding";
  25. public static final String AES_ECB_PKCS5 = "AES/ECB/PKCS5Padding";
  26. public static final String AES_CBC_PKCS7 = "AES/CBC/PKCS7Padding";
  27. public static final String AES_CBC_PKCS5 = "AES/CBC/PKCS5Padding";
  28. public static byte[] encryptHMAC(String secret, String data, HmacAlgorithm algorithm) throws IOException {
  29. return encryptHMAC(secret.getBytes("utf-8"), data.getBytes("utf-8"), algorithm);
  30. }
  31. public static byte[] encryptHMAC(byte[] secret, byte[] data, HmacAlgorithm algorithm) throws IOException {
  32. byte[] bytes = null;
  33. try {
  34. SecretKey secretKey = new SecretKeySpec(secret, algorithm.getAlgorithm());
  35. Mac mac = Mac.getInstance(secretKey.getAlgorithm());
  36. mac.init(secretKey);
  37. bytes = mac.doFinal(data);
  38. } catch (GeneralSecurityException gse) {
  39. throw new IOException(gse.toString());
  40. }
  41. return bytes;
  42. }
  43. public static String byte2hex(byte[] bytes) {
  44. StringBuilder sign = new StringBuilder();
  45. for (byte aByte : bytes) {
  46. String hex = Integer.toHexString(aByte & 0xFF);
  47. if (hex.length() == 1) {
  48. sign.append("0");
  49. }
  50. sign.append(hex);
  51. }
  52. return sign.toString();
  53. }
  54. public static String generateSalt(int length) {
  55. return RandomStringUtils.random(length);
  56. }
  57. public static byte[] encryptWithAES128ECB(byte[] in, String key) throws UnsupportedEncodingException, NoSuchAlgorithmException,
  58. NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException {
  59. if (key == null || key.length() != 16) {
  60. throw new InvalidParameterException("key is invalid");
  61. }
  62. SecretKeySpec skeySpec = new SecretKeySpec(key.getBytes("utf-8"), "AES");
  63. Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
  64. cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
  65. return cipher.doFinal(in);
  66. }
  67. }