AES256Util.java 3.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. package com.its.utils;
  2. import javax.crypto.BadPaddingException;
  3. import javax.crypto.Cipher;
  4. import javax.crypto.IllegalBlockSizeException;
  5. import javax.crypto.NoSuchPaddingException;
  6. import javax.crypto.spec.IvParameterSpec;
  7. import javax.crypto.spec.SecretKeySpec;
  8. import java.io.UnsupportedEncodingException;
  9. import java.security.*;
  10. public class AES256Util {
  11. private String iv;
  12. private Key keySpec;
  13. public static String DEFAULT_KEY = "qjeockd!csp#qs)x";
  14. public AES256Util(String key) throws UnsupportedEncodingException {
  15. this.iv = key.substring(0, 16);
  16. byte[] keyBytes = new byte[16];
  17. byte[] b = key.getBytes("UTF-8");
  18. int len = b.length;
  19. if (len > keyBytes.length) {
  20. len = keyBytes.length;
  21. }
  22. System.arraycopy(b, 0, keyBytes, 0, len);
  23. SecretKeySpec keySpec = new SecretKeySpec(keyBytes, "AES");
  24. this.keySpec = keySpec;
  25. }
  26. // public String aesEncode2(String str) throws UnsupportedEncodingException, NoSuchAlgorithmException,
  27. // NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException,
  28. // IllegalBlockSizeException, BadPaddingException{
  29. // Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
  30. // c.init(Cipher.ENCRYPT_MODE, keySpec, new IvParameterSpec(iv.getBytes()));
  31. // byte[] encrypted = c.doFinal(str.getBytes("UTF-8"));
  32. // return new String(Base64.encodeBase64(encrypted));
  33. // }
  34. public byte[] aesEncode(String str) throws UnsupportedEncodingException, NoSuchAlgorithmException,
  35. NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException,
  36. IllegalBlockSizeException, BadPaddingException{
  37. Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
  38. c.init(Cipher.ENCRYPT_MODE, keySpec, new IvParameterSpec(iv.getBytes()));
  39. byte[] encrypted = c.doFinal(str.getBytes("UTF-8"));
  40. return encrypted;
  41. }
  42. // public String aesDecode(String str) throws UnsupportedEncodingException, NoSuchAlgorithmException,
  43. // NoSuchPaddingException, InvalidKeyException, InvalidAlgorithmParameterException,
  44. // IllegalBlockSizeException, BadPaddingException {
  45. // Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
  46. // c.init(Cipher.DECRYPT_MODE, keySpec, new IvParameterSpec(iv.getBytes("UTF-8")));
  47. // byte[] byteStr = Base64.decodeBase64(str.getBytes());
  48. // return new String(c.doFinal(byteStr),"UTF-8");
  49. // }
  50. public static String encryptSHA256(String str) {
  51. String SHA = "";
  52. try {
  53. MessageDigest msgDigest = MessageDigest.getInstance("SHA-256");
  54. msgDigest.update(str.getBytes());
  55. byte byteData[] = msgDigest.digest();
  56. StringBuffer builder = new StringBuffer();
  57. for (byte b : byteData) {
  58. builder.append(String.format("%02x", b));
  59. }
  60. // for(int i=0; i < byteData.length; i++) {
  61. // builder.append(Integer.toString((byteData[i]&0xff) + 0x100, 16).substring(1));
  62. // }
  63. SHA = builder.toString();
  64. } catch (NoSuchAlgorithmException e) {
  65. SHA = "";
  66. }
  67. return SHA;
  68. }
  69. }