当前位置: 首页>>代码示例>>C#>>正文


C# Cryptography.PasswordDeriveBytes类代码示例

本文整理汇总了C#中System.Security.Cryptography.PasswordDeriveBytes的典型用法代码示例。如果您正苦于以下问题:C# PasswordDeriveBytes类的具体用法?C# PasswordDeriveBytes怎么用?C# PasswordDeriveBytes使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。


PasswordDeriveBytes类属于System.Security.Cryptography命名空间,在下文中一共展示了PasswordDeriveBytes类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: DecryptString

 protected static string DecryptString(string InputText, string Password)
 {
     try
     {
         RijndaelManaged RijndaelCipher = new RijndaelManaged();
         byte[] EncryptedData = Convert.FromBase64String(InputText);
         byte[] Salt = Encoding.ASCII.GetBytes(Password.Length.ToString());
         PasswordDeriveBytes SecretKey = new PasswordDeriveBytes(Password, Salt);
         // Create a decryptor from the existing SecretKey bytes.
         ICryptoTransform Decryptor = RijndaelCipher.CreateDecryptor(SecretKey.GetBytes(16), SecretKey.GetBytes(16));
         MemoryStream memoryStream = new MemoryStream(EncryptedData);
         // Create a CryptoStream. (always use Read mode for decryption).
         CryptoStream cryptoStream = new CryptoStream(memoryStream, Decryptor, CryptoStreamMode.Read);
         // Since at this point we don't know what the size of decrypted data
         // will be, allocate the buffer long enough to hold EncryptedData;
         // DecryptedData is never longer than EncryptedData.
         byte[] PlainText = new byte[EncryptedData.Length];
         // Start decrypting.
         int DecryptedCount = cryptoStream.Read(PlainText, 0, PlainText.Length);
         memoryStream.Close();
         cryptoStream.Close();
         // Convert decrypted data into a string.
         string DecryptedData = Encoding.Unicode.GetString(PlainText, 0, DecryptedCount);
         // Return decrypted string.
         return DecryptedData;
     }
     catch (Exception exception)
     {
         return (exception.Message);
     }
 }
开发者ID:balajivit1,项目名称:Sourcecode,代码行数:31,代码来源:ViewAdminRequests.cs

示例2: Encrypt

 public static string Encrypt(string plainText, string passPhrase)
 {
     byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
     using (PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, null))
     {
         byte[] keyBytes = password.GetBytes(keysize / 8);
         using (RijndaelManaged symmetricKey = new RijndaelManaged())
         {
             symmetricKey.Mode = CipherMode.CBC;
             using (ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes))
             {
                 using (MemoryStream memoryStream = new MemoryStream())
                 {
                     using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
                     {
                         cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
                         cryptoStream.FlushFinalBlock();
                         byte[] cipherTextBytes = memoryStream.ToArray();
                         return Convert.ToBase64String(cipherTextBytes);
                     }
                 }
             }
         }
     }
 }
开发者ID:FilipeDominguesGit,项目名称:GeoserverManager,代码行数:25,代码来源:HashUtils.cs

示例3: Encrypt

        /// <summary>
        /// Encrypts a string
        /// </summary>
        /// <param name="plaintext">Text to be encrypted</param>
        /// <param name="password">Password to encrypt with</param>
        /// <param name="salt">Salt to encrypt with</param>
        /// <returns>An encrypted string</returns>
        public static string Encrypt(string plaintext, string password, string salt)
        {
            string Result = "";

            PasswordDeriveBytes DerivedPassword = new PasswordDeriveBytes(Encoding.ASCII.GetBytes(password), Encoding.ASCII.GetBytes(salt), "SHA512", 12345);

            using (RijndaelManaged SymmetricKey = new RijndaelManaged())
            {
                SymmetricKey.Mode = CipherMode.CBC;
                using (ICryptoTransform Encryptor = SymmetricKey.CreateEncryptor(DerivedPassword.GetBytes(32), DerivedPassword.GetBytes(16)))
                {
                    using (MemoryStream MemStream = new MemoryStream())
                    {
                        using (CryptoStream CryptoStream = new CryptoStream(MemStream, Encryptor, CryptoStreamMode.Write))
                        {
                            byte[] PlainTextBytes = Encoding.ASCII.GetBytes(plaintext);
                            CryptoStream.Write(PlainTextBytes, 0, PlainTextBytes.Length);
                            CryptoStream.FlushFinalBlock();
                            Result = Convert.ToBase64String(MemStream.ToArray());
                        }
                    }
                }
            }

            return Result;
        }
开发者ID:Robin--,项目名称:RMLib,代码行数:33,代码来源:AES.cs

示例4: Ctor_PasswordSalt

	public void Ctor_PasswordSalt ()
	{
		PasswordDeriveBytes pdb = new PasswordDeriveBytes ("s3kr3t", salt);
		Assert.AreEqual ("SHA1", pdb.HashName, "HashName");
		Assert.AreEqual (100, pdb.IterationCount, "IterationCount");
		Assert.AreEqual (ssalt, BitConverter.ToString (pdb.Salt), "Salt");
	}
开发者ID:Profit0004,项目名称:mono,代码行数:7,代码来源:PasswordDeriveBytesTest.cs

示例5: Ctor_PasswordSaltNull

	public void Ctor_PasswordSaltNull ()
	{
		PasswordDeriveBytes pdb = new PasswordDeriveBytes ("s3kr3t", null);
		Assert.AreEqual ("SHA1", pdb.HashName, "HashName");
		Assert.AreEqual (100, pdb.IterationCount, "IterationCount");
		Assert.IsNull (pdb.Salt, "Salt");
	}
开发者ID:Profit0004,项目名称:mono,代码行数:7,代码来源:PasswordDeriveBytesTest.cs

示例6: Decrypt

        public static string Decrypt(string text)
        {
            string decryptedData = null;

            if (!string.IsNullOrEmpty(text)) {
                var rijndaelCipher = new RijndaelManaged();
                byte[] encryptedData = Convert.FromBase64String(text);
                byte[] salt = Encoding.ASCII.GetBytes(Password.Length.ToString());

                //Making of the key for decryption
                var secretKey = new PasswordDeriveBytes(Password, salt);

                //Creates a symmetric Rijndael decryptor object.
                ICryptoTransform decryptor = rijndaelCipher.CreateDecryptor(secretKey.GetBytes(32), secretKey.GetBytes(16));
                var memoryStream = new MemoryStream(encryptedData);

                //Defines the cryptographics stream for decryption.THe stream contains decrpted data
                var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
                byte[] plainText = new byte[encryptedData.Length];
                int decryptedCount = cryptoStream.Read(plainText, 0, plainText.Length);
                memoryStream.Close();
                cryptoStream.Close();

                //Converting to string
                decryptedData = Encoding.Unicode.GetString(plainText, 0, decryptedCount);
            }

            return decryptedData;
        }
开发者ID:Antoniotoress1992,项目名称:asp,代码行数:29,代码来源:SecurityManager.cs

示例7: Encrypt

        public static string Encrypt(string plainText)
        {
            var registryKey =
                Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\VisualStudio\10.0\PbsEncryptionKey");
            var password = (string)registryKey.GetValue("Key");
            var passwordDerivedBytes = new PasswordDeriveBytes(password, _salt);
            var plainTextBytes = Encoding.Unicode.GetBytes(plainText);

            using (var memoryStream = new MemoryStream())
            {
                using (Rijndael rijndael = Rijndael.Create())
                {
                    using (
                        ICryptoTransform cryptoTransform = rijndael.CreateEncryptor(passwordDerivedBytes.GetBytes(32), rgbIV: passwordDerivedBytes.GetBytes(16)))
                    {
                        using (
                            var cryptoStream = new CryptoStream(memoryStream, cryptoTransform,
                                CryptoStreamMode.Write))
                        {
                            cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
                            cryptoStream.FlushFinalBlock();
                            return Convert.ToBase64String(memoryStream.ToArray());
                        }
                    }
                }
            }
        }
开发者ID:mnasif786,项目名称:Business-Safe,代码行数:27,代码来源:RijndaelEncryptor.cs

示例8: Decrypt

        // Decrypt a string into a string using a password
        //    Uses Decrypt(byte[], byte[], byte[])
        public static string Decrypt(string cipherText, string Password)
        {
            // First we need to turn the input string into a byte array.
            // We presume that Base64 encoding was used
            byte[] cipherBytes = Convert.FromBase64String(cipherText);

            // Then, we need to turn the password into Key and IV
            // We are using salt to make it harder to guess our key using a dictionary attack -
            // trying to guess a password by enumerating all possible words.
            PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password,
                                                              new byte[]
                                                                  {
                                                                      0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76,
                                                                      0x65, 0x64, 0x65, 0x76
                                                                  });

            // Now get the key/IV and do the decryption using the function that accepts byte arrays.
            // Using PasswordDeriveBytes object we are first getting 32 bytes for the Key
            // (the default Rijndael key length is 256bit = 32bytes) and then 16 bytes for the IV.
            // IV should always be the block size, which is by default 16 bytes (128 bit) for Rijndael.
            // If you are using DES/TripleDES/RC2 the block size is 8 bytes and so should be the IV size.
            // You can also read KeySize/BlockSize properties off the algorithm to find out the sizes.
            byte[] decryptedData = Decrypt(cipherBytes, pdb.GetBytes(32), pdb.GetBytes(16));

            // Now we need to turn the resulting byte array into a string.
            // A common mistake would be to use an Encoding class for that. It does not work
            // because not all byte values can be represented by characters.
            // We are going to be using Base64 encoding that is designedexactly for what we are
            // trying to do.
            return System.Text.Encoding.Unicode.GetString(decryptedData);
        }
开发者ID:tonix,项目名称:Onet,代码行数:33,代码来源:Cryptography.cs

示例9: Decrypt

        public string Decrypt(string cipherText, string password)
        {
            if (string.IsNullOrEmpty(cipherText))
                return cipherText;
            try
            {
                if (System.Web.HttpContext.Current != null)
                    cipherText = System.Web.HttpContext.Current.Server.UrlDecode(cipherText);

                cipherText = cipherText.Replace(" ", "+");
                byte[] cipherBytes = Convert.FromBase64String(cipherText);

                PasswordDeriveBytes pdb = new PasswordDeriveBytes(password,
                    new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65,
            0x64, 0x76, 0x65, 0x64, 0x65, 0x76});

                byte[] decryptedData = Decrypt(cipherBytes,
                    pdb.GetBytes(32), pdb.GetBytes(16));

                return System.Text.Encoding.Unicode.GetString(decryptedData);
            }
            catch
            {
                return string.Empty;
            }
        }
开发者ID:phtmahajan,项目名称:ShoppingCart,代码行数:26,代码来源:UtilEncryptDecrypt.cs

示例10: Decrypt

        /// <summary>
        /// Decrypts a string
        /// </summary>
        /// <param name="cipherText">Text to be decrypted</param>
        /// <param name="password">Password to decrypt with</param>
        /// <param name="salt">Salt to decrypt with</param>
        /// <param name="hashAlgorithm">Can be either SHA1 or MD5</param>
        /// <param name="passwordIterations">Number of iterations to do. The number of times the algorithm is run on the text. </param>
        /// <param name="initialVector">Needs to be 16 ASCII characters long</param>
        /// <param name="keySize">Can be 128, 192, or 256</param>
        /// <returns>A decrypted string</returns>
        public static string Decrypt(string cipherText, string password, string salt = "69ad1bfbd6605f3f6a3011460cdfb9db7757e0f9", string hashAlgorithm = "SHA1", int passwordIterations = 2, string initialVector = "OFRna73m*aze01xY", int keySize = 256)
        {
            if (string.IsNullOrEmpty(cipherText))
                return "";

            byte[] initialVectorBytes = Encoding.ASCII.GetBytes(initialVector);
            byte[] saltValueBytes = Encoding.ASCII.GetBytes(salt);
            byte[] cipherTextBytes = Convert.FromBase64String(cipherText);
            var derivedPassword = new PasswordDeriveBytes(password, saltValueBytes, hashAlgorithm, passwordIterations);
            byte[] keyBytes = derivedPassword.GetBytes(keySize / 8);
            var symmetricKey = new RijndaelManaged {Mode = CipherMode.CBC};
            byte[] plainTextBytes = new byte[cipherTextBytes.Length];
            int byteCount;

            using (ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, initialVectorBytes))
            {
                using (MemoryStream memStream = new MemoryStream(cipherTextBytes))
                {
                    using (CryptoStream cryptoStream = new CryptoStream(memStream, decryptor, CryptoStreamMode.Read))
                    {
                        byteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
                        memStream.Close();
                        cryptoStream.Close();
                    }
                }
            }
            symmetricKey.Clear();

            return Encoding.UTF8.GetString(plainTextBytes, 0, byteCount);
        }
开发者ID:Christind,项目名称:ucn-3semproject-dm79-group1,代码行数:41,代码来源:EncryptionHelper.cs

示例11: Encrypt

        public static string Encrypt(string plainText)
        {
            // Encryption operates on byte arrays, not on strings.
            byte[] plainTextBytes =
              System.Text.Encoding.Unicode.GetBytes(plainText);

            // Derive a key from the password.
            PasswordDeriveBytes passwordDerivedBytes = new PasswordDeriveBytes(NOT_SECRET_KEY,
                new byte[] {0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76});

            // Use Rijndael symmetric algorithm to do the encryption.
            Rijndael rijndaelAlgorithm = Rijndael.Create();
            rijndaelAlgorithm.Key = passwordDerivedBytes.GetBytes(32);
            rijndaelAlgorithm.IV = passwordDerivedBytes.GetBytes(16);

            MemoryStream memoryStream = new MemoryStream();

            CryptoStream cryptoStream = new CryptoStream(memoryStream, rijndaelAlgorithm.CreateEncryptor(), CryptoStreamMode.Write);
            cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
            cryptoStream.Close();

            byte[] encryptedBytes = memoryStream.ToArray();

            return Convert.ToBase64String(encryptedBytes);
        }
开发者ID:SpivEgin,项目名称:hmailserver,代码行数:25,代码来源:Encryption.cs

示例12: Decrypt

        /// <summary>
        /// Decrypts a string which was encrypted using this class
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public String Decrypt(String text)
        {
            byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
            byte[] saltValueBytes = Encoding.ASCII.GetBytes(salt);
            byte[] cipherTextBytes = Convert.FromBase64String(text);

            PasswordDeriveBytes password = new PasswordDeriveBytes(key,saltValueBytes,"SHA1",1);
            byte[] keyBytes = password.GetBytes(keySize / 8);

            RijndaelManaged symmetricKey = new RijndaelManaged();
            symmetricKey.Mode = CipherMode.CBC;

            ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes,initVectorBytes);
            MemoryStream memoryStream = new MemoryStream(cipherTextBytes);
            CryptoStream cryptoStream = new CryptoStream(memoryStream,decryptor,CryptoStreamMode.Read);

            byte[] plainTextBytes = new byte[cipherTextBytes.Length];
            int decryptedByteCount = cryptoStream.Read(plainTextBytes,0,plainTextBytes.Length);

            memoryStream.Close();
            cryptoStream.Close();

            string plainText = Encoding.UTF8.GetString(plainTextBytes,0,decryptedByteCount);
            return plainText;
        }
开发者ID:beachead,项目名称:gooey-cms-v2,代码行数:30,代码来源:TextEncryption.cs

示例13: Encrypt

 public static string Encrypt(string password, string key)
 {
     byte[] clearBytes = System.Text.Encoding.Unicode.GetBytes(password);
     PasswordDeriveBytes pdb = new PasswordDeriveBytes(key, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
     byte[] encryptedData = Encrypt(clearBytes, pdb.GetBytes(32), pdb.GetBytes(16));
     return Convert.ToBase64String(encryptedData);
 }
开发者ID:kennygohcl,项目名称:AppWizAdmin,代码行数:7,代码来源:Util.cs

示例14: Decrypt

 public static string Decrypt(string cipherTextPassword, string key)
 {
     byte[] cipherBytes = Convert.FromBase64String(cipherTextPassword);
     PasswordDeriveBytes pdb = new PasswordDeriveBytes(key, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });
     byte[] decryptedData = Decrypt(cipherBytes, pdb.GetBytes(32), pdb.GetBytes(16));
     return System.Text.Encoding.Unicode.GetString(decryptedData);
 }
开发者ID:kennygohcl,项目名称:AppWizAdmin,代码行数:7,代码来源:Util.cs

示例15: Decrypt

 public static string Decrypt(string cipherText, string Password)
 {
     byte[] cipherData = Convert.FromBase64String(cipherText);
       PasswordDeriveBytes bytes = new PasswordDeriveBytes(Password, new byte[] { 0x49, 0x76, 0x61, 110, 0x20, 0x4d, 0x65, 100, 0x76, 0x65, 100, 0x65, 0x76 });
       byte[] buffer2 = Decrypt(cipherData, bytes.GetBytes(0x20), bytes.GetBytes(0x10));
       return Encoding.Unicode.GetString(buffer2);
 }
开发者ID:AdamCarrick,项目名称:inbefore404,代码行数:7,代码来源:EncDec.cs


注:本文中的System.Security.Cryptography.PasswordDeriveBytes类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。