C# 3DES加密详解

C# 3DES加密详解,第1张

概述最近一个项目中,因为服务端是用的java开发的,客户端是用的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,needData,data.length);      if (key.length == 16) {        System.arraycopy(key,k,key.length);        System.arraycopy(key,16,8);      } else {        System.arraycopy(key,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[],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,data.Length);    if (key.Length == 16)    {      Buffer.Blockcopy(key,key.Length);      Buffer.Blockcopy(key,8);    }    else    {      Buffer.Blockcopy(key,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,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();  }}

以上所述就是本文的全部内容了,希望大家能够喜欢。

总结

以上是内存溢出为你收集整理的C# 3DES加密详解全部内容,希望文章能够帮你解决C# 3DES加密详解所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1260894.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存