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


C# PasswordDeriveBytes.GetBytes方法代码示例

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


在下文中一共展示了PasswordDeriveBytes.GetBytes方法的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: DecryptString

        public static String DecryptString(String Text, String Key)
        {
            if (Text.IsNullOrEmpty())
                throw new BPAExtensionException("DecryptString Text Not Found!");

            RijndaelManaged RijndaelCipher = new RijndaelManaged();

            byte[] EncryptedData = Convert.FromBase64String(Text.Replace("_", "/").Replace("-", "+"));
            byte[] Salt = new UTF8Encoding().GetBytes(Key.Length.ToString());

            PasswordDeriveBytes SecretKey = new PasswordDeriveBytes(Key, Salt);
            ICryptoTransform Decryptor = RijndaelCipher.CreateDecryptor(SecretKey.GetBytes(16), SecretKey.GetBytes(16));
            MemoryStream memoryStream = new MemoryStream(EncryptedData);
            CryptoStream 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();

            String DecryptedData = new UTF8Encoding().GetString(PlainText, 0, DecryptedCount);

            if (HttpContext.Current != null)
                return HttpContext.Current.Server.HtmlDecode(DecryptedData);
            else
                return DecryptedData;
        }
开发者ID:rafaelberrocalj,项目名称:BPA,代码行数:28,代码来源:EncryptDecrypt.cs

示例3: Cryption

        public static String Cryption(string text, string password, bool boolCrypt)
        {
            byte[] utf8_Salt = new byte[] { 0x26, 0x19, 0x81, 0x4E, 0xA0, 0x6D, 0x95, 0x34, 0x26, 0x75, 0x64, 0x05, 0xF6 };

            PasswordDeriveBytes i_Pass = new PasswordDeriveBytes(password, utf8_Salt);

            Rijndael i_Alg = Rijndael.Create();
            i_Alg.Key = i_Pass.GetBytes(32);
            i_Alg.IV = i_Pass.GetBytes(16);

            ICryptoTransform i_Trans = (boolCrypt) ? i_Alg.CreateEncryptor() : i_Alg.CreateDecryptor();

            MemoryStream i_Mem = new MemoryStream();
            CryptoStream i_Crypt = new CryptoStream(i_Mem, i_Trans, CryptoStreamMode.Write);

            byte[] utf8_Data;
            if (boolCrypt) utf8_Data = Encoding.Unicode.GetBytes(text);
            else utf8_Data = Convert.FromBase64String(text);

            try
            {
                i_Crypt.Write(utf8_Data, 0, utf8_Data.Length);
                i_Crypt.Close();
            }
            catch { return null; }

            if (boolCrypt) return Convert.ToBase64String(i_Mem.ToArray());
            else return Encoding.Unicode.GetString(i_Mem.ToArray());
        }
开发者ID:hyori7,项目名称:WebDirectory,代码行数:29,代码来源:Cryptopraphy.cs

示例4: 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

示例5: 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

示例6: 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

示例7: omDecryptEx

        public static string omDecryptEx(string strEncryptedString)
        {
            if (!string.IsNullOrEmpty(strEncryptedString))
            {
                try
                {
                    string strPassword = "H3faLump";
                    RijndaelManaged managed = new RijndaelManaged();
                    byte[] buffer = Convert.FromBase64String(strEncryptedString);
                    byte[] rgbSalt = Encoding.ASCII.GetBytes(strPassword.Length.ToString());
                    PasswordDeriveBytes bytes = new PasswordDeriveBytes(strPassword, rgbSalt);
                    ICryptoTransform transform = managed.CreateDecryptor(bytes.GetBytes(0x20), bytes.GetBytes(0x10));
                    MemoryStream stream = new MemoryStream(buffer);
                    CryptoStream stream2 = new CryptoStream(stream, transform, CryptoStreamMode.Read);
                    byte[] buffer3 = new byte[buffer.Length];
                    int count = stream2.Read(buffer3, 0, buffer3.Length);
                    stream.Close();
                    stream2.Close();
                    return Encoding.Unicode.GetString(buffer3, 0, count);
                }
                catch
                {
                    throw new SecurityException("Could not decrypt string");
                }
            }

            return string.Empty;
        }
