import java.security.Key;java import java.security.Security;數組 import java.util.Date;app import javax.crypto.BadPaddingException;ide import javax.crypto.Cipher;工具 import com.hurong.core.util.DateUtil;this
public class DesUtils {加密
private static String strDefaultKey = "";//字符串默認鍵值spa private Cipher encryptCipher = null;//加密工具code private Cipher decryptCipher = null;//解密工具ip
/** * 將byte數組轉換爲表示16進制值的字符串, 如:byte[]{8,18}轉換爲:0813, 和public static byte[] * hexStr2ByteArr(String strIn) 互爲可逆的轉換過程 * @param arrB 須要轉換的byte數組 * @return 轉換後的字符串 * */ public static String byteArr2HexStr(byte[] arrB){ int iLen = arrB.length; StringBuffer sb = new StringBuffer(iLen * 2);// 每一個byte用兩個字符才能表示,因此字符串的長度是數組長度的兩倍 for (int i = 0; i < iLen; i++) { int intTmp = arrB[i]; while (intTmp < 0) {// 把負數轉換爲正數 intTmp = intTmp + 256; } if (intTmp < 16) {// 小於0F的數須要在前面補0 sb.append("0"); } sb.append(Integer.toString(intTmp, 16)); } return sb.toString(); }
/** * 將表示16進制值的字符串轉換爲byte數組, 和public static String byteArr2HexStr(byte[] arrB) * 互爲可逆的轉換過程 * * @param strIn 須要轉換的字符串 * @return 轉換後的byte數組 */ public static byte[] hexStr2ByteArr(String strIn){ byte[] arrB = strIn.getBytes(); int iLen = arrB.length; byte[] arrOut = new byte[iLen / 2];// 兩個字符表示一個字節,因此字節數組長度是字符串長度除以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; }
/** * 默認構造方法,使用默認密鑰 * * @throws Exception */ public DesUtils() throws Exception { this(strDefaultKey); }
/** * DES字符串加密 * 指定密鑰構造方法 * @param strKey 指定的密鑰 * @throws Exception */ public DesUtils(String strKey){ Security.addProvider(new com.sun.crypto.provider.SunJCE()); try { Key key = getKey(strKey.getBytes()); encryptCipher = Cipher.getInstance("DES"); encryptCipher.init(Cipher.ENCRYPT_MODE, key); decryptCipher = Cipher.getInstance("DES"); decryptCipher.init(Cipher.DECRYPT_MODE, key); } catch (Exception e) { e.printStackTrace(); } }
/** * 加密字節數組 * * @param arrB 需加密的字節數組 * @return 加密後的字節數組 * @throws Exception */ public byte[] encrypt(byte[] arrB){ byte[] encrypt=null; try { encrypt=encryptCipher.doFinal(arrB); } catch (Exception e) { e.printStackTrace(); return null; } return encrypt; }
/** * 加密字符串 * * @param strIn 需加密的字符串 * @return 加密後的字符串 * @throws Exception */ public String encrypt(String strIn){ return byteArr2HexStr(encrypt(strIn.getBytes())); }
/** * 解密字節數組 * * @param arrB 需解密的字節數組 * @return 解密後的字節數組 * @throws Exception */ public byte[] decrypt(byte[] arrB) throws BadPaddingException{ byte[] decrypt=null; try { decrypt= decryptCipher.doFinal(arrB); } catch (Exception e) { //e.printStackTrace(); return null; } return decrypt; }
/** * 解密字符串 * * @param strIn 需解密的字符串 * @return 解密後的字符串 * @throws Exception */ public String decrypt(String strIn) throws BadPaddingException{
if(null==decrypt(hexStr2ByteArr(strIn))){ return ""; } return new String(decrypt(hexStr2ByteArr(strIn))); }
/** * 從指定字符串生成密鑰,密鑰所需的字節數組長度爲8位 不足8位時後面補0,超出8位只取前8位 * * @param arrBTmp 構成該字符串的字節數組 * @return 生成的密鑰 * @throws java.lang.Exception */ private Key getKey(byte[] arrBTmp) throws Exception { byte[] arrB = new byte[8];// 建立一個空的8位字節數組(默認值爲0) for (int i = 0; i < arrBTmp.length && i < arrB.length; i++) {// 將原始字節數組轉換爲8位 arrB[i] = arrBTmp[i]; } Key key = new javax.crypto.spec.SecretKeySpec(arrB, "DES");// 生成密鑰 return key; } public static boolean getAuthorization(String password,String key){
try{ boolean flag=false; DesUtils des = new DesUtils(password);//自定義密鑰 String code=des.decrypt(key); String mac=GetMACUtil.getMacStr();//機器碼 if(mac.equals(code)){ flag=true; } return flag; } catch (Exception e) { e.printStackTrace(); return false; } }
public String getEncod(String mahCode,String password,String endDate,DesUtils des) { try { String str=""; String dstr=mahCode+","+endDate; str=des.encrypt(dstr); return str; } catch (Exception e) { e.printStackTrace(); return ""; } } public static void main(String[] args) { DesUtils du=new DesUtils("creditsoftware"); String code=du.getEncod("70-1A-04-71-09-0E", "creditsoftware", "2012-12-31", du); System.out.println(code);
} } |