fork(2) download
  1. import java.security.Key;
  2.  
  3. import javax.crypto.Cipher;
  4.  
  5. /**
  6.  * DES加密和解密工具,可以对字符串进行加密和解密操作 。
  7.  * 该加密工具适用于当前主流的"android锁机软件"的DES加解密
  8.  * @author 1595901624
  9.  * <p>
  10.  * 2016-10-01
  11.  * </p>
  12.  */
  13. public class DesUtils {
  14.  
  15. /** 字符串默认键值 */
  16. private static String strDefaultKey = "national";
  17.  
  18. /** 加密工具 */
  19. private Cipher encryptCipher = null;
  20.  
  21. /** 解密工具 */
  22. private Cipher decryptCipher = null;
  23.  
  24. /**
  25. * 将byte数组转换为表示16进制值的字符串, 如:byte[]{8,18}转换为:0813, 和public static byte[]
  26. * hexStr2ByteArr(String str) 互为可逆的转换过程
  27. *
  28. * @param arrB
  29. * 需要转换的byte数组
  30. * @return 转换后的字符串
  31. * @throws Exception
  32. * 本方法不处理任何异常,所有异常全部抛出
  33. */
  34. public static String byteArr2HexStr(byte[] arrB) throws Exception {
  35. int iLen = arrB.length;
  36. // 每个byte用两个字符才能表示,所以字符串的长度是数组长度的两倍
  37. StringBuffer sb = new StringBuffer(iLen * 2);
  38. for (int i = 0; i < iLen; i++) {
  39. int intTmp = arrB[i];
  40. // 把负数转换为正数
  41. while (intTmp < 0) {
  42. intTmp = intTmp + 256;
  43. }
  44. // 小于0F的数需要在前面补0
  45. if (intTmp < 16) {
  46. sb.append("0");
  47. }
  48. sb.append(Integer.toString(intTmp, 16));
  49. }
  50. return sb.toString();
  51. }
  52.  
  53. /**
  54. * 将表示16进制值的字符串转换为byte数组, 和public static String byteArr2HexStr(byte[] arrB)
  55. * 互为可逆的转换过程
  56. *
  57. * @param str
  58. * 需要转换的字符串
  59. * @return 转换后的byte数组
  60. * @throws Exception
  61. * 本方法不处理任何异常,所有异常全部抛出
  62. * @author 1595901624
  63. */
  64. public static byte[] hexStr2ByteArr(String str) throws Exception {
  65. byte[] arrB = str.getBytes();
  66. int iLen = arrB.length;
  67.  
  68. // 两个字符表示一个字节,所以字节数组长度是字符串长度除以2
  69. byte[] arrOut = new byte[iLen / 2];
  70. for (int i = 0; i < iLen; i = i + 2) {
  71. String strTmp = new String(arrB, i, 2);
  72. arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16);
  73. }
  74. return arrOut;
  75. }
  76.  
  77. /**
  78. * 默认构造方法,使用默认密钥
  79. *
  80. * @throws Exception
  81. */
  82. public DesUtils() throws Exception {
  83. this(strDefaultKey);
  84. }
  85.  
  86. /**
  87. * 指定密钥构造方法
  88. *
  89. * @param strKey
  90. * 指定的密钥
  91. * @throws Exception
  92. */
  93. public DesUtils(String strKey) throws Exception {
  94. Key key = getKey(strKey.getBytes());
  95.  
  96. encryptCipher = Cipher.getInstance("DES");
  97. encryptCipher.init(Cipher.ENCRYPT_MODE, key);
  98.  
  99. decryptCipher = Cipher.getInstance("DES");
  100. decryptCipher.init(Cipher.DECRYPT_MODE, key);
  101. }
  102.  
  103. /**
  104. * 加密字节数组
  105. *
  106. * @param arrB
  107. * 需加密的字节数组
  108. * @return 加密后的字节数组
  109. * @throws Exception
  110. */
  111. public byte[] encrypt(byte[] arrB) throws Exception {
  112. return encryptCipher.doFinal(arrB);
  113. }
  114.  
  115. /**
  116. * 加密字符串
  117. *
  118. * @param str
  119. * 需加密的字符串
  120. * @return 加密后的字符串
  121. * @throws Exception
  122. */
  123. public String encrypt(String str) throws Exception {
  124. return byteArr2HexStr(encrypt(str.getBytes()));
  125. }
  126.  
  127. /**
  128. * 解密字节数组
  129. *
  130. * @param arrB
  131. * 需解密的字节数组
  132. * @return 解密后的字节数组
  133. * @throws Exception
  134. */
  135. public byte[] decrypt(byte[] arrB) throws Exception {
  136. return decryptCipher.doFinal(arrB);
  137. }
  138.  
  139. /**
  140. * 解密字符串
  141. *
  142. * @param str
  143. * 需解密的字符串
  144. * @return 解密后的字符串
  145. * @throws Exception
  146. */
  147. public String decrypt(String str) throws Exception {
  148. return new String(decrypt(hexStr2ByteArr(str)));
  149. }
  150.  
  151. /**
  152. * 从指定字符串生成密钥,密钥所需的字节数组长度为8位 不足8位时后面补0,超出8位只取前8位
  153. *
  154. * @param arrBTmp
  155. * 构成该字符串的字节数组
  156. * @return 生成的密钥
  157. * @throws java.lang.Exception
  158. */
  159. private Key getKey(byte[] arrBTmp) throws Exception {
  160. // 创建一个空的8位字节数组(默认值为0)
  161. byte[] arrB = new byte[8];
  162.  
  163. // 将原始字节数组转换为8位
  164. for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {
  165. arrB[i] = arrBTmp[i];
  166. }
  167.  
  168. // 生成密钥
  169. Key key = new javax.crypto.spec.SecretKeySpec(arrB, "DES");
  170.  
  171. return key;
  172. }
  173.  
  174. /**
  175. * main方法
  176. *
  177. * @author 1595901624
  178. * @param args
  179. */
  180. public static void main(String[] args) {
  181.  
  182. }
  183. }
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
Main.java:13: error: class DesUtils is public, should be declared in a file named DesUtils.java
public class DesUtils {
       ^
1 error
stdout
Standard output is empty