public static byte[] Decrypt(byte[] inData, string key) { // 密钥 byte[] btKey = MyTools.HexStringToBytes(key); // 初始向量 byte[] btIV = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; DES des = new DESCryptoServiceProvider(); des.Mode = CipherMode.ECB; des.Padding = PaddingMode.None; des.BlockSize = 64; using (MemoryStream ms = new MemoryStream()) { try { using (CryptoStream cs = new CryptoStream(ms, des.CreateDecryptor(btKey, btIV), CryptoStreamMode.Write)) { cs.Write(inData, 0, inData.Length); cs.FlushFinalBlock(); cs.Close(); } return(ms.ToArray()); } catch (Exception e) { MessageBox.Show(e.Message, "消息提示框"); } } return(null); }
// 获取字符串的MD5值,1为UTF-8编码,2位Hex public static String GetMD5(String strSrc, int index) { try { if (strSrc == null) { MessageBox.Show("输入数据不能为空!", "消息提示框"); } byte[] byt = null; if (index == 1) { byt = Encoding.UTF8.GetBytes(strSrc); } else if (index == 2) { byt = MyTools.HexStringToBytes(strSrc); } MD5 md5 = new MD5CryptoServiceProvider(); byte[] result = md5.ComputeHash(byt); return(MyTools.BytesToHexString(result)); } catch (Exception e) { MessageBox.Show(e.Message, "消息提示框"); } return(null); }
/** 计算方法 */ private string Hex2ChineseChar_Method(string text) { byte[] bs = MyTools.HexStringToBytes(Regex.Replace(text, @"\s", "")); if (bs == null) { return("数据不合法,请参照提示重新输入!"); } if (this.radioButton01_01.Checked) { if (this.radioButton01_03.Checked) { return(MyTools.addBlank(Encoding.GetEncoding("GBK").GetString(bs), int.Parse(this.textBox01_01.Text), true)); } else { return(Encoding.GetEncoding("GBK").GetString(bs)); } } else { if (this.radioButton01_03.Checked) { return(MyTools.addBlank(Encoding.GetEncoding("UTF-8").GetString(bs), int.Parse(this.textBox01_01.Text), true)); } else { return(Encoding.GetEncoding("UTF-8").GetString(bs)); } } }
public static String Hex2ASCII(string hex, int index) { try { byte[] bys = MyTools.HexStringToBytes(Regex.Replace(hex, @"\s", "")); if (bys == null) { return(null); } StringBuilder stringBuilder = new StringBuilder(); if (index == 0) { for (int i = 0; i < bys.Length; i++) { stringBuilder.Append(((char)bys[i]).ToString()); } } else { for (int i = 0; i < bys.Length; i++) { stringBuilder.Append(((char)bys[i]).ToString() + " "); } } return(stringBuilder.ToString()); } catch (Exception e) { MessageBox.Show(e.Message, "消息提示框"); } return(null); }
// 获取源数据UTF-8编码的byte,Hex编码的密钥,返回16进制字符串 public static String Get3DES(String strSrc, string key) { SymmetricAlgorithm tdes = new TripleDESCryptoServiceProvider(); // 密钥 tdes.Key = MyTools.HexStringToBytes(key); // 初始值 tdes.IV = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; // 指定加密的运算模式 tdes.Mode = System.Security.Cryptography.CipherMode.ECB; // 获取或设置加密算法的填充模式 tdes.Padding = System.Security.Cryptography.PaddingMode.PKCS7; // 块大小 tdes.BlockSize = 64; ICryptoTransform ct; System.IO.MemoryStream ms; CryptoStream cs; try { ct = tdes.CreateEncryptor(tdes.Key, tdes.IV); // 创建加密对象 byte[] byt = Encoding.UTF8.GetBytes(strSrc); // 转换为byte数组 int len = byt.Length; // 获取数组长度 int diff = 8 - len % 8; // 距8的整数倍还差多少 byte[] byt1 = new byte[len + diff]; // 定义大于byt的最小8的整数倍个空间 Array.Copy(byt, 0, byt1, 0, byt.Length); // 将与数据拷贝过去 byt1[len] = 0x80; // for (int j = 0; j < diff - 1; j++) { byt1[len + j + 1] = 0x00; } ms = new System.IO.MemoryStream(); cs = new CryptoStream(ms, ct, CryptoStreamMode.Write); cs.Write(byt1, 0, byt1.Length); cs.FlushFinalBlock(); if (cs != null) { cs.Close(); } byte[] temp = ms.ToArray(); byte[] result = new byte[temp.Length - 8]; Array.Copy(temp, 0, result, 0, temp.Length - 8); return(MyTools.BytesToHexString(result)); } catch (Exception e) { MessageBox.Show(e.Message, "消息提示框"); } return(null); }
public static byte[] Encrypt(byte[] inData, string key) { byte[] btKey = MyTools.HexStringToBytes(key); if (btKey == null) { return(null); } // 初始向量 byte[] btIV = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; try { // DES加密算法对象 DES des = new DESCryptoServiceProvider(); des.Mode = CipherMode.ECB; des.Padding = PaddingMode.None; des.BlockSize = 64; using (MemoryStream ms = new MemoryStream()) { // 获得报文的长度 int srcLen = inData.Length; // 获得报文与8的倍数之差 int diff = 8 - srcLen % 8; // 定义新报文的空间 byte[] bys = new byte[srcLen + diff]; Array.Copy(inData, bys, srcLen); // 为报文的后面追加0x80、0x00 bys[srcLen] = 0x80; for (int i = 0; i < diff - 1; i++) { bys[srcLen + i + 1] = 0x00; } try { using (CryptoStream cs = new CryptoStream(ms, des.CreateEncryptor(btKey, btIV), CryptoStreamMode.Write)) { cs.Write(bys, 0, bys.Length); cs.FlushFinalBlock(); cs.Close(); } return(ms.ToArray()); } catch (Exception e) { MessageBox.Show(e.Message, "消息提示框"); } } } catch (Exception e) { MessageBox.Show(e.Message, "消息提示框"); } return(null); }
/** 发送区1发送 */ private void btnManualSend3_Click(object sender, EventArgs e) { if (this.textSendRegionHex03.Checked) { this.SendData(MyTools.HexStringToBytes(this.textSendRegion3.Text.Replace(" ", ""))); } else { this.SendData(System.Text.Encoding.Default.GetBytes(this.textSendRegion3.Text)); } this.sendFrames++; this.sendBytes += textSendRegion3.Text.Replace(" ", "").Length / 2; DataCountShow(); // 滚动到控件光标处 RecvRegionShow(textSendRegion3.Text, false); }
public static String Get3Mac(String srcStr, string key) { try { // 获取密钥 string lKey = key.Substring(0, 16); string rKey = key.Substring(16, 16); // 初始值 byte[] btIV = new byte[] { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; // 获取3DES加密后的结果 // byte[] bys = MyTools.HexStringToBytes(srcStr); String strTemp = MyEnctypt.Get3DES(srcStr, key); // 该方法获取源数据UTF-8编码的byte,Hex编码的密钥,返回16进制字符串 byte[] md5 = MyTools.HexStringToBytes(MyEnctypt.GetMD5(strTemp, 2)); // 获取字符串的MD5值,1为UTF-8编码,2为Hex byte[] bysTemp = new byte[8]; // 临时数组 byte[] bysTemp1 = new byte[8]; // 临时数组 Array.Copy(md5, 0, bysTemp, 0, 8); // 将md5的前8个byte拷贝到bysTemp中 byte[] bysTemp2 = DoXOR(bysTemp, btIV); // 将前8个byte和初始值求异或 for (int i = 1; i < md5.Length / 8; i++) // 轮流DES加密、异或 { Array.Copy(Encrypt(bysTemp2, lKey), 0, bysTemp, 0, 8); // 加密 Array.Copy(md5, i * 8, bysTemp1, 0, 8); // 获取下一组 Array.Copy(DoXOR(bysTemp, bysTemp1), 0, bysTemp2, 0, 8); // 异或 } byte[] tail = new byte[] { 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; Array.Copy(Encrypt(bysTemp2, lKey), 0, bysTemp, 0, 8); // 加密 Array.Copy(DoXOR(bysTemp, tail), 0, bysTemp2, 0, 8); // 异或 Array.Copy(Encrypt(bysTemp2, lKey), 0, bysTemp, 0, 8); // 左8个字节密钥加密 Array.Copy(Decrypt(bysTemp, rKey), 0, bysTemp2, 0, 8); // 右8个字节密钥解密 Array.Copy(Encrypt(bysTemp2, lKey), 0, bysTemp, 0, 8); // 左8个字节密钥加密 return(MyTools.BytesToHexString(bysTemp)); // 返回16进制字符串 } catch (Exception e) { MessageBox.Show(e.Message, "消息提示框"); } return(null); }