DES加密解密
C# Code
/// <summary> /// DES加密解密帮助类 /// </summary> public static class DESHelper { /// <summary> /// DES加密 /// </summary> /// <param name="pToEncrypt"></param> /// <param name="sKey">密钥(8位字符)</param> /// <returns></returns> public static string MD5Encrypt(string pToEncrypt, string sKey) { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] inputByteArray = Encoding.Default.GetBytes(pToEncrypt); des.Key = ASCIIEncoding.ASCII.GetBytes(sKey); des.IV = ASCIIEncoding.ASCII.GetBytes(sKey); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); StringBuilder ret = new StringBuilder(); foreach (byte b in ms.ToArray()) { ret.AppendFormat("{0:X2}", b); } ret.ToString(); return ret.ToString(); } /// <summary> /// DES解密 /// </summary> /// <param name="pToDecrypt"></param> /// <param name="sKey">密钥(8位字符)</param> /// <returns></returns> public static string MD5Decrypt(string pToDecrypt, string sKey) { DESCryptoServiceProvider des = new DESCryptoServiceProvider(); byte[] inputByteArray = new byte[pToDecrypt.Length / 2]; for (int x = 0; x < pToDecrypt.Length / 2; x++) { int i = (Convert.ToInt32(pToDecrypt.Substring(x * 2, 2), 16)); inputByteArray[x] = (byte)i; } des.Key = ASCIIEncoding.ASCII.GetBytes(sKey); des.IV = ASCIIEncoding.ASCII.GetBytes(sKey); MemoryStream ms = new MemoryStream(); CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write); cs.Write(inputByteArray, 0, inputByteArray.Length); cs.FlushFinalBlock(); StringBuilder ret = new StringBuilder(); return System.Text.Encoding.Default.GetString(ms.ToArray()); }
class Program { static void Main(string[] args) { #region DES加密 string password = "abcd"; string key = "12345678"; string encrypPassword = DESHelper.MD5Encrypt(password, key); Console.WriteLine("原来的:" + password); Console.WriteLine("加密后:"+encrypPassword); #endregion #region DES解密 string decryptPassword = DESHelper.MD5Decrypt(encrypPassword, key); Console.WriteLine("解密后:" + decryptPassword); Console.ReadKey(); #endregion } }
输出结果:
原来的:abcd
加密后:EA7240FBE783B4D7
解密后:abcd
Java Code
import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec; import javax.crypto.spec.IvParameterSpec; public class Des { /** * 加密 * @param data * @param sKey * @return */ public static byte[] encrypt(byte[] data, String sKey) { try { byte[] key = sKey.getBytes(); // 初始化向量 IvParameterSpec iv = new IvParameterSpec(key); DESKeySpec desKey = new DESKeySpec(key); // 创建一个密匙工厂,然后用它把DESKeySpec转换成securekey SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); SecretKey securekey = keyFactory.generateSecret(desKey); // Cipher对象实际完成加密操作 Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); // 用密匙初始化Cipher对象 cipher.init(Cipher.ENCRYPT_MODE, securekey, iv); // 现在,获取数据并加密 // 正式执行加密操作 return cipher.doFinal(data); } catch (Throwable e) { e.printStackTrace(); } return null; } /** * 解密 * @param src * @param sKey * @return * @throws Exception */ public static byte[] decrypt(byte[] src, String sKey) throws Exception { byte[] key = sKey.getBytes(); // 初始化向量 IvParameterSpec iv = new IvParameterSpec(key); // 创建一个DESKeySpec对象 DESKeySpec desKey = new DESKeySpec(key); // 创建一个密匙工厂 SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES"); // 将DESKeySpec对象转换成SecretKey对象 SecretKey securekey = keyFactory.generateSecret(desKey); // Cipher对象实际完成解密操作 Cipher cipher = Cipher.getInstance("DES/CBC/PKCS5Padding"); // 用密匙初始化Cipher对象 cipher.init(Cipher.DECRYPT_MODE, securekey, iv); // 真正开始解密操作 return cipher.doFinal(src); } /** * 将二进制转换成16进制 * * @param buf * @return */ public static String parseByte2HexStr(byte buf[]) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < buf.length; i++) { String hex = Integer.toHexString(buf[i] & 0xFF); if (hex.length() == 1) { hex = '0' + hex; } sb.append(hex.toUpperCase()); } return sb.toString(); } /** * 将16进制转换为二进制 * * @param hexStr * @return */ public static byte[] parseHexStr2Byte(String hexStr) { if (hexStr.length() < 1) return null; byte[] result = new byte[hexStr.length() / 2]; for (int i = 0; i < hexStr.length() / 2; i++) { int high = Integer.parseInt(hexStr.substring(i * 2, i * 2 + 1), 16); int low = Integer.parseInt(hexStr.substring(i * 2 + 1, i * 2 + 2), 16); result[i] = (byte) (high * 16 + low); } return result; } }
import java.nio.charset.Charset; public class DesUtil { /** * 加密 * @param srcStr * @param charset * @param sKey * @return */ public static String encrypt(String srcStr, Charset charset, String sKey) { byte[] src = srcStr.getBytes(charset); byte[] buf = Des.encrypt(src, sKey); return Des.parseByte2HexStr(buf); } /** * 解密 * * @param hexStr * @param sKey * @return * @throws Exception */ public static String decrypt(String hexStr, Charset charset, String sKey) throws Exception { byte[] src = Des.parseHexStr2Byte(hexStr); byte[] buf = Des.decrypt(src, sKey); return new String(buf, charset); } }
import java.nio.charset.Charset; public class Test { private static final String SKEY = "12345678"; private static final Charset CHARSET = Charset.forName("UTF-8"); public static void main(String[] args) { // 待加密内容 String password = "abcd"; String encryptPassword = DesUtil.encrypt(password, CHARSET, SKEY); System.out.println("原来的:" + password); System.out.println("加密后:"+encryptPassword); // 直接将如上内容解密 String decryPassword; try { decryPassword = DesUtil.decrypt(encryptPassword, CHARSET, SKEY); System.out.println("解密后:"+decryPassword); } catch (Exception e) { e.printStackTrace(); } } }
输出结果:
原来的:abcd
加密后:EA7240FBE783B4D7
解密后:abcd