C# 3DES加密详解
最近一个项目中,因为服务端是用的java开发的,客户端是用的C#,由于通信部分采用到了3DES加密,所以做个记录,以备以后需要的时候直接用。
这是对方(java)的加密算法,和网上流传的代码也差不多(主密钥直接写死了,方便测试)
package org.zwork.market.mina.msg; import java.security.spec.KeySpec; import javax.crypto.Cipher; import javax.crypto.SecretKey; import javax.crypto.SecretKeyFactory; import javax.crypto.spec.DESKeySpec; import javax.crypto.spec.DESedeKeySpec; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.zwork.market.MktContants; public class ThreeEncryptDecrypt { private static final Logger LOGGER = LoggerFactory.getLogger(ThreeEncryptDecrypt.class); // 定义 加密算法,可用 DES,DESede,Blowfish public static final String Algorithm = "DESede"; public static String DES = "DES/ECB/NoPadding"; public static String TriDes = "DESede/ECB/NoPadding"; // des加密 public static byte[] des_crypt(byte key[], byte data[]) { try { KeySpec ks = new DESKeySpec(key); SecretKeyFactory kf = SecretKeyFactory.getInstance("DES"); SecretKey ky = kf.generateSecret(ks); Cipher c = Cipher.getInstance(DES); c.init(Cipher.ENCRYPT_MODE, ky); return c.doFinal(data); } catch (Exception e) { LOGGER.error("des_crypt error:", e); return null; } } // des解密 public static byte[] des_decrypt(byte key[], byte data[]) { try { KeySpec ks = new DESKeySpec(key); SecretKeyFactory kf = SecretKeyFactory.getInstance("DES"); SecretKey ky = kf.generateSecret(ks); Cipher c = Cipher.getInstance(DES); c.init(Cipher.DECRYPT_MODE, ky); return c.doFinal(data); } catch (Exception e) { LOGGER.error("des_decrypt error:", e); return null; } } // 3DES加密 public static byte[] trides_crypt(byte key[], byte data[]) { try { byte[] k = new byte[24]; int len = data.length; if (data.length % 8 != 0) { len = data.length - data.length % 8 + 8; } byte[] needData = null; if (len != 0) needData = new byte[len]; for (int i = 0; i < len; i++) { needData[i] = 0x00; } System.arraycopy(data, 0, needData, 0, data.length); if (key.length == 16) { System.arraycopy(key, 0, k, 0, key.length); System.arraycopy(key, 0, k, 16, 8); } else { System.arraycopy(key, 0, k, 0, 24); } KeySpec ks = new DESedeKeySpec(k); SecretKeyFactory kf = SecretKeyFactory.getInstance("DESede"); SecretKey ky = kf.generateSecret(ks); Cipher c = Cipher.getInstance(TriDes); c.init(Cipher.ENCRYPT_MODE, ky); return c.doFinal(needData); } catch (Exception e) { LOGGER.error("trides_crypt error:", e); return null; } } // 3DES解密 public static byte[] trides_decrypt(byte key[], byte data[]) { try { byte[] k = new byte[24]; int len = data.length; if (data.length % 8 != 0) { len = data.length - data.length % 8 + 8; } byte[] needData = null; if (len != 0) needData = new byte[len]; for (int i = 0; i < len; i++) { needData[i] = 0x00; } System.arraycopy(data, 0, needData, 0, data.length); if (key.length == 16) { System.arraycopy(key, 0, k, 0, key.length); System.arraycopy(key, 0, k, 16, 8); } else { System.arraycopy(key, 0, k, 0, 24); } KeySpec ks = new DESedeKeySpec(k); SecretKeyFactory kf = SecretKeyFactory.getInstance("DESede"); SecretKey ky = kf.generateSecret(ks); Cipher c = Cipher.getInstance(TriDes); c.init(Cipher.DECRYPT_MODE, ky); return c.doFinal(needData); } catch (Exception e) { LOGGER.error("trides_decrypt error:", e); return null; } } public static String getPass(String source) { byte[] data= hexToBytes(source); byte[] key ="111111111111111111111111111a1.1.".getBytes(); String result = byte2hex(trides_decrypt(key, data)).toUpperCase(); return result.substring(2, 8); } public static String byte2hex(byte[] data) { StringBuffer sb = new StringBuffer(); for (int i = 0; i < data.length; i++) { String temp = Integer.toHexString(((int) data[i]) & 0xFF); for (int t = temp.length(); t < 2; t++) { sb.append("0"); } sb.append(temp); } return sb.toString(); } public static byte[] hexToBytes(String str) { if (str == null) { return null; } else if (str.length() < 2) { return null; } else { int len = str.length() / 2; byte[] buffer = new byte[len]; for (int i = 0; i < len; i++) { buffer[i] = (byte) Integer.parseInt(str.substring(i * 2, i * 2 + 2), 16); } return buffer; } } }
因为客户端只负责数据加密,所以我这里只写了加密的部分。由于java和C#语言很相似,所以我就仿这他们给的java代码改成C#的,当然也在网上参考了一些代码,不过中间还是出现了些问题,比如C#不支持弱密钥(把密钥弄复杂点),因为没注意大小写造成加密结果不一致等等。编程这东西一个点都能让整个系统崩溃,所以小细节很重要!!!
public class DESHelper { /// <summary> /// 将密码转成直接数组 /// </summary> /// <param name="str"></param> /// <returns></returns> public static byte[] HexToBytes(String str) { if (str == null) { return null; } else if (str.Length < 2) { return null; } else { int len = str.Length / 2; byte[] buffer = new byte[len]; for (int i = 0; i < len; i++) { var temp = str.Substring(i * 2, 2); buffer[i] = (byte)Convert.ToInt32(temp, 16); } return buffer; } } /// <summary> /// 3DES加密 /// </summary> /// <param name="key"></param> /// <param name="data"></param> /// <returns></returns> public static byte[] GetDes3EncryptedText(byte[] key, byte[] data) { byte[] k = new byte[24]; int len = data.Length; if (data.Length % 8 != 0) { len = data.Length - data.Length % 8 + 8; } byte[] needData = null; if (len != 0) needData = new byte[len]; for (int i = 0; i < len; i++) { needData[i] = 0x00; } Buffer.BlockCopy(data, 0, needData, 0, data.Length); if (key.Length == 16) { Buffer.BlockCopy(key, 0, k, 0, key.Length); Buffer.BlockCopy(key, 0, k, 16, 8); } else { Buffer.BlockCopy(key, 0, k, 0, 24); } var des3 = new TripleDESCryptoServiceProvider(); des3.Key = k; des3.Mode = CipherMode.ECB; des3.Padding = PaddingMode.Zeros; using (MemoryStream ms = new MemoryStream()) using (CryptoStream cs = new CryptoStream(ms, des3.CreateEncryptor(), CryptoStreamMode.Write)) { cs.Write(data, 0, data.Length); cs.FlushFinalBlock(); return ms.ToArray(); } } /// <summary> /// 将加密结果转成字符串 /// </summary> /// <param name="data"></param> /// <returns></returns> public static String GetByte2Hex(byte[] data) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < data.Length; i++) { String temp = string.Format("{0:X}", ((int)data[i]) & 0xFF); for (int t = temp.Length; t < 2; t++) { sb.Append("0"); } sb.Append(temp); } return sb.ToString(); } }
以上所述就是本文的全部内容了,希望大家能够喜欢。
赞 (0)