}//end private static LicenseObj DesSerializationObj1(string str) { NetDataContractSerializer net = new NetDataContractSerializer(); System.IO.MemoryStream s = new MemoryStream(); byte[] bs = Convert.FromBase64String(str); s.Write(bs, 0, bs.Length); s.Position = 0; LicenseObj list = (LicenseObj)net.Deserialize(s); s.Close(); return(list); }
private static License GetLicenseFromString(string licenseFile) { License obj = new License(); try { string publickey = "<RSAKeyValue><Modulus>usDbA+n8n/lwRraXvdSiSO8lThz+0m0cn+1ESndCmLk8tp+XKklCN1s2i1Q5ytHYVXHcMnAVPAwDux2XQmhcCUZn1+usxpLUo/yL4IoABYHjPXtdCCDXxV5aHLqmo+5+FSe+l8jgDIPKDaCPzBhAt51JZZSJ9qnZUZP+pIDXbn0=</Modulus><Exponent>AQAB</Exponent></RSAKeyValue>";//公钥 //公钥验证 FileStream aFile = new FileStream(licenseFile, FileMode.Open, FileAccess.Read); StreamReader read = new StreamReader(aFile); string objString = read.ReadLine(); read.Close(); LicenseObj _LicenseObj = DesSerializationObj1(objString); //获取本机cpu disk //byte[] messagebytes = Encoding.UTF8.GetBytes(GetCpuID() + GetHardDiskID()); StringBuilder str = new StringBuilder(GetCpuID() + GetHardDiskID1()); for (int i = 0; i < str.Length; i++) { str[i] = Convert.ToChar(Convert.ToInt32(str[i]) % 7 + Convert.ToInt32(str[i])); } byte[] messagebytes = Encoding.UTF8.GetBytes(str.ToString()); RSACryptoServiceProvider oRSA4 = new RSACryptoServiceProvider(); oRSA4.FromXmlString(publickey); bool bVerify = oRSA4.VerifyData(messagebytes, "SHA1", _LicenseObj.Message); if (!bVerify) { throw new LicenseException("未知的授权信息,请联系中联重科!"); } if (bVerify && _LicenseObj.type == 2) { if (string.IsNullOrEmpty(_LicenseObj.DateStart) || string.IsNullOrEmpty(_LicenseObj.DateEnd)) { throw new LicenseException("没有找到程序授权时间信息,请联系中联重科!"); } //验证时间 DateTime DateStart = Convert.ToDateTime(_LicenseObj.DateStart); DateTime DateEnd = Convert.ToDateTime(_LicenseObj.DateEnd); if (DateTime.Now < DateStart || DateTime.Now > DateEnd) { throw new LicenseException("不在授权期内,请联系中联重科!"); } } obj.Verify = bVerify; obj.DateStart = _LicenseObj.DateStart; obj.DateEnd = _LicenseObj.DateEnd; obj.type = _LicenseObj.type; obj.Version = _LicenseObj.Version; return(obj); } catch (Exception e) { if (e is LicenseException) { throw e; } else { throw new LicenseException("授权出现未知错误!错误信息:" + e.Message); } } }