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

C# PasswordDeriveBytes类代码示例

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

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



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

示例1: Decrypt

    protected string Decrypt(string cipherText, string passPhrase, string saltValue, string hashAlgorithm, int passwordIterations, string initVector, int keySize)
    {
        byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
        byte[] saltValueBytes = Encoding.ASCII.GetBytes(saltValue);
        byte[] cipherTextBytes = Convert.FromBase64String(cipherText);
        PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, saltValueBytes, hashAlgorithm, passwordIterations);
        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);

        StringWriter writer = new StringWriter();
        HttpContext.Current.Server.UrlDecode(plainText, writer);

        return writer.ToString();
    }
开发者ID:bigWebApps,项目名称:Substitute,代码行数:23,代码来源:ConfirmJob.aspx.cs


示例2: Encrypt

    // Encrypt a string into a string using a password
    //    Uses Encrypt(byte[], byte[], byte[])
    public static string Encrypt(string clearText, string Password)
    {
        // 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.
        return Convert.ToBase64String(encryptedData);
    }
开发者ID:relaxar,项目名称:bin.net,代码行数:37,代码来源:AesCalculator.cs


示例3: 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:LCruel,项目名称:LCrypt2,代码行数:25,代码来源:Program.cs


示例4: Decrypt

 /// <summary>
 /// Decrypts a Base64 encoded string previously generated with a specific crypt class, returns a string
 /// </summary>
 /// <param name="cipherText">A base64 encoded string containing encryption information</param>
 /// <param name="passPhrase">The passphrase used to encrypt the inputed text</param>
 /// <returns></returns>
 public string Decrypt(string cipherText, string passPhrase)
 {
     try
         {
             var ciphertextS = DecodeFrom64(cipherText);
             var ciphersplit = Regex.Split(ciphertextS, "-");
             var passsalt = Convert.FromBase64String(ciphersplit[1]);
             var initVectorBytes = Convert.FromBase64String(ciphersplit[0]);
             var cipherTextBytes = Convert.FromBase64String(ciphersplit[2]);
             var password = new PasswordDeriveBytes(passPhrase, passsalt, "SHA512", 100);
             var keyBytes = password.GetBytes(256/8);
             var symmetricKey = new RijndaelManaged();
             symmetricKey.Mode = CipherMode.CBC;
             var decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes);
             var memoryStream = new MemoryStream(cipherTextBytes);
             var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read);
             var plainTextBytes = new byte[cipherTextBytes.Length];
             var decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
             memoryStream.Close();
             cryptoStream.Close();
             return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
         }
         catch (Exception m)
         {
             return "error";
         }
 }
开发者ID:BinaryEvolved,项目名称:C-AES256-Encryption-Class,代码行数:33,代码来源:Crypt.cs


示例5: Decrypt

 public static string Decrypt(string cipherText, string passPhrase)
 {
     byte[] cipherTextBytes = Convert.FromBase64String(cipherText);
     using (PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, null))
     {
         byte[] keyBytes = password.GetBytes(keysize / 8);
         using (RijndaelManaged symmetricKey = new RijndaelManaged())
         {
             symmetricKey.Mode = CipherMode.CBC;
             using (ICryptoTransform decryptor = symmetricKey.CreateDecryptor(keyBytes, initVectorBytes))
             {
                 using (MemoryStream memoryStream = new MemoryStream(cipherTextBytes))
                 {
                     try
                     {
                         using (CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
                         {
                             byte[] plainTextBytes = new byte[cipherTextBytes.Length];
                             int decryptedByteCount = cryptoStream.Read(plainTextBytes, 0, plainTextBytes.Length);
                             return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
                         }
                     }
                     catch
                     {
                         return "ERROR";
                     }
                 }
             }
         }
     }
 }
开发者ID:LCruel,项目名称:LCrypt2,代码行数:31,代码来源:Program.cs


示例6: Encrypt

 public static string Encrypt(string clearText, string Password)
 {
     byte[] clearBytes = Encoding.UTF8.GetBytes(clearText);
     PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password, new byte[] { 73, 118, 97, 110, 32, 77, 101, 100, 118, 101, 100, 101, 118 });
     byte[] encryptedData = TEncrypt.Encrypt(clearBytes, pdb.GetBytes(32), pdb.GetBytes(16));
     return Convert.ToBase64String(encryptedData);
 }
开发者ID:ranyaof,项目名称:gismaster,代码行数:7,代码来源:TEncrypt.cs


示例7: DecryptString

 protected 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:DawnShift,项目名称:Multi-cloud-Encryption-,代码行数:31,代码来源:Cryptography.cs


示例8: Decrypt

    /// <summary>
    /// Decrypts a previously encrypted string.
    /// </summary>
    /// <param name="inputText">The encrypted string to decrypt.</param>
    /// <returns>A decrypted string.</returns>
    public static string Decrypt(string inputText)
    {
        string decrypted = null;
        try
        {
            RijndaelManaged rijndaelCipher = new RijndaelManaged();
            byte[] encryptedData = Convert.FromBase64String(inputText);
            PasswordDeriveBytes secretKey = new PasswordDeriveBytes(ENCRYPTION_KEY, SALT);

            using (ICryptoTransform decryptor = rijndaelCipher.CreateDecryptor(secretKey.GetBytes(32), secretKey.GetBytes(16)))
            {
                using (MemoryStream memoryStream = new MemoryStream(encryptedData))
                {
                    using (CryptoStream cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
                    {
                        byte[] plainText = new byte[encryptedData.Length];
                        int decryptedCount = cryptoStream.Read(plainText, 0, plainText.Length);
                        decrypted = Encoding.Unicode.GetString(plainText, 0, decryptedCount);
                    }
                }
            }
        }
        catch (Exception)
        {
        }
        return decrypted;
    }
开发者ID:kiquenet,项目名称:B4F,代码行数:32,代码来源:QueryStringModule.cs


示例9: Decrypt

 public static string Decrypt(string cipherText, string Password)
 {
     byte[] cipherBytes = Convert.FromBase64String(cipherText);
     PasswordDeriveBytes pdb = new PasswordDeriveBytes(Password, new byte[] { 73, 118, 97, 110, 32, 77, 101, 100, 118, 101, 100, 101, 118 });
     byte[] decryptedData = TEncrypt.Decrypt(cipherBytes, pdb.GetBytes(32), pdb.GetBytes(16));
     return Encoding.UTF8.GetString(decryptedData);
 }
开发者ID:ranyaof,项目名称:gismaster,代码行数:7,代码来源:TEncrypt.cs


示例10: Main

    public static void Main() {
        string PlainText = "Titan";
        byte[] PlainBytes = new byte[5];
        PlainBytes = Encoding.ASCII.GetBytes(PlainText.ToCharArray());
        PrintByteArray(PlainBytes);
        byte[] CipherBytes = new byte[8];
        PasswordDeriveBytes pdb = new PasswordDeriveBytes("Titan", null);
        byte[] IV = new byte[8];
        byte[] Key = pdb.CryptDeriveKey("RC2", "SHA1", 40, IV);
        PrintByteArray(Key);
        PrintByteArray(IV);

        // Now use the data to encrypt something
        RC2CryptoServiceProvider rc2 = new RC2CryptoServiceProvider();
        Console.WriteLine(rc2.Padding);
        Console.WriteLine(rc2.Mode);
        ICryptoTransform sse = rc2.CreateEncryptor(Key, IV);
        MemoryStream ms = new MemoryStream();
        CryptoStream cs1 = new CryptoStream(ms, sse, CryptoStreamMode.Write);
        cs1.Write(PlainBytes, 0, PlainBytes.Length);
        cs1.FlushFinalBlock();
        CipherBytes = ms.ToArray();
        cs1.Close();
        Console.WriteLine(Encoding.ASCII.GetString(CipherBytes));
        PrintByteArray(CipherBytes);

        ICryptoTransform ssd = rc2.CreateDecryptor(Key, IV);
        CryptoStream cs2 = new CryptoStream(new MemoryStream(CipherBytes), ssd, CryptoStreamMode.Read);
        byte[] InitialText = new byte[5];
        cs2.Read(InitialText, 0, 5);
        Console.WriteLine(Encoding.ASCII.GetString(InitialText));
    	PrintByteArray(InitialText);
    }
开发者ID:aura1213,项目名称:netmf-interpreter,代码行数:33,代码来源:DeriveBytesTest.cs


示例11: Decrypt

    public static string Decrypt(string inName, string password)
    {
        PasswordDeriveBytes pdb = new PasswordDeriveBytes(password, keySalt);

        Rijndael alg = Rijndael.Create();
        alg.Key = pdb.GetBytes(32);
        alg.IV = pdb.GetBytes(16);

        using (var fin = new FileStream(inName, FileMode.Open, FileAccess.Read))
        using (var fout = new MemoryStream())
        using (var cs = new CryptoStream(fout, alg.CreateDecryptor(), CryptoStreamMode.Write))
        {
            byte[] buffer = new byte[4096];
            int bytesRead;

            do
            {
                bytesRead = fin.Read(buffer, 0, buffer.Length);
                cs.Write(buffer, 0, bytesRead);
            }
            while (bytesRead != 0);

            cs.FlushFinalBlock();
            return Encoding.ASCII.GetString(fout.ToArray());
        }
    }
开发者ID:Diullei,项目名称:Storm,代码行数:26,代码来源:host.cs


示例12: Ctor_NullSalt

 public static void Ctor_NullSalt()
 {
     using (var pdb = new PasswordDeriveBytes(TestPassword, null))
     {
         Assert.Equal(DefaultIterationCount, pdb.IterationCount);
         Assert.Equal(null, pdb.Salt);
         Assert.Equal("SHA1", pdb.HashName);
     }
 }
开发者ID:chcosta,项目名称:corefx,代码行数:9,代码来源:PasswordDeriveBytesTests.cs


示例13: Ctor_EmptySalt

 public static void Ctor_EmptySalt()
 {
     using (var pdb = new PasswordDeriveBytes(TestPassword, Array.Empty<byte>()))
     {
         Assert.Equal(DefaultIterationCount, pdb.IterationCount);
         Assert.Equal(Array.Empty<byte>(), pdb.Salt);
         Assert.Equal("SHA1", pdb.HashName);
     }
 }
开发者ID:chcosta,项目名称:corefx,代码行数:9,代码来源:PasswordDeriveBytesTests.cs


示例14: Ctor_DiminishedSalt

 public static void Ctor_DiminishedSalt()
 {
     using (var pdb = new PasswordDeriveBytes(TestPassword, new byte[7]))
     {
         Assert.Equal(DefaultIterationCount, pdb.IterationCount);
         Assert.Equal(7, pdb.Salt.Length);
         Assert.Equal("SHA1", pdb.HashName);
     }
 }
开发者ID:chcosta,项目名称:corefx,代码行数:9,代码来源:PasswordDeriveBytesTests.cs


示例15: Ctor_NullPasswordBytes

 public static void Ctor_NullPasswordBytes()
 {
     using (var pdb = new PasswordDeriveBytes((byte[])null, s_testSalt))
     {
         Assert.Equal(DefaultIterationCount, pdb.IterationCount);
         Assert.Equal(s_testSalt, pdb.Salt);
         Assert.Equal("SHA1", pdb.HashName);
     }
 }
开发者ID:chcosta,项目名称:corefx,代码行数:9,代码来源:PasswordDeriveBytesTests.cs


示例16: GetBytesSmall

	/// <summary>
	///		GetBytes with small values
	/// </summary>
	private static bool GetBytesSmall()
	{
		string password = "pwd";
		byte[] salt		= new byte[] {0, 1, 2, 3, 4, 5, 6, 7};
		int	   c		= 5;

    	PasswordDeriveBytes pdb = new PasswordDeriveBytes(password, salt, "SHA1", c);
    	pdb.Salt = salt;

    	byte[] res1 = pdb.GetBytes(1);
    	byte[] res2 = pdb.GetBytes(2);

		return
			res1 != null && res1.Length == 1 &&
			res2 != null && res2.Length == 2;
	}
开发者ID:aura1213,项目名称:netmf-interpreter,代码行数:19,代码来源:PwdDrvBytes.cs


示例17: Decrypt

 public string Decrypt(string cipherText, string passPhrase)
 {
     byte[] initVectorBytes = Encoding.ASCII.GetBytes(initVector);
     byte[] cipherTextBytes = Convert.FromBase64String(cipherText);
     PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, null);
     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();
     return Encoding.UTF8.GetString(plainTextBytes, 0, decryptedByteCount);
 }
开发者ID:satishinext,项目名称:php,代码行数:17,代码来源:StringCipher.cs


示例18: EncryptString

 //Encrypt
 public string EncryptString(string plainText)
 {
     string passPhrase = "c00ked!n";
     byte[] initVectorBytes = Encoding.UTF8.GetBytes(initVector);
     byte[] plainTextBytes = Encoding.UTF8.GetBytes(plainText);
     PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, null);
     byte[] keyBytes = password.GetBytes(keysize / 8);
     RijndaelManaged symmetricKey = new RijndaelManaged();
     symmetricKey.Mode = CipherMode.CBC;
     ICryptoTransform encryptor = symmetricKey.CreateEncryptor(keyBytes, initVectorBytes);
     MemoryStream memoryStream = new MemoryStream();
     CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write);
     cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
     cryptoStream.FlushFinalBlock();
     byte[] cipherTextBytes = memoryStream.ToArray();
     memoryStream.Close();
     cryptoStream.Close();
     return Convert.ToBase64String(cipherTextBytes);
 }
开发者ID:blinklabllp,项目名称:cookedinrepo,代码行数:20,代码来源:EncryptionDecryption.cs


示例19: Encrypt

    /// <summary>
    /// Encrypts any string using the Rijndael algorithm.
    /// </summary>
    /// <param name="inputText">The string to encrypt.</param>
    /// <returns>A Base64 encrypted string.</returns>
    public static string Encrypt(string inputText)
    {
        RijndaelManaged rijndaelCipher = new RijndaelManaged();
        byte[] plainText = Encoding.Unicode.GetBytes(inputText);
        PasswordDeriveBytes SecretKey = new PasswordDeriveBytes(ENCRYPTION_KEY, SALT);

        using (ICryptoTransform encryptor = rijndaelCipher.CreateEncryptor(SecretKey.GetBytes(32), SecretKey.GetBytes(16)))
        {
            using (MemoryStream memoryStream = new MemoryStream())
            {
                using (CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
                {
                    cryptoStream.Write(plainText, 0, plainText.Length);
                    cryptoStream.FlushFinalBlock();
                    return "?" + PARAMETER_NAME + Convert.ToBase64String(memoryStream.ToArray());
                }
            }
        }
    }
开发者ID:kiquenet,项目名称:B4F,代码行数:24,代码来源:QueryStringModule.cs


示例20: GetScopeKey

	// Get the encryption key to use to protect memory for a scope.
	private static byte[] GetScopeKey(MemoryProtectionScope scope, byte[] salt)
			{
				String key;
				PasswordDeriveBytes derive;
				if(scope == MemoryProtectionScope.SameLogon)
				{
					key = Environment.UserName;
				}
				else
				{
					key = Environment.UserName + "/" + Environment.MachineName;
				}
				if(salt == null)
				{
					salt = new byte [16];
				}
				derive = new PasswordDeriveBytes(key, salt);
				return derive.CryptDeriveKey("Rijndael", "SHA1", 16, null);
			}
开发者ID:jjenki11,项目名称:blaze-chem-rendering,代码行数:20,代码来源:ProtectedData.cs



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


鲜花

握手

雷人

路过

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

请发表评论

全部评论

专题导读
上一篇:
C# PatchInjection类代码示例发布时间:2022-05-24
下一篇:
C# PasswordConnectionInfo类代码示例发布时间:2022-05-24
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

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

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

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