C# 3DES加密详解
程序员文章站
2022-06-21 09:48:09
最近一个项目中,因为服务端是用的java开发的,客户端是用的c#,由于通信部分采用到了3des加密,所以做个记录,以备以后需要的时候直接用。
这是对方(java)的加...
最近一个项目中,因为服务端是用的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(); } }
以上所述就是本文的全部内容了,希望大家能够喜欢。
上一篇: 关节炎吃什么食物好呢
下一篇: C#静态字段的两个用处