1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
| package com.bjtcrj.gms.core.utils;
import javax.crypto.Cipher; import javax.crypto.NoSuchPaddingException; import java.security.Key; import java.security.NoSuchAlgorithmException;
public class DesUtil {
private static Cipher mEncryptCipher = null; private static Cipher mDecryptCipher = null;
static { try { mEncryptCipher = Cipher.getInstance("DES"); mDecryptCipher = Cipher.getInstance("DES"); } catch (NoSuchAlgorithmException e) { e.printStackTrace(); } catch (NoSuchPaddingException e) { e.printStackTrace(); } }
private DesUtil() { }
public static String encrypt(String strIn, String key) throws Exception { return byte2HexStr(encrypt(strIn.getBytes(), key)); }
private static byte[] encrypt(byte[] arrB, String key) throws Exception { mEncryptCipher.init(Cipher.ENCRYPT_MODE, getKey(key.getBytes())); return mEncryptCipher.doFinal(arrB); }
public static String decrypt(String strIn, String key) throws Exception { return new String(decrypt(hexStr2Byte(strIn), key)); }
private static byte[] decrypt(byte[] arrB, String key) throws Exception { mDecryptCipher.init(Cipher.DECRYPT_MODE,getKey(key.getBytes())); return mDecryptCipher.doFinal(arrB); }
private static Key getKey(byte[] arrBTmp) throws Exception { byte[] arrB = new byte[8];
for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) { arrB[i] = arrBTmp[i]; }
Key key = new javax.crypto.spec.SecretKeySpec(arrB, "DES");
return key; }
public static byte[] hexStr2Byte(String strIn) throws Exception { byte[] arrB = strIn.getBytes(); int iLen = arrB.length;
byte[] arrOut = new byte[iLen / 2]; for (int i = 0; i < iLen; i = i + 2) { String strTmp = new String(arrB, i, 2); arrOut[i / 2] = (byte) Integer.parseInt(strTmp, 16); } return arrOut; }
public static String byte2HexStr(byte[] arrB) throws Exception { int iLen = arrB.length; StringBuffer sb = new StringBuffer(iLen * 2); for (int i = 0; i < iLen; i++) { int intTmp = arrB[i]; while (intTmp < 0) { intTmp = intTmp + 256; } if (intTmp < 16) { sb.append("0"); } sb.append(Integer.toString(intTmp, 16)); } return sb.toString(); }
public static void main(String[] args) { try { System.out.println("加密前:"); StringBuilder stringBuilder = new StringBuilder("asdklfjalsdkfj"); long start = System.currentTimeMillis(); String pwd = encrypt(stringBuilder.toString(), "abc"); long encend = System.currentTimeMillis(); System.out.println("加密耗时:" + (encend -start) + "ms"); System.out.println("加密后:" + pwd); pwd = decrypt(pwd, "abc"); System.out.println("解密耗时:" + (System.currentTimeMillis() -encend) + "ms"); System.out.println("解密密后:" + pwd);
} catch (Exception e) { e.printStackTrace(); } } }
|