Example #1
0
        public static string SignDataWithPrivateKey(string strData,
                                                    string keyPrivate,
                                                    string hashAlgorithm,
                                                    out string error)
        {
            try
            {
                Chilkat.Rsa rsa = new Chilkat.Rsa();

                rsa.ImportPrivateKey(keyPrivate);

                // sign a string, and receive the signature in a hex-encoded string.
                // Therefore, set the encoding mode to "hex":
                rsa.EncodingMode = "hex";

                // It is important to match the byte-ordering.
                // The LittleEndian property may be set to true for little-endian byte ordering,
                // or false  for big-endian byte ordering.
                // Microsoft apps typically use little-endian, while
                // OpenSSL and other services (such as Amazon CloudFront) use big-endian.
                rsa.LittleEndian = false;

                // Sign the string using the  md5 hash algorithm.
                // Other valid choices are "md2", "sha256", "sha384",
                // "sha512", and "sha-1".
                string hexSignature = rsa.SignStringENC(strData, hashAlgorithm);
                error = string.Empty;
                return(hexSignature);
            }
            catch (Exception ex)
            {
                error = ex.Message;
                return(null);
            }
        }
Example #2
0
        public static String CreateSignature(String fileContent)
        {
            signatureRsa = new Chilkat.Rsa();

            signatureRsa.UnlockComponent("30-day trial");

            signatureRsa.GenerateKey(1024);


            signatureRsa.EncodingMode = "hex";
            signatureRsa.LittleEndian = false;

            return(signatureRsa.SignStringENC(fileContent, "sha-256"));
        }
Example #3
0
        static void RSAChilat(string Data)
        {
            Chilkat.PrivateKey l_key = new Chilkat.PrivateKey();
            bool   l_res             = l_key.LoadPemFile(@"D:\Work\Other\ForexStars\_incomming\project1074.ppk");
            string l_pr_key_xml      = l_key.GetXml();


            Chilkat.Rsa rsa = new Chilkat.Rsa();
            rsa.ImportPrivateKey(l_pr_key_xml);
            //rsa.UnlockComponent("30-day trial");
            rsa.UnlockComponent("RSA$TEAM$BEAN_495C86FD5RkU");
            rsa.EncodingMode = "hex";
            rsa.LittleEndian = false;
            string l_sign = rsa.SignStringENC(Data, "sha-1");
        }
