登录 注册
当前位置:主页 > 资源下载 > 30 > C#实现对称分组加密算法RC2

C#实现对称分组加密算法RC2

  • 更新:2024-09-22 21:07:15
  • 大小:249KB
  • 推荐:★★★★★
  • 来源:网友上传分享
  • 类别:C# - 后端
  • 格式:RAR

资源介绍

RC2是由著名密码学家Ron Rivest设计的一种传统对称分组加密算法,它可作为DES算法的建议替代算法。它的输入和输出都是64比特。密钥的长度是从8字节到128字节可变,但目前的实现是8字节(1998年)。 此算法被设计为可容易地在16位的微处理器上实现。在一个IBM AT机上,RC2加密算法的执行可比DES算法快两倍(假设进行密钥扩展)。在C#中用RC2CryptoServiceProvider可以实现此算法。 算法原理: 1.根据计算机cpu序列号 ,硬盘ID,网卡硬件地址 号生成注册码: //获取机器码 public static string getMachineCode() { string machineCode = ""; MD5CryptoServiceProvider provider1; byte[] array1; string text1; string text2; byte num1; byte[] array2; int num2; provider1 = new MD5CryptoServiceProvider(); string cpuInfo = "";//cpu序列号 ManagementClass cimobject = new ManagementClass("Win32_Processor"); ManagementObjectCollection moc = cimobject.GetInstances(); foreach (ManagementObject mo in moc) { cpuInfo += mo.Properties["ProcessorId"].Value.ToString(); } //获取硬盘ID string HDid = ""; ManagementClass cimobjectHDid = new ManagementClass("Win32_DiskDrive"); ManagementObjectCollection mocHDid = cimobjectHDid.GetInstances(); foreach (ManagementObject mo in mocHDid) { HDid += (string)mo.Properties["Model"].Value; } //获取网卡硬件地址 string strMac = ""; ManagementClass mc = new ManagementClass("Win32_NetworkAdapterConfiguration"); ManagementObjectCollection mocMac = mc.GetInstances(); foreach (ManagementObject mo in mocMac) { if ((bool)mo["IPEnabled"] == true) strMac += mo["MacAddress"].ToString(); mo.Dispose(); } array1 = provider1.ComputeHash(Encoding.Default.GetBytes(cpuInfo + strMac)); text1 = "ENTOPYMICROSYSTEMSDEVINIMMUHENDISLIK231456789ACD23456789AEFABGHJKLMNPRSTUVWYZXAHMETALIAKKASHAKANESKICI"; text2 = string.Empty; array2 = array1; for (num2 = 0; (num2 < array2.Length); num2 = (num2 + 1)) { num1 = array2[num2]; text2 = string.Concat(text2, text1.Substring((num1 % text1.Length), 1)); } machineCode = text2.Substring(0, 10); return machineCode; } 2.根据注册码按照RC2生成注册码: //根据算法得到注册码 public static string getRegisterCode(string machineCode,string _configName) { string computeCode = ""; byte[] array1, array2, array3, array4; MemoryStream stream1 = new MemoryStream(); BinaryFormatter formatter1 = new BinaryFormatter(); formatter1.Serialize(stream1, machineCode); array1 = stream1.ToArray(); RC2CryptoServiceProvider provider11 = new RC2CryptoServiceProvider(); provider11.KeySize = 128; #region byte[] str1 = new byte[8] ; byte[] str2 = new byte[16] ; DataTable dt = Xml.GetAllDataFromXml("Config.xml"); if (dt != null && dt.Rows.Count > 0) { foreach(DataRow dr in dt.Rows) { if (dr[1].ToString() == _configName) { str1 = new byte[] { byte.Parse(dr[2].ToString()), byte.Parse(dr[3].ToString()), byte.Parse(dr[4].ToString()), byte.Parse(dr[5].ToString()), byte.Parse(dr[6].ToString()), byte.Parse(dr[7].ToString()), byte.Parse(dr[8].ToString()), byte.Parse(dr[9].ToString()) }; str2 = new byte[] { byte.Parse(dr[10].ToString()), byte.Parse(dr[11].ToString()), byte.Parse(dr[12].ToString()), byte.Parse(dr[13].ToString()), byte.Parse(dr[14].ToString()), byte.Parse(dr[15].ToString()), byte.Parse(dr[16].ToString()), byte.Parse(dr[17].ToString()), byte.Parse(dr[18].ToString()), byte.Parse(dr[19].ToString()), byte.Parse(dr[20].ToString()), byte.Parse(dr[21].ToString()), byte.Parse(dr[22].ToString()), byte.Parse(dr[23].ToString()),byte.Parse(dr[24].ToString()),byte.Parse(dr[25].ToString()) }; break; } } } #endregion array2 =str1 ; array3 = str2 ; provider11.IV = array2; provider11.Key = array2; ICryptoTransform transform1 = provider11.CreateEncryptor(); stream1 = new MemoryStream(); CryptoStream stream2 = new CryptoStream(stream1, transform1, System.Security.Cryptography.CryptoStreamMode.Write); try { stream2.Write(array1, 0, array1.Length); stream2.FlushFinalBlock(); array4 = stream1.ToArray(); } finally { stream1.Close(); stream2.Close(); } stream1 = new MemoryStream(); formatter1.Serialize(stream1, array4); computeCode = Convert.ToBase64String(stream1.ToArray()).Trim(); return computeCode; } 3.check public static bool checkRegisterCode(string machineCode, string registerCode,string configName) { string computeCode = getRegisterCode(machineCode, configName); if (computeCode == registerCode) { return true; } else return false; }