Inheritance: System.Security.Cryptography.RSA
Example #1
0
        private string SendData(string url, string productKey, Dictionary<string, string> dataParams, Dictionary<string, string> prvDataParams)
        {
            // private params put in xml format and encrypt
            string prvData = null;
            if (prvDataParams.Count > 0) {
                prvData += "<data>";
                foreach (string paramName in prvDataParams.Keys) {
                    prvData += "<" + paramName + ">" + prvDataParams[paramName] + "</" + paramName + ">";
                }
                prvData += "</data>";

                EZRSA ezrsa_public = new EZRSA(1024);
                ezrsa_public.FromXmlString(productKey);

                string encrypted = "";
                int chunkSize = 40;
                for (int i = 0; i < prvData.Length / chunkSize; i++) {
                    byte[] encryptedBytes = ezrsa_public.Encrypt(Encoding.Unicode.GetBytes(prvData.Substring(i * chunkSize, chunkSize)), false);
                    // convert to hexa string
                    for (int j = 0; j < encryptedBytes.Length; ++j) {
                        encrypted += encryptedBytes[j].ToString("X2");
                    }
                }
                if (prvData.Length % chunkSize != 0) {
                    byte[] encryptedBytes = ezrsa_public.Encrypt(Encoding.Unicode.GetBytes(prvData.Substring(prvData.Length - prvData.Length % chunkSize)), false);
                    for (int j = 0; j < encryptedBytes.Length; ++j) {
                        encrypted += encryptedBytes[j].ToString("X2");
                    }
                }
                prvData = encrypted;
            }

            // fill post data (and include private data above)
            string postData = "";
            foreach (string paramName in dataParams.Keys) {
                postData += paramName + "=" + dataParams[paramName] + "&";
            }
            if (prvData != null) {
                postData += "prvdata=" + prvData;
            }
            if (postData[postData.Length - 1] == '&') postData = postData.Substring(0, postData.Length - 1);

            System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
            byte[] data = encoding.GetBytes(postData);

            HttpWebRequest httpRequest = (HttpWebRequest)WebRequest.Create(url);
            httpRequest.Method = "POST";
            httpRequest.ContentType = "application/x-www-form-urlencoded";
            httpRequest.ContentLength = data.Length;
            httpRequest.Timeout = 20 * 1000;
            System.IO.Stream newStream = httpRequest.GetRequestStream();

            // Send the data.
            newStream.Write(data, 0, data.Length);
            newStream.Close();

            HttpWebResponse response = (HttpWebResponse)httpRequest.GetResponse();
            System.IO.StreamReader reader = new System.IO.StreamReader(response.GetResponseStream());
            string responseText = reader.ReadToEnd();
            response.Close();

            return responseText.Trim();
        }
        public bool IsValid(string productCode, string versionCode)
        {
            //System.Diagnostics.Stopwatch timer = new System.Diagnostics.Stopwatch();
            //timer.Start();

            // always pass BaseProductVerions in activation code IsValid
            if (RegCode == null || RegCode.IsExpired())
                return false;

            EZRSA ezrsa_public = new EZRSA(1024);
            ezrsa_public.FromXmlString(ProductKey);

            byte[] signatureBytes = new byte[128];
            for (int i = 0; i < 128; i++) {
                signatureBytes[i] = Convert.ToByte(ActivationCode.Substring(i * 2, 2), 16);
            }

            return ezrsa_public.VerifyData(Encoding.Unicode.GetBytes(Host + RegistrationCode + RegCode.R + productCode + versionCode), new SHA1CryptoServiceProvider(), signatureBytes);
        }