日期:2014-05-17 浏览次数:20973 次
// java格式化16进制输出 public static void formatIntTo16(byte[] bytes) { String[] resultBytes = new String[bytes.length]; for (int i = 0; i < bytes.length; i++) { resultBytes[i] = Integer.toHexString(bytes[i] & 0xFF); } }
//MD5加密 public static String md5Encode(String strContent) { try { MessageDigest messageDigest = MessageDigest.getInstance("MD5"); byte[] tempBytes = messageDigest.digest(strContent.getBytes("UnicodeLittleUnmarked")); StringBuffer stringBuffer = new StringBuffer(); for(byte bytes: tempBytes) { String tempByte = Integer.toHexString(bytes & 0xFF); if(tempByte.length() == 1) { stringBuffer.append("0"); } stringBuffer.append(tempByte); } return stringBuffer.toString(); } catch(Exception e) { e.printStackTrace(); } return null; }
//md5加密 public static void MD5(string str) { string cl1 = str; string pwd = ""; System.Security.Cryptography.MD5 md5 = System.Security.Cryptography.MD5.Create(); byte[] s = md5.ComputeHash(Encoding.Unicode.GetBytes(cl1)); for (int i = 0; i < s.Length; i++) { pwd = pwd + s[i].ToString("x"); } Console.Write(pwd); Console.WriteLine(); }
// 3DES加密 基方法 public static byte[] des3EncodeCBC(byte[] data, byte[] key) throws Exception { Key deskey = null; DESedeKeySpec spec = new DESedeKeySpec(key); SecretKeyFactory keyfactory = SecretKeyFactory.getInstance("desede"); deskey = keyfactory.generateSecret(spec); Cipher cipher = Cipher.getInstance("desede" + "/CBC/PKCS5Padding"); IvParameterSpec ips = new IvParameterSpec(IV); cipher.init(Cipher.ENCRYPT_MODE, deskey, ips); return cipher.doFinal(data, 0, data.length); }
// 3DES加密算法 private static bool Encrypt( byte[] key, byte[] input, out byte[] output ) { output = null; try { TripleDESCryptoServiceProvider trippleDesProvider = new TripleDESCryptoServiceProvider(); ICryptoTransform encryptObj = trippleDesProvider.CreateEncryptor(rkey, IV); output = encryptObj.TransformFinalBlock(input, 0, input.Length); trippleDesProvider.Clear(); } catch (Exception) { return false; } return true; }
// 3DES 解密 基方法 public static byte[] des3DecodeCBC(byte[] data, byte[] key) throws Exception { Key deskey = null; DESedeKeySpec spec = new DESedeKeySpec(key); SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("desede"); deskey = keyFactory.generateSecret(spec); Cipher cipher = Cipher.getInstance("desede" + "/CBC/PKCS5Padding"); IvParameterSpec ips = new IvParameterSpec(IV); cipher.init(Cipher.DECRYPT_MODE, deskey, ips); return cipher.doFinal(data, 0, data.length); }
// 3DES 解密 基方法 private static bool Decrypt(byte[] key, byte[] input, out byte[] output) { output = null; try { TripleDESCryptoServiceProvider trippleDesProvider = new TripleDESCryptoServiceProvider(); ICryptoTransform decryptObj = trippleDesProvider.CreateDecryptor(key,