Example #4
0
        static string HttpGet(string url)
        {
            bool           success = false;
            HttpWebRequest req     = WebRequest.Create(url) as HttpWebRequest;

            string Token = String.Format("{0:yyyy-MM-dd HH:mm:ss}", new DateTime(2010, 02, 02, 21, 15, 0));

            //SIGN
            Chilkat.PrivateKey privkey1 = new Chilkat.PrivateKey();
            privkey1.LoadPem(mykey);
            Chilkat.Rsa rsa1 = new Chilkat.Rsa();
            success           = rsa1.UnlockComponent("HAFSJORSA_K36nxU3n1Yui");
            success           = rsa1.ImportPrivateKey(privkey1.GetXml());
            rsa1.EncodingMode = "base64";
            rsa1.Charset      = "ANSI";
            rsa1.LittleEndian = false;
            rsa1.OaepPadding  = false;
            string hexSig = rsa1.SignStringENC(Token, "sha-1");


            //VERIFY
            Chilkat.Cert cert2 = new Chilkat.Cert();
            success = cert2.LoadFromBase64(mycert);
            Chilkat.PublicKey pubKey2 = null;
            pubKey2 = cert2.ExportPublicKey();
            Chilkat.Rsa rsa2 = new Chilkat.Rsa();
            success           = rsa2.ImportPublicKey(pubKey2.GetXml());
            rsa2.EncodingMode = "base64";
            rsa2.Charset      = "ANSI";
            rsa2.LittleEndian = false;
            rsa2.OaepPadding  = false;
            success           = rsa2.VerifyStringENC(Token, "sha-1", hexSig);

            req.Headers.Add("Token", Token);
            req.Headers.Add("Signature", hexSig);

            //ENCRYPT
            Chilkat.Cert cert3 = new Chilkat.Cert();
            success = cert3.LoadFromBase64(mycert);
            Chilkat.PublicKey pubKey3 = null;
            pubKey3 = cert3.ExportPublicKey();
            Chilkat.Rsa rsa3 = new Chilkat.Rsa();
            success           = rsa3.UnlockComponent("HAFSJORSA_K36nxU3n1Yui");
            rsa3.EncodingMode = "base64";
            rsa3.Charset      = "ANSI";
            rsa3.LittleEndian = true;
            rsa3.OaepPadding  = false;
            rsa3.ImportPublicKey(pubKey3.GetXml());
            bool usePrivateKey = false;

            System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();
            byte[] TokenArr = encoding.GetBytes(Token);
            //byte[] encryptedArr = rsa3.EncryptBytes(TokenArr, usePrivateKey);
            string encryptedstr = rsa3.EncryptBytesENC(TokenArr, usePrivateKey);

            //DECRYPT
            Chilkat.Rsa rsa4 = new Chilkat.Rsa();
            rsa4.EncodingMode = "base64";
            rsa4.Charset      = "ANSI";
            rsa4.LittleEndian = true;
            rsa4.OaepPadding  = false;
            rsa4.ImportPrivateKey(privkey1.GetXml());
            usePrivateKey = true;
            //encryptedStr = "XJ65xTR/xvD2N9xBKyKPqPijqTAyJuVtOlbaFUIboJaEPHH9pv+Lhrd5o6MSwKF6TeXs6hVsKnj8jVeYFYoEDgJS95GqaaUomWBhEZYchOp/6dn3ZxCeQoljAWLt6m4C829R9b5JYatYar9YV0d+QV+jVWE4U0rlNrkTqtA02Qw4ztN4/oehgCISrBnc81N1MYNwG9vrTHSVM6tSUWjWxMRubpOBvqKqOxyA9fpJNHgUyzio2X1cp12K++1GEUWNWyYVhTiBr/QM3mUN67mHcn0vvWZvmPhYlIaVn9DqIvVdMbHRbLwrCczFgY4PdHrhcH9yDTlkkAbKUatgDQiI4w==";
            //encryptedStr = "6KQbxh+x5SGIzD89zEwj+/IVVCBocemCXWl1mr+mk9wxRMydCfmMSUHDOafnqiJ6GAJapKbLTHOc9d1OyWTwsp5BQBT5VM20hb9r+AkDrHwkgL06ifizP0gTEO17cyO95jwlRXOfkQKb3cERLBEtOAnRep4bKMSsPLyxRRBX5VT4d19yxRor2V9js0CEFONinxl7qRxjckwvQk53+qpxeQ8jOx+pmrQukX7nWkMajWi+ZFndyfLL3LfRBYZKN2R0vdrnSMKdkxUEUUJybsv4QCMWshNpQznPSantq2dKNe07eB5mX4fRufy4mY4qjqBlf8+XFKdD+J37C6r3THL6pw==";
            //string decryptedStr = rsa4.DecryptStringENC(encryptedStr, usePrivateKey);



            Chilkat.Crypt2 crypt = new Chilkat.Crypt2();
            success              = crypt.UnlockComponent("HAFSJOCrypt_0xo09cJWVQAw");
            crypt.EncodingMode   = "base64";
            crypt.CryptAlgorithm = "none";
            req.Headers.Add("authorization", "Basic " + crypt.EncryptStringENC("Mogens:Hafsjold"));

            string result = null;

            using (HttpWebResponse resp = req.GetResponse() as HttpWebResponse)
            {
                StreamReader reader = new StreamReader(resp.GetResponseStream());
                result = reader.ReadToEnd();
            }
            return(result);
        }