internal string EncodePassword(string pass)
 {
     byte[] bIn  = Encoding.UTF8.GetBytes(pass);
     byte[] bRet = Cryptographer.EncryptSymmetric("Custom Symmetric Cryptography Provider", bIn);
     return(Convert.ToBase64String(bRet));
     //return Cryptographer.EncryptSymmetric("RijndaelManaged", pass);
 }
        /// <summary>
        /// Encrypt a sample text using the default symmetric provider.
        /// </summary>
        /// <param name="sender">Event sender</param>
        /// <param name="e">Event arguments</param>
        private void encryptButton_Click(object sender, System.EventArgs e)
        {
            if (this.inputForm != null)
            {
                this.inputForm.Title            = Resources.EncryptTitleMessage;
                this.inputForm.InstructionsText = Resources.EncryptInstructionsMessage;
                if (this.inputForm.ShowDialog() == DialogResult.OK)
                {
                    try
                    {
                        this.Cursor = Cursors.WaitCursor;

                        string valueToEncrypt = this.inputForm.Input;

                        this.encryptedContentsBase64 = Cryptographer.EncryptSymmetric(symmProvider, this.inputForm.Input);

                        this.DisplayResults(string.Format(Resources.Culture, Resources.OriginalTextMessage, this.inputForm.Input));
                        this.DisplayResults(string.Format(Resources.EncryptedTextMessage, this.encryptedContentsBase64));
                    }
                    catch (Exception ex)
                    {
                        ProcessUnhandledException(ex);
                    }
                    finally
                    {
                        this.decryptButton.Enabled = true;
                        this.Cursor = Cursors.Default;
                    }
                }
            }
        }
Exemplo n.º 3
0
 ///--------------------------------------------------------------------------------
 /// <summary>This method gets an application setting as a string.</summary>
 ///
 /// <param name="valueToEncrypt">The value to encrypt.</param>
 ///
 /// <remarks>A  symmetric provider called "RijndaelManaged" must be defined through
 /// the Microsoft Enterprise Library 4.1 Configuration.</remarks>
 ///
 /// <returns>An encrypted string.</returns>
 ///--------------------------------------------------------------------------------
 public static string EncryptSymmetric(string valueToEncrypt)
 {
     if (String.IsNullOrEmpty(valueToEncrypt))
     {
         return(null);
     }
     return(Cryptographer.EncryptSymmetric("RijndaelManaged", valueToEncrypt));
 }
Exemplo n.º 4
0
        public void EncryptAndDecryptBytes()
        {
            byte[] encrypted = Cryptographer.EncryptSymmetric(symmInstance, plainTextBytes);
            Assert.IsFalse(CryptographyUtility.CompareBytes(plainTextBytes, encrypted));

            byte[] decrypted = Cryptographer.DecryptSymmetric(symmInstance, encrypted);
            Assert.IsTrue(CryptographyUtility.CompareBytes(plainTextBytes, decrypted));
        }
Exemplo n.º 5
0
        public void EncryptAndDecryptString()
        {
            string encrypted = Cryptographer.EncryptSymmetric(symmInstance, plainTextString);

            Assert.IsFalse(plainTextString == encrypted);

            string decrypted = Cryptographer.DecryptSymmetric(symmInstance, encrypted);

            Assert.IsTrue(plainTextString == decrypted);
        }
Exemplo n.º 6
0
        public void EncryptAndDecryptStringWithASymmetricAlgorithm()
        {
            string encrypted = Cryptographer.EncryptSymmetric(symmetricAlgorithm1, plainTextString);

            Assert.IsFalse(plainTextString == encrypted);

            string decrypted = Cryptographer.DecryptSymmetric(symmetricAlgorithm1, encrypted);

            Assert.IsTrue(plainTextString == decrypted);
        }
        static void Main(string[] args)
        {
            string Encrypt = Cryptographer.EncryptSymmetric("RC2CryptoServiceProvider", "Hello,World! ---你好,世界!");

            Console.WriteLine("密文:" + Encrypt);

            Console.WriteLine("---------------------------------------------------");

            Encrypt = Cryptographer.DecryptSymmetric("RC2CryptoServiceProvider", Encrypt);

            Console.WriteLine("原文:" + Encrypt);
        }
Exemplo n.º 8
0
        public void EncryptAndDecryptOneMegabyte()
        {
            byte[] megabyte = new byte[1024 * 1024];
            CryptographyUtility.GetRandomBytes(megabyte);

            byte[] encrypted = Cryptographer.EncryptSymmetric(symmInstance, megabyte);
            Assert.IsFalse(CryptographyUtility.CompareBytes(megabyte, encrypted));

            byte[] decrypted = Cryptographer.DecryptSymmetric(symmInstance, encrypted);

            Assert.IsTrue(CryptographyUtility.CompareBytes(megabyte, decrypted));
        }
Exemplo n.º 9
0
        public void EncryptAndDecryptOneByte()
        {
            byte[] onebyte = new byte[1];
            CryptographyUtility.GetRandomBytes(onebyte);

            byte[] encrypted = Cryptographer.EncryptSymmetric(symmInstance, onebyte, context);
            Assert.IsFalse(CryptographyUtility.CompareBytes(onebyte, encrypted), "enc");

            byte[] decrypted = Cryptographer.DecryptSymmetric(symmInstance, encrypted, context);

            Assert.IsTrue(CryptographyUtility.CompareBytes(onebyte, decrypted), "dec");
        }
Exemplo n.º 10
0
        /// <summary>
        /// 加密保存连接字符串
        /// </summary>
        public static void SaveConnectionStrings(string connectionStringFileName, string encryptedFileName)
        {
            AMS.Profile.IProfile profile = new AMS.Profile.Xml(encryptedFileName);

            string sKey = Cryptographer.GenerateKey();

            profile.SetValue("ConnectionStringSetting", "Key", sKey);

            string originalTxt   = System.IO.File.ReadAllText(connectionStringFileName, Encoding.UTF8);
            string encryptedText = Cryptographer.EncryptSymmetric(originalTxt, sKey);

            profile.SetValue("ConnectionStringSetting", "ConnectionString", encryptedText);
        }
Exemplo n.º 11
0
        // App.config中添加symmetricCryptoProviders节点(对称秘钥加密)
        static void test2()
        {
            string Encrypt = Cryptographer.EncryptSymmetric("DPAPI Symmetric Crypto Provider", "SensitiveData");

            Console.WriteLine("密文:" + Encrypt);

            Console.WriteLine("--------------------------------------------");

            // 测试数据篡改后效果
            //Encrypt = Encrypt.Replace("A", "B");

            Encrypt = Cryptographer.DecryptSymmetric("DPAPI Symmetric Crypto Provider", Encrypt);

            Console.WriteLine("原文:" + Encrypt);
        }
 public static string EncryptEnterprise(string str)
 {
     if (string.IsNullOrEmpty(str))
     {
         return(str);
     }
     try
     {
         return(Cryptographer.EncryptSymmetric("OLBSymmProvider", str));
     }
     catch (Exception ex)
     {
         Console.WriteLine(ex.Message);
         return(str);
     }
 }
Exemplo n.º 13
0
        public static void Main()
        {
            Console.WriteLine("Enter string to encrypt:");
            string stringToEncrypt = Console.ReadLine();

            // encrypt
            byte[] valueToEncrypt    = Encoding.Unicode.GetBytes(stringToEncrypt);
            byte[] encryptedContents = Cryptographer.EncryptSymmetric("My DPAPI Symmetric Cryptography Provider", valueToEncrypt);

            string stringToDecrypt = (new UnicodeEncoding()).GetString(encryptedContents);

            Console.WriteLine("Encrypted as \"{0}\"", stringToDecrypt);

            // decrypt
            byte[] valueToDecrypt    = Encoding.Unicode.GetBytes(stringToDecrypt);
            byte[] decryptedContents = Cryptographer.DecryptSymmetric("My DPAPI Symmetric Cryptography Provider", valueToDecrypt);
            string plainText         = (new UnicodeEncoding()).GetString(decryptedContents);

            Console.WriteLine("Decrypted to \"{0}\"", plainText);

            // hashing
            string stringValueToHash = "password";

            byte[] valueToHash   = (new UnicodeEncoding()).GetBytes(stringValueToHash);
            byte[] generatedHash = Cryptographer.CreateHash("MySHA1Managed", valueToHash);
            string hashString    = (new UnicodeEncoding()).GetString(generatedHash);

            Console.WriteLine("Hash of \"{0}\" is \"{1}\"", stringValueToHash, hashString);

            byte[] stringToCompare     = (new UnicodeEncoding()).GetBytes(stringValueToHash);
            bool   comparisonSucceeded = Cryptographer.CompareHash("MySHA1Managed", stringToCompare, generatedHash);

            Console.WriteLine("\"{0}\" hashes to \"{1}\" = {2} ", stringValueToHash, hashString, comparisonSucceeded);

            Console.Read();
        }
Exemplo n.º 14
0
 public static byte[] AESEncrypt(byte[] plainText)
 {
     return(Cryptographer.EncryptSymmetric(AESProviderName, plainText));
 }
Exemplo n.º 15
0
 public static string AESEncrypt(string plainText)
 {
     return(Cryptographer.EncryptSymmetric(AESProviderName, plainText));
 }
Exemplo n.º 16
0
 public void EncryptWithZeroLengthInstanceString()
 {
     Cryptographer.EncryptSymmetric(string.Empty, plainTextString, context);
 }
Exemplo n.º 17
0
 /// <summary>
 /// Criptografa um valor no algoritmo Simétrico de Rijndael do Enterprise Library 4. Com método definido no webconfig
 /// </summary>
 /// <param name="value">Valor em texto pleno a ser criptografado</param>
 /// <returns>Retorna um valor criptografado</returns>
 public static string EncryptSymmetric(string value)
 {
     return(Cryptographer.EncryptSymmetric("SpongeBuilderKey", value));
 }
Exemplo n.º 18
0
 public void EncryptWithNullInstance()
 {
     Cryptographer.EncryptSymmetric(null, plainTextBytes, context);
 }
Exemplo n.º 19
0
 public void EncryptWithNullInstanceString()
 {
     Cryptographer.EncryptSymmetric(null, plainTextString, context);
 }
Exemplo n.º 20
0
        /// <summary>
        /// Encrypts the XML fields.
        /// </summary>
        /// <param name="doc">The doc.</param>
        /// <param name="ahsId">The ahs id.</param>
        /// <param name="lobCd">The lob cd.</param>
        /// <param name="type">The type.</param>
        /// <param name="decrypt">if set to <c>true</c> [decrypt].</param>
        /// <returns></returns>
        static public string EncryptXmlFields(string doc, string ahsId, string lobCd, string type, bool decrypt)
        {
            var results = doc;

            try
            {
                var encryptDoc = new XmlDocument();
                encryptDoc.LoadXml(doc);
                var root = encryptDoc.DocumentElement;
                if (null != root)
                {
                    var encyptAttributes = new EncryptAttributeSet {
                        AhsId = ahsId, LobCd = lobCd, EncryptType = type
                    };
                    Assert.Test(encyptAttributes.Execute(), encyptAttributes.LastError);

                    foreach (DataRow encryptRow in encyptAttributes)
                    {
                        var attributeName = encryptRow[(int)EncryptAttributeSet.ResultList.AttributeName].ToString();
                        var nodes         = encryptDoc.SelectNodes(attributeName);

                        if (null == nodes)
                        {
                            continue;
                        }
                        foreach (XmlNode node in nodes)
                        {
                            if (string.IsNullOrEmpty(node.InnerText))
                            {
                                continue;
                            }
                            node.InnerText = decrypt ? Cryptographer.DecryptSymmetric(SymmProvider, node.InnerText) : Cryptographer.EncryptSymmetric(SymmProvider, node.InnerText);
                        }
                    }
                    results = encryptDoc.OuterXml;
                }
            }
            catch (Exception ex)
            {
                ErrorLog.LogError("EncryptXmlFields(): " + ex.Message, ServiceName);
            }
            return(results);
        }
 public void EncryptWithNullInstanceStringThrows()
 {
     Cryptographer.EncryptSymmetric(null, plainTextString);
 }
 protected override byte[] EncryptPassword(byte[] password)
 {
     return(Cryptographer.EncryptSymmetric("Custom Symmetric Cryptography Provider", password));
 }
Exemplo n.º 23
0
 /// <summary>
 /// Encrypts the value. Preserves the original value if there are problems.
 /// </summary>
 static public string EncryptValue(string symmProvider, string value)
 {
     return(!string.IsNullOrEmpty(value) ? Cryptographer.EncryptSymmetric(symmProvider, value) : value);
 }
Exemplo n.º 24
0
 /// <summary>
 /// 3DES加密
 /// </summary>
 /// <param name="encryptString"></param>
 /// <returns></returns>
 public static string TripleDESEncrypt(string encryptString)
 {
     return(Cryptographer.EncryptSymmetric("TripleDESCryptoServiceProvider", encryptString));
 }
 public void EncryptWithNullInstanceThrows()
 {
     Cryptographer.EncryptSymmetric(null, plainTextBytes);
 }
Exemplo n.º 26
0
 public byte[] EncryptSymmetric(byte[] value)
 {
     return(Cryptographer.EncryptSymmetric("EncryptionProvider", value, this.ConfigurationContext));
 }
Exemplo n.º 27
0
 /*
  *知识:
  * 1. 产生 key 文件的过程中有个选择项, 一个是用户模式,一个是机器模式。
  *   用户模式是登陆到该计算机的用户才能使用这个 key文件,机器模式当然就是只要是这个计算机上的用户都可以使用这个 key 文件。比如部署 WebForms 的WebSite 需要使用机器模式,至于WinForms的部署,如果不能确认计算机上只有一个帐户会使用你部署的软件,还是使用机器模式;
  * 2. 对称加密(也叫私钥加密)指加密和解密使用相同密钥的加密算法。有时又叫传统密码算法,就是加密密钥能够从解密密钥中推算出来,同时解密密钥也可以从加密密钥中推算出来。而在大多数的对称算法中,加密密钥和解密密钥是相同的,所以也称这种加密算法为秘密密钥算法或单密钥算法。它要求发送方和接收方在安全通信之前,商定一个密钥。对称算法的安全性依赖于密钥,泄漏密钥就意味着任何人都可以对他们发送或接收的消息解密,所以密钥的保密性对通信性至关重要。
  *
  * 3.对称加密算法的特点是算法公开、计算量小、加密速度快、加密效率高。
  *
  * 4.不足之处是,交易双方都使用同样钥匙,安全性得不到保证。此外,每对用户每次使用对称加密算法时,都需要使用其他人不知道的惟一钥匙,这会使得发收信双方所拥有的钥匙数量呈几何级数增长,密钥管理成为用户的负担。对称加密算法在分布式网络系统上使用较为困难,主要是因为密钥管理困难,使用成本较高。而与公开密钥加密算法比起来,对称加密算法能够提供加密和认证却缺乏了签名功能,使得使用范围有所缩小。在计算机专网系统中广泛使用的对称加密算法有DES和IDEA等。美国国家标准局倡导的AES即将作为新标准取代DES。
  *
  * 5.对称加密具体算法:DES算法,3DES算法,TDEA算法,Blowfish算法,RC5算法,IDEA算法。
  *
  *
  */
 /// <summary>
 /// 对称加密
 /// </summary>
 /// <param name="symmetricName">对称加密方式</param>
 /// <param name="encrytData">需要加密的字符串</param>
 /// <returns>加密字符串</returns>
 public static string EncryptSymmetric(string symmetricName, string encrytData)
 {
     return(Cryptographer.EncryptSymmetric(symmetricName, encrytData));
 }
Exemplo n.º 28
0
 public static string Encrypt(string plaintext)
 {
     return(Cryptographer.EncryptSymmetric("ForTestingProvider", plaintext));
 }
 public void EncryptWithInvalidInstanceThrows()
 {
     Cryptographer.EncryptSymmetric("invalid instance", plainTextString);
 }
 public void EncryptWithZeroLengthInstanceThrows()
 {
     Cryptographer.EncryptSymmetric(string.Empty, plainTextBytes);
 }