Guest User

Untitled

a guest
Nov 19th, 2017
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.73 KB | None | 0 0
  1. package com.sxd.yfl;
  2.  
  3. import android.util.Base64;
  4.  
  5. import java.io.UnsupportedEncodingException;
  6. import java.security.MessageDigest;
  7.  
  8. import javax.crypto.Cipher;
  9. import javax.crypto.spec.IvParameterSpec;
  10. import javax.crypto.spec.SecretKeySpec;
  11.  
  12. /**
  13. * Created by wujie on 2016/5/7.
  14. */
  15. public class ISecurity {
  16. public static final String APP_ID = Global.APP_ID;
  17. public static final String APP_KEY = Global.APP_KEY;
  18.  
  19. public static String encrypt(String clear, String key, String iv) {
  20. try {
  21. byte[] encrypted = encrypt(clear.getBytes(), md5(key), md5(iv));
  22. return Base64.encodeToString(encrypted, Base64.DEFAULT);
  23. } catch (Exception e) {
  24. e.printStackTrace();
  25. }
  26. return "";
  27. }
  28.  
  29. public static String decrypt(String encrypted, String key, String iv) {
  30. try {
  31. byte[] decrypted = decrypt(Base64.decode(encrypted, Base64.DEFAULT), md5(key), md5(iv));
  32. return new String(decrypted).trim();
  33. } catch (Exception e) {
  34. e.printStackTrace();
  35. }
  36. return "";
  37. }
  38.  
  39. private static byte[] encrypt(byte[] clear, byte[] key, byte[] iv) throws Exception {
  40. Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
  41. SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
  42. IvParameterSpec ivParamSpec = new IvParameterSpec(iv);
  43. cipher.init(Cipher.ENCRYPT_MODE, skeySpec, ivParamSpec);
  44. return cipher.doFinal(clear);
  45. }
  46.  
  47. private static byte[] decrypt(byte[] encrypted, byte[] key, byte[] iv) throws Exception {
  48. Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
  49. SecretKeySpec skeySpec = new SecretKeySpec(key, "AES");
  50. IvParameterSpec ivParamSpec = new IvParameterSpec(iv);
  51. cipher.init(Cipher.DECRYPT_MODE, skeySpec, ivParamSpec);
  52. return cipher.doFinal(encrypted);
  53. }
  54.  
  55. public static byte[] md5(String str) {
  56. try {
  57. return md5(str.getBytes("utf-8"));
  58. } catch (UnsupportedEncodingException e) {
  59. return md5(str.getBytes());
  60. }
  61. }
  62.  
  63. public static byte[] md5(byte[] bytes) {
  64. try {
  65. MessageDigest md5 = MessageDigest.getInstance("MD5");
  66. md5.update(bytes);
  67. return md5.digest();
  68. } catch (Exception e) {
  69. e.printStackTrace();
  70. }
  71. return bytes;
  72. }
  73.  
  74. public static String MD5(String str) {
  75. try {
  76. return MD5(str.getBytes("utf-8"));
  77. } catch (UnsupportedEncodingException e) {
  78. return MD5(str.getBytes());
  79. }
  80. }
  81.  
  82. private static String MD5(byte[] bytes) {
  83. try {
  84. MessageDigest md5 = MessageDigest.getInstance("MD5");
  85. md5.update(bytes);
  86. byte[] digested = md5.digest();
  87. char hexDigits[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  88. 'A', 'B', 'C', 'D', 'E', 'F'};
  89. // byte tmp[] = bytes;// MD5 的计算结果是一个 128 位的长整数,
  90. // 用字节表示就是 16 个字节
  91. char str[] = new char[16 * 2];// 每个字节用 16 进制表示的话,使用两个字符, 所以表示成 16
  92. // 进制需要 32 个字符
  93. int k = 0;// 表示转换结果中对应的字符位置
  94. for (int i = 0; i < 16; i++) {// 从第一个字节开始,对 MD5 的每一个字节// 转换成 16
  95. // 进制字符的转换
  96. byte byte0 = digested[i];// 取第 i 个字节
  97. str[k++] = hexDigits[byte0 >>> 4 & 0xf];// 取字节中高 4 位的数字转换,// >>>
  98. // 为逻辑右移,将符号位一起右移
  99. str[k++] = hexDigits[byte0 & 0xf];// 取字节中低 4 位的数字转换
  100.  
  101. }
  102. return new String(str);// 换后的结果转换为字符串
  103. } catch (Exception e) {
  104. e.printStackTrace();
  105. }
  106. return new String(bytes);
  107. }
  108.  
  109. public static String sha1(byte[] bytes) {
  110. try {
  111. MessageDigest md5 = MessageDigest.getInstance("SHA-1");
  112. md5.update(bytes);
  113. byte[] digested = md5.digest();
  114. return hexDigits(digested, digested.length);
  115. } catch (Exception e) {
  116. e.printStackTrace();
  117. }
  118. return new String(bytes);
  119. }
  120.  
  121. private static String hexDigits(byte[] bytes, int length) {
  122. char hexChars[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
  123. 'a', 'b', 'c', 'd', 'e', 'f'};
  124. char str[] = new char[length * 2];
  125. int k = 0;
  126. for (int i = 0; i < length; i++) {
  127. byte byte0 = bytes[i];
  128. str[k++] = hexChars[byte0 >>> 4 & 0xf];
  129. str[k++] = hexChars[byte0 & 0xf];
  130. }
  131. return new String(str);
  132. }
  133. }
Add Comment
Please, Sign In to add comment