开发者ID:vimalgupta1980,项目名称:dotnetlibs,代码行数:28,代码来源:Security.cs

示例8: 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

示例9: Encrypt

        /**
         * AES 256 bits encrypt with salt
        **/
        private static byte[] Encrypt(byte[] plainTextBytes, byte[] passwordBytes, byte[] saltBytes)
        {
            byte[] cipherTextBytes = null;
            ICryptoTransform encryptor = null;

            PasswordDeriveBytes key = new PasswordDeriveBytes(passwordBytes, saltBytes, DEFAULT_HASH_ALGORITHM, PASSWORD_ITERATIONS);

            RijndaelManaged AES = new RijndaelManaged();
            AES.KeySize = DEFAULT_KEY_SIZE;
            AES.Mode = CipherMode.CBC;

            // Get Key And IV From Password And Salt
            AES.Key = key.GetBytes(AES.KeySize / 8);
            AES.IV = key.GetBytes(AES.BlockSize / 8);
            encryptor = AES.CreateEncryptor();

            MemoryStream memoryStream = new MemoryStream();
            CryptoStream cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write);
            cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);

            cryptoStream.FlushFinalBlock();
            cipherTextBytes = memoryStream.ToArray();

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

            return cipherTextBytes;
        }
开发者ID:jabex,项目名称:hammertoss,代码行数:31,代码来源:Program.cs

示例10: Decrypt

        private static string Decrypt(string inputText)
        {
            RijndaelManaged rijndaelCipher = new RijndaelManaged();
            Byte[] encryptedData = Convert.FromBase64String(inputText);

            //Rfc2898DeriveBytes pwdGen = new Rfc2898DeriveBytes(ENCRYPTION_KEY, ENCRYPTION_SALT, 1000);
            PasswordDeriveBytes pwdGen = new PasswordDeriveBytes(ENCRYPTION(), ENCRYPTION_SALT());
            byte[] Key = pwdGen.GetBytes(32);
            byte[] IV = pwdGen.GetBytes(16);

            using (ICryptoTransform decryptor = rijndaelCipher.CreateDecryptor(Key, IV))
            {
                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);
                        return Encoding.Unicode.GetString(plainText, 0, decryptedCount);
                    }
                }
            }
        }
开发者ID:sinaaslani,项目名称:kids.bmi.ir,代码行数:25,代码来源:LogUtility.cs

示例11: Encrypt

        /// <summary>
        /// Passwort-Verschlüsselung
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static string Encrypt(string text)
        {
            if (text.Length < 1)
            {
                return text;
            }

            string code = "";

            System.Text.UnicodeEncoding enc = new System.Text.UnicodeEncoding();
            byte[] bytes = enc.GetBytes(text);
            PasswordDeriveBytes pdb = new PasswordDeriveBytes(code, new byte[] { 0x49, 0x76, 0x61, 0x6e, 0x20, 0x4d, 0x65, 0x64, 0x76, 0x65, 0x64, 0x65, 0x76 });

            using (MemoryStream ms = new MemoryStream())
            {
                Rijndael alg = Rijndael.Create();
                alg.Key = pdb.GetBytes(32);
                alg.IV = pdb.GetBytes(16);

                using (CryptoStream cs = new CryptoStream(ms, alg.CreateEncryptor(), CryptoStreamMode.Write))
                {
                    cs.Write(bytes, 0, bytes.Length);
                }

                bytes = ms.ToArray();
            }
            return Convert.ToBase64String(bytes);
        }
开发者ID:Faluwen,项目名称:TournamentOrganizer,代码行数:33,代码来源:CryptManager.cs

示例12: EncryptString

      public static string EncryptString(string InputText, string Password)
      {
         RijndaelManaged RijndaelCipher = new RijndaelManaged();

         byte[] PlainText = System.Text.Encoding.Unicode.GetBytes(InputText);

         byte[] Salt = Encoding.ASCII.GetBytes(Password.Length.ToString());

         PasswordDeriveBytes SecretKey = new PasswordDeriveBytes(Password, Salt);

         ICryptoTransform Encryptor = RijndaelCipher.CreateEncryptor(SecretKey.GetBytes(32), SecretKey.GetBytes(16));

         MemoryStream memoryStream = new MemoryStream();

         CryptoStream cryptoStream = new CryptoStream(memoryStream, Encryptor, CryptoStreamMode.Write);

         cryptoStream.Write(PlainText, 0, PlainText.Length);

         cryptoStream.FlushFinalBlock();

         byte[] CipherBytes = memoryStream.ToArray();

         memoryStream.Close();

         cryptoStream.Close();

         string EncryptedData = Convert.ToBase64String(CipherBytes);

         // Return encrypted string.
         return EncryptedData;
      }
开发者ID:codemonkies,项目名称:UTGV1.0,代码行数:31,代码来源:Cryotor.cs

示例13: Decrypt

        /// <summary>
        /// Triple DES Descrypt
        /// </summary>
        /// <param name="input">String to decrypt</param>
        /// <param name="key">Encryption Key</param>
        /// <returns></returns>
        public static string Decrypt(string input, string key)
        {
            TripleDESCryptoServiceProvider crp =new TripleDESCryptoServiceProvider();
            System.Text.UnicodeEncoding uEncode = new UnicodeEncoding();
            System.Text.ASCIIEncoding aEncode =new ASCIIEncoding();

            Byte[] bytCipherText = System.Convert.FromBase64String(input);
            MemoryStream stmPlainText =new MemoryStream();
            MemoryStream stmCipherText = new MemoryStream(bytCipherText);

            Byte[] slt= {0x12};

            PasswordDeriveBytes pdb = new PasswordDeriveBytes(key, slt);
            Byte[] bytDerivedKey = pdb.GetBytes(24);
            crp.Key = bytDerivedKey;

            crp.IV = pdb.GetBytes(8);
            CryptoStream csDecrypted = new CryptoStream(stmCipherText, crp.CreateDecryptor(), CryptoStreamMode.Read);

            StreamWriter sw = new StreamWriter(stmPlainText);
            StreamReader sr = new StreamReader(csDecrypted);
            sw.Write(sr.ReadToEnd());

            sw.Flush();

            crp.Clear();

            return uEncode.GetString(stmPlainText.ToArray());
        }
开发者ID:asr340,项目名称:owasp-code-central,代码行数:35,代码来源:encryption.cs

示例14: 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 designed exactly for what we are
            // trying to do.
            return System.Text.Encoding.Unicode.GetString(decryptedData);
        }
开发者ID:Klaudit,项目名称:inbox2_desktop,代码行数:29,代码来源:Crypto.cs

示例15: DecryptBinary

        public byte[] DecryptBinary( byte [] encryptedBytes, string key )
        {
            RijndaelManaged RijndaelCipher = new RijndaelManaged ();

            byte [] EncryptedData = encryptedBytes;
            byte [] Salt = Encoding.ASCII.GetBytes ( key.Length.ToString () );

            PasswordDeriveBytes SecretKey = new PasswordDeriveBytes ( key, Salt );

            // Create a decryptor from the existing SecretKey bytes.
            ICryptoTransform Decryptor = RijndaelCipher.CreateDecryptor ( SecretKey.GetBytes ( 32 ), 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 ();

            // Return decrypted string.
            return PlainText;
        }
开发者ID:bmantoni,项目名称:PasswordKeeper,代码行数:31,代码来源:RijndaelEncryption.cs


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