Example #1
0
        /// <summary>
        /// this method is used when client cannot connect to the internet or service is broken.
        /// </summary>
        /// <returns></returns>
        public bool SNVerifyLocally()
        {
            if (string.IsNullOrEmpty(this.SN))
            {
                return(false);
            }

            try
            {
                RSAPublicKey rsaPublic       = RSAPublicKey.FromXmlString(AppHelper.STR_PUBLIC_KEY);
                byte[]       decryptBytes    = RSAHelper.Decrypt(Convert.FromBase64String(this.SN), rsaPublic);
                string       resultDecrypted = Encoding.UTF8.GetString(decryptBytes, 0, decryptBytes.Length);

                string[] sns = resultDecrypted.Split(new char[] { '|' }, 3);

                if (sns.Length == 2 || sns.Length == 3)
                {
                    var localHash = sns[1];

                    var tmp  = string.Format("{0}-{1}-{2}", this.AppId.Substring(0, 8), this.MAC, sns[0]).ToLower();
                    var data = (new EncDec()).GetMd5Hash(tmp);

                    if (data.ToLower() == localHash.ToLower())
                    {
                        if (sns.Length == 3)
                        {
                            DateTime expireDate = DateTime.Parse(sns[2]);

                            if (DateTime.Now > expireDate)
                            {
                                return(false);
                            }
                        }
                        return(true);
                    }
                    else
                    {
                        //the SN value might be modified by client user manually.
                        //Just keep the value that user modified.
                        return(false);
                    }
                }
            }
            catch (Exception)
            {
                return(false);
            }

            return(false);
        }
Example #2
0
        public static byte[] Decrypt(byte[] data, RSAPublicKey publicKey)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }

            if (publicKey == null)
            {
                throw new ArgumentNullException("publicKey");
            }

            int blockSize = publicKey.Modulus.Length;

            return(Compute(data, publicKey, blockSize));
        }
Example #3
0
        public static byte[] Sign(byte[] data, RSAPublicKey publicKey, HashAlgorithm hash)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }

            if (publicKey == null)
            {
                throw new ArgumentNullException("publicKey");
            }

            if (hash == null)
            {
                throw new ArgumentNullException("hash");
            }

            byte[] hashData  = hash.ComputeHash(data);
            byte[] signature = Encrypt(hashData, publicKey);
            return(signature);
        }
Example #4
0
        public static bool Verify(byte[] data, RSAPublicKey publicKey, HashAlgorithm hash, byte[] signature)
        {
            if (data == null)
            {
                throw new ArgumentNullException("data");
            }

            if (publicKey == null)
            {
                throw new ArgumentNullException("publicKey");
            }

            if (hash == null)
            {
                throw new ArgumentNullException("hash");
            }

            if (signature == null)
            {
                throw new ArgumentNullException("signature");
            }

            byte[] hashData = hash.ComputeHash(data);

            byte[] signatureHashData = Decrypt(signature, publicKey);

            if (signatureHashData != null && signatureHashData.Length == hashData.Length)
            {
                for (int i = 0; i < signatureHashData.Length; i++)
                {
                    if (signatureHashData[i] != hashData[i])
                    {
                        return(false);
                    }
                }
                return(true);
            }

            return(false);
        }
Example #5
0
        public static RSAPublicKey FromXmlString(string xmlString)
        {
            if (string.IsNullOrEmpty(xmlString))
            {
                return(null);
            }

            try
            {
                using (XmlReader reader = XmlReader.Create(new StringReader(xmlString)))
                {
                    if (!reader.ReadToFollowing("RSAKeyValue"))
                    {
                        return(null);
                    }

                    if (reader.LocalName != "Modulus" && !reader.ReadToFollowing("Modulus"))
                    {
                        return(null);
                    }
                    string modulus = reader.ReadElementContentAsString();

                    if (reader.LocalName != "Exponent" && !reader.ReadToFollowing("Exponent"))
                    {
                        return(null);
                    }
                    string exponent = reader.ReadElementContentAsString();

                    RSAPublicKey publicKey = new RSAPublicKey();
                    publicKey.Modulus  = Convert.FromBase64String(modulus);
                    publicKey.Exponent = Convert.FromBase64String(exponent);

                    return(publicKey);
                }
            }
            catch
            {
                return(null);
            }
        }