Пример #1
0
 private void btnEncrypt_Click(object sender, EventArgs e)
 {
     if (!string.IsNullOrEmpty(textBox1.Text))
     {
         textBox2.Text = AsymmetricEncryption.EncryptText(textBox1.Text, keySize, publicKey);
     }
 }
Пример #2
0
        public static string RSAEncrypt(this string s)
        {
            const int keySize    = 2048;
            var       key        = AsymmetricEncryption.PemToXml(Settings.RSAKey);
            var       base64Card = Convert.ToBase64String(Encoding.UTF8.GetBytes(s));

            return(AsymmetricEncryption.EncryptText(base64Card, keySize, key));
        }
Пример #3
0
        public JsonResult AsymmetricEncrypt(WebSecurity.Models.EncryptionModel encryption, string ButtonType)
        {
            RSACryptoServiceProvider rsa = new RSACryptoServiceProvider();

            string publicKey  = string.Empty; // false to get the public key
            string privateKey = string.Empty; // true to get the private key

            if (!string.IsNullOrEmpty(encryption.PublicKey) && !string.IsNullOrEmpty(encryption.PrivateKey))
            {
                publicKey  = HttpUtility.HtmlDecode(encryption.PublicKey);
                privateKey = HttpUtility.HtmlDecode(encryption.PrivateKey);
            }
            else
            {
                publicKey  = rsa.ToXmlString(false); // false to get the public key
                privateKey = rsa.ToXmlString(true);  // true to get the private key
            }

            var result = new EncryptionModel();

            var asymEncryption = new AsymmetricEncryption();

            if (ButtonType == "Encrypt")
            {
                var encryptedText = asymEncryption.EncryptText(publicKey, encryption.PlainText);
                result = new EncryptionModel()
                {
                    EncryptedText = encryptedText,
                    PlainText     = encryption.PlainText,
                    PublicKey     = HttpUtility.HtmlEncode(publicKey),
                    PrivateKey    = HttpUtility.HtmlEncode(privateKey)
                };
            }
            else if (ButtonType == "Decrypt")
            {
                var plainText = asymEncryption.DecryptData(HttpUtility.HtmlDecode(encryption.PrivateKey), encryption.EncryptedText);

                result = new EncryptionModel()
                {
                    EncryptedText = encryption.EncryptedText,
                    PlainText     = plainText,
                    PublicKey     = HttpUtility.HtmlEncode(publicKey),
                    PrivateKey    = HttpUtility.HtmlEncode(privateKey)
                };
            }

            return(Json(result));
        }
Пример #4
0
        private static void ForTempSave(string userName)
        {
            const int keySize = 2048;
            string    publicAndPrivateKey;
            string    publicKey;
            var       root = new XElement("Root");

            root.SetAttributeValue(Constants._ID, Guid.NewGuid().ToString());
            AsymmetricEncryption.GenerateKeys(keySize, out publicKey, out publicAndPrivateKey);
            Console.WriteLine(@"public key:" + publicKey);
            Console.WriteLine(@"public & private key:" + publicAndPrivateKey);
            root.SetAttributeValue("PublicKey", publicKey);
            root.SetAttributeValue("PublicAndPrivateKey", publicAndPrivateKey);
            var productid = AsymmetricEncryption.GetProductId();

            root.SetAttributeValue("ProductId", productid);

            //string userName = "******";
            var text      = userName + productid;
            var encrypted = AsymmetricEncryption.EncryptText(text, keySize, publicKey);

            Console.WriteLine(@"Encrypted: {0}", encrypted);

            //send encrypted data to service
            File.WriteAllText(userName + ".pem",
                              @"UserName:
"******"
Public Key:" + publicKey + @"
Public and Private Key:
" +
                              publicAndPrivateKey + @"Secrect:
" + encrypted + @"
For Test:
" + productid);
            var decrypted = AsymmetricEncryption.DecryptText(encrypted, keySize, publicAndPrivateKey);

            //service person do below
            Console.WriteLine(@"Decrypted: {0}", decrypted);

            //            Configuration.Set("UserName", userName);
            //            Configuration.Set("PublicKey", publicKey);
            //            Configuration.SaveSettings();
            DBFactory.GetData().Save(root);
            Console.WriteLine(DBFactory.GetData().Read("a02cf4ad-ba0c-4c69-9f7c-e7d73a8fecad"));
        }
Пример #5
0
        String EncryptToServer(String text)
        {
            String encrypted = AsymmetricEncryption.EncryptText(text, KEYSIZE, _publicServerKey);

            return(encrypted);
        }
Пример #6
0
        String EncryptToClient(String text)
        {
            String encrypted = AsymmetricEncryption.EncryptText(text, KEYSIZE, _publicClientKey);

            return(encrypted);
        }