• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

C# Cryptography.PasswordDeriveBytes类代码示例

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

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



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


示例16: Encrypt

        /// <summary>
        /// Encrypt a string into a string using a password. Uses Encrypt(byte[], byte[], byte[]) 
        /// </summary>
        public static string Encrypt(string clearText, string Password, bool useUrlEncoding)
        {
            // First we need to turn the input string into a byte array.
            byte[] clearBytes = System.Text.Encoding.Unicode.GetBytes(clearText);

            // 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 encryption 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[] encryptedData = Encrypt(clearBytes, 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 designed exactly for what we are trying to do.
            string data = HttpUtility.UrlEncode(Convert.ToBase64String(encryptedData));

            // Optionally URL encode the encrypted data (use if data will be put in URL as URL parameter/querystring).
            if (useUrlEncoding) {
                data = HttpUtility.UrlEncode(data );
            }

            return data;
        }
开发者ID:bartvanderwal,项目名称:Hre,代码行数:36,代码来源:EncDec.cs


示例17: Encrypt

 public string Encrypt(string Data, string Password, int Bits)
 {
     byte[] byteArray2;
     byte[] byteArray3;
     if (this._userKey != "bengkel1q2w3e")
     {
         return "Hubungi Admin";
     }
     byte[] byteArray1 = Encoding.Unicode.GetBytes(Data);
     PasswordDeriveBytes passwordDeriveBytes1 = new PasswordDeriveBytes(Password, new byte[] { byte.MinValue, 1, 2, 28, 29, 30, 3, 4, 5, 15, 32, 33, 173, 175, 164 });
     if (Bits == 128)
     {
         byteArray2 = this.Encrypt(byteArray1, passwordDeriveBytes1.GetBytes(16), passwordDeriveBytes1.GetBytes(16));
         return Convert.ToBase64String(byteArray2);
     }
     if (Bits == 192)
     {
         byteArray3 = this.Encrypt(byteArray1, passwordDeriveBytes1.GetBytes(24), passwordDeriveBytes1.GetBytes(16));
         return Convert.ToBase64String(byteArray3);
     }
     if (Bits != 256)
     {
         return string.Concat(Bits);
     }
     byte[] byteArray4 = this.Encrypt(byteArray1, passwordDeriveBytes1.GetBytes(32), passwordDeriveBytes1.GetBytes(16));
     return Convert.ToBase64String(byteArray4);
 }
开发者ID:rhizalpatrax64bit,项目名称:logistic-management-system,代码行数:27,代码来源:AE_Encrypt.cs


示例18: 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>
		/// <param name="InitialVector">Needs to be 16 ASCII characters long</param>
		/// <param name="HashAlgorithm">Can be either SHA1 or MD5</param>
		/// <param name="PasswordIterations">Number of iterations to do</param>
		/// <param name="KeySize">Can be 128, 192, or 256</param>
		/// <returns>An encrypted bytes</returns>
		public static byte[] Encrypt(byte[] PlainText, byte[] Password, byte[] InitialVector,
			byte[] Salt)
		{
			PasswordDeriveBytes DerivedPassword = new PasswordDeriveBytes(Password, Salt, HASH_ALGORITHM, PASSWORDITERATIONS);
			byte[] KeyBytes = DerivedPassword.GetBytes(KEY_SIZE / 8);
			RijndaelManaged SymmetricKey = new RijndaelManaged();
			SymmetricKey.Mode = CipherMode.CBC;
			byte[] CipherTextBytes = null;

			using (ICryptoTransform Encryptor = SymmetricKey.CreateEncryptor(KeyBytes, InitialVector))
			{
				using (MemoryStream MemStream = new MemoryStream())
				{
					using (CryptoStream CryptoStream = new CryptoStream(MemStream, Encryptor, CryptoStreamMode.Write))
					{
						CryptoStream.Write(PlainText, 0, PlainText.Length);
						CryptoStream.FlushFinalBlock();
						CipherTextBytes = MemStream.ToArray();
						MemStream.Close();
						CryptoStream.Close();
					}
				}
			}

			SymmetricKey.Clear();
			return CipherTextBytes;
			//return Encoding.UTF8.GetBytes(Convert.ToBase64String(CipherTextBytes));
		}
开发者ID:BjkGkh,项目名称:R106,代码行数:39,代码来源:Encryption.cs


示例19: 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</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 byte array in UTF-8 format</returns>
		public static byte[] Decrypt(byte[] CipherText, byte[] Password, byte[] InitialVector, byte[] Salt)
		{
			PasswordDeriveBytes DerivedPassword = new PasswordDeriveBytes(Password, Salt, HASH_ALGORITHM, PASSWORDITERATIONS);
			byte[] KeyBytes = DerivedPassword.GetBytes(KEY_SIZE / 8);
			RijndaelManaged SymmetricKey = new RijndaelManaged();
			SymmetricKey.Mode = CipherMode.CBC;
			byte[] PlainTextBytes = new byte[CipherText.Length];
			int ByteCount = 0;
			using (ICryptoTransform Decryptor = SymmetricKey.CreateDecryptor(KeyBytes, InitialVector))
			{
				using (MemoryStream MemStream = new MemoryStream(CipherText))
				{
					using (CryptoStream CryptoStream = new CryptoStream(MemStream, Decryptor, CryptoStreamMode.Read))
					{

						ByteCount = CryptoStream.Read(PlainTextBytes, 0, PlainTextBytes.Length);
						MemStream.Close();
						CryptoStream.Close();
					}
				}
			}
			SymmetricKey.Clear();
			byte[] decrypted = new byte[ByteCount];
			Array.Copy(PlainTextBytes, decrypted, ByteCount);
			return decrypted;
		}
开发者ID:BjkGkh,项目名称:R106,代码行数:37,代码来源:Encryption.cs


示例20: DecryptData

 /// <summary>
 /// Decrypts a byte array with a password
 /// </summary>
 /// <param name="data">Data to decrypt</param>
 /// <param name="password">Password to use</param>
 /// <param name="paddingMode">Padding mode to use</param>
 /// <returns>Decrypted byte array</returns>
 /// <exception cref="System.ArgumentNullException">
 /// data
 /// or
 /// password
 /// </exception>
 /// <exception cref="ArgumentNullException"></exception>
 public static byte[] DecryptData(byte[] data, string password, PaddingMode paddingMode)
 {
     if (data == null || data.Length == 0)
         throw new ArgumentNullException("data");
     if (password == null)
         throw new ArgumentNullException("password");
     var pdb = new PasswordDeriveBytes(password, Encoding.UTF8.GetBytes("Salt"));
     var rm = new RijndaelManaged { Padding = paddingMode };
     ICryptoTransform decryptor = rm.CreateDecryptor(pdb.GetBytes(16), pdb.GetBytes(16));
     pdb.Dispose();
     using (var msDecrypt = new MemoryStream(data))
     using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
     {
         // Decrypted bytes will always be less then encrypted bytes, so length of encrypted data will be big enough for buffer.
         byte[] fromEncrypt = new byte[data.Length];
         // Read as many bytes as possible.
         int read = csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);
         if (read < fromEncrypt.Length)
         {
             // Return a byte array of proper size.
             byte[] clearBytes = new byte[read];
             Buffer.BlockCopy(fromEncrypt, 0, clearBytes, 0, read);
             return clearBytes;
         }
         return fromEncrypt;
     }
 }
开发者ID:rickyHong,项目名称:IPCameraCtl,代码行数:40,代码来源:EncDec.cs



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


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
C# Cryptography.RNGCryptoServiceProvider类代码示例发布时间:2022-05-26
下一篇:
C# Cryptography.OidCollection类代码示例发布时间:2022-05-26
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap