,,,,
,,,,
<强> 强>
import java.security.MessageDigest; public static  final String 编码(String s), { ,,,,,,,char hexDigits [],=, { “0”,“1”,“2”,“3”,“4”,“5”,“6”,“7”,“8”,“9”, A, B,,“C”,“D”,“E”,,“F”,}; try { 时间=byte [], btInput s.getBytes ();//,获得MD5摘要算法的,MessageDigest 对象 MessageDigest mdInst =, MessageDigest.getInstance (MD5);//,使用指定的字节更新摘要 mdInst.update (btInput);//,获得密文 时间=byte [], md mdInst.digest ();//,把密文转换成十六进制的字符串形式 int j =, md.length; char str [],=, new char (* j 2); int k =, 0; for (int 小姐:=,0;,小姐:& lt;, j;,我+ +),{ byte byte0 =, md[我]; str (k + +),=, hexDigits [byte0 在祝辞祝辞,4,,,0 xf); str (k + +),=, hexDigits [byte0 ,, 0 xf); } return new 字符串(str); },catch (Exception e), { e.printStackTrace (); return 零; } }
<强> 强>
public static String COMMON_KEY =,“公元843年”;//密钥 public static  byte [], desEncrypt (String 味精,String 盐),{ if (msg ==, null) msg =,“”; if (salt ==, null), { 时间=salt COMMON_KEY; } 时间=byte [], keyBytes new 字节[8]; int saltLen =, salt.length (); 时间=byte [], saltBytes salt.getBytes();//转换成字节数组,这是加密的必要步骤! for (int 小姐:=,0;,小姐:& lt;, 8;,我+ +),{ keyBytes[我],=,saltBytes[小姐:%,saltLen); } try { DESKeySpec keySpec =, new DESKeySpec (keyBytes); SecretKey key =, SecretKeyFactory.getInstance (DES) .generateSecret (keySpec); Cipher desCipher =, . getinstance (“DES/欧洲/PKCS5Padding”); desCipher.init (Cipher.ENCRYPT_MODE,关键); 时间=byte [], text msg.getBytes (“utf - 8”); 时间=byte [], ciphertext desCipher.doFinal(文本); return 密文; },catch (Exception e), { e.printStackTrace (); } return 零; } public static  String desDecrypt (byte[],味精、,String 盐),{ if (msg ==, null) return 零; if (salt ==, null), { 时间=salt COMMON_KEY; } 时间=byte [], keyBytes new 字节[8]; int saltLen =, salt.length (); 时间=byte [], saltBytes salt.getBytes (); for (int 小姐:=,0;,小姐:& lt;, 8;,我+ +),{ keyBytes[我],=,saltBytes[小姐:%,saltLen); } try { DESKeySpec keySpec =, new DESKeySpec (keyBytes); SecretKey key =, SecretKeyFactory.getInstance (DES) .generateSecret (keySpec); Cipher desCipher =, . getinstance (“DES/欧洲/PKCS5Padding”); desCipher.init (Cipher.DECRYPT_MODE,关键); 时间=byte [], deciphertext desCipher.doFinal(味精); return new 字符串(deciphertext,“utf - 8”); },catch (Exception e), { e.printStackTrace (); } return 零; } public static  String dumpBytes (byte[],字节),{ StringBuffer sb =, new StringBuffer (); for (int 小姐:=,0;,小姐:& lt;, bytes.length;,我+ +),{ if (小姐:%,32,==,0,,,,小姐:!=,0),{ sb.append (“\ n”); } String s =, Integer.toHexString(字节[我]); if (s.length (), & lt;, 2), { 时间=s “0”, +,年代; } if (s.length(),祝辞,2),{ 时间=s s.substring (s.length(),安康;2); } sb.append(年代); } return sb.toString (); } public static  byte [], parseBytes (String str), { try { int len =, str.length (),/, 2; 时间=if (len & lt; 2), { return new  byte [], {, Byte.parseByte (str),}; } 时间=byte [], arr new 字节(len); for (int 小姐:=,0;,小姐:& lt;, arr.length;,我+ +),{ arr[我],=,(字节),Integer.parseInt ( str.substring(小姐:*,2,,小姐:*,2,+,2),,16); } return 加勒比海盗; },catch (Exception e), { return new 字节[0]; } }/* * ,*加密 *大敌; ,* @param encrypt_value ,*,,,,,,,,,,,被加密的字符串 ,* @param encrypt_key ,*,,,,,,,,,,,加密的密钥 ,* @return ,*/null null null null null null null null null null null null null null null null nullMD5和3 des加密方式浅析