Exemple #1
0
 public TokenEntry(LicenseToken token, string tokenStr, int position, int length)
 {
     Token    = token;
     Text     = tokenStr;
     Position = position;
     Length   = length;
 }
Exemple #2
0
 private void AddToken(LicenseToken Token, string TokenStr = "", int Position = -1, int EndPosition = -1)
 {
     if (EndPosition >= 0)
     {
         Tokens.Add(new TokenEntry(Token, TokenStr, Position, EndPosition - Position));
     }
     else
     {
         Tokens.Add(new TokenEntry(Token, TokenStr, Position));
     }
 }
        protected override void ProcessRecord()
        {
            var communicationKey = Convert.FromBase64String(CommunicationKeyAsBase64);

            if (communicationKey.Length != 32)
            {
                throw new NotSupportedException("Communication key must be 256 bits long.");
            }

            var envelope = new Hashtable
            {
                { "version", 1 },
                { "com_key_id", CommunicationKeyId.ToString() },
                { "message", LicenseToken }
            };

            // Copy the expiration from the "message" structure if they are defined.
            if (LicenseToken.ContainsKey("begin_date"))
            {
                envelope["begin_date"] = LicenseToken["begin_date"];
            }

            if (LicenseToken.ContainsKey("expiration_date"))
            {
                envelope["expiration_date"] = LicenseToken["expiration_date"];
            }

            // Now we have the outer structure ready. Convert to JSON and then sign.
            var json = JsonConvert.SerializeObject(envelope);

            WriteVerbose(json);

            // The output is the signed JOSE object, short-form serialized.
            var joseObject = JWT.Encode(json, communicationKey, JwsAlgorithm.HS256);

            WriteObject(joseObject);
        }
Exemple #4
0
        public LicenseToken Get()
        {
            string[]     encryptedText;
            LicenseToken token = new LicenseToken {
                Id = Guid.NewGuid(), Message = "MAC Address错误,请更新授权文件!", Result = false, Timestamp = DateTime.Now
            };

            try
            {
                encryptedText = File.ReadAllLines(@"C:\license.dat");
                if (encryptedText == null || encryptedText.Length < 1)
                {
                    token.Result  = false;
                    token.Message = "授权文件为空,请更新授权文件!";
                    return(token);
                }
            }
            catch (Exception e)
            {
                token.Result  = false;
                token.Message = "找不到授权文件,请更新授权文件!";
                return(token);
            }

            Crypto cryptoHelper = new Crypto();

            byte[] encryptedBytes;
            byte[] passwordBytes;
            byte[] decryptedBytes;
            string decryptedText;

            try {
                encryptedBytes = Convert.FromBase64String(encryptedText[0]);
            } catch (Exception e)
            {
                token.Result  = false;
                token.Message = "授权文件内容不对,请更新授权文件!";
                return(token);
            }
            passwordBytes = Encoding.UTF8.GetBytes("Lorentz@QWESTRO");
            passwordBytes = SHA256.Create().ComputeHash(passwordBytes);

            decryptedBytes = cryptoHelper.AES_Decrypt(encryptedBytes, passwordBytes);
            decryptedText  = Encoding.UTF8.GetString(decryptedBytes);

            if (decryptedText.Length != 21)
            {
                token.Result  = false;
                token.Message = "授权文件内容不对,请更新授权文件!";
                return(token);
            }

            string macAddress    = "";
            string expiryDateStr = "";

            try
            {
                macAddress    = decryptedText.Substring(0, 12);
                expiryDateStr = decryptedText.Substring(13);
            }
            catch (Exception e)
            {
                token.Result  = false;
                token.Message = "授权文件格式不对,请更新授权文件!";
                return(token);
            }

            DateTime localDate = DateTime.Now;
            DateTime expiryDate;

            try {
                expiryDate = DateTime.ParseExact(expiryDateStr, "ddMMyyyy", null);
            } catch
            {
                token.Result  = false;
                token.Message = "日期格式不对,请更新授权文件!";
                return(token);
            }

            IPGlobalProperties computerProperties = IPGlobalProperties.GetIPGlobalProperties();

            NetworkInterface[] nics = NetworkInterface.GetAllNetworkInterfaces();

            if (nics == null || nics.Length < 1)
            {
                token.Result  = false;
                token.Message = "没有网卡,必须安装网卡运行!";
                return(token);
            }

            foreach (NetworkInterface adapter in nics)
            {
                if (!String.Equals(adapter.NetworkInterfaceType.ToString(), "ethernet", StringComparison.OrdinalIgnoreCase))
                {
                    continue;
                }

                PhysicalAddress address = adapter.GetPhysicalAddress();

                if (String.Equals(address.ToString(), macAddress, StringComparison.OrdinalIgnoreCase))
                {
                    if (DateTime.Compare(localDate, expiryDate) <= 0)
                    {
                        token.Result  = true;
                        token.Message = macAddress;
                    }
                    else
                    {
                        token.Result  = false;
                        token.Message = "授权文件已经过期(" + expiryDate.ToString() + "),请更新授权文件!";
                    }
                }
            }

            return(token);
        }