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


C# PasswordDeriveBytes.CryptDeriveKey方法代码示例

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


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

示例1: TripleDESDecode

 public string TripleDESDecode(string str)
 {
     try
     {
         if (str.Length <= 0)
             return null;
         TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
         Byte[] iv = new Byte[8]; ;
         des.IV = iv;
         PasswordDeriveBytes pdb = new PasswordDeriveBytes("St$$#ef)!en", Encoding.ASCII.GetBytes("St$$#ef)!en".Length.ToString()));
         des.Key = pdb.CryptDeriveKey("TripleDES", "SHA1", 192, des.IV);
         Byte[] encryptedbytes = Convert.FromBase64String(str);
         MemoryStream ms = new MemoryStream(str.Length);
         CryptoStream decstream = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
         decstream.Write(encryptedbytes, 0, encryptedbytes.Length);
         decstream.FlushFinalBlock();
         Byte[] plainbytes = ms.ToArray();
         ms.Position = 0;
         ms.Read(plainbytes, 0, (int)(ms.Length - 1));
         decstream.Close();
         return System.Text.Encoding.UTF8.GetString(plainbytes);
     }
     catch
     {
         return null;
     }
 }
开发者ID:ash53,项目名称:SQLCM,代码行数:27,代码来源:EncryptionFuncs.cs

示例2: Decrypt

        public byte[] Decrypt(byte[] encrypted, byte[] publicKey)
        {
            RijndaelManaged objCrypRij = new RijndaelManaged();

             byte[] bytIV = GetMD5EncodeBytes(publicKey);

             objCrypRij.IV = bytIV;

             PasswordDeriveBytes pdb = new PasswordDeriveBytes(GetDecodeBytes(publicKey), new byte[0]);

             objCrypRij.Key = pdb.CryptDeriveKey("RC2", "MD5", 128, new byte[8]);

             byte[] encryptedBytes = encrypted;

             MemoryStream msText = new MemoryStream(encrypted.GetLength(0));

             CryptoStream decStream = new CryptoStream(msText, objCrypRij.CreateDecryptor(), CryptoStreamMode.Write);
             decStream.Write(encryptedBytes, 0, encryptedBytes.Length);
             decStream.FlushFinalBlock();
             byte[] plainBytes = new byte[msText.Length];
             msText.Position = 0;
             msText.Read(plainBytes, 0, Convert.ToInt32(msText.Length));
             decStream.Close();

             return plainBytes;
        }
开发者ID:ArquitecturaSoftware,项目名称:texfinadev,代码行数:26,代码来源:Cryptor.cs

示例3: PasswordDerivedBytes_Test

        public void PasswordDerivedBytes_Test()
        {

            byte[] randBytes = new byte[5];
            new Random(10032010).NextBytes(randBytes);


            var tdes = new TripleDESCryptoServiceProvider();
            var pwddb = new PasswordDeriveBytes("1", new byte[] { 1 });
            tdes.Key = pwddb.CryptDeriveKey("TripleDES", "SHA1", 192, tdes.IV);
            //string s = Convert.ToBase64String(tdes.Key);

        }
开发者ID:haoasqui,项目名称:ONLYOFFICE-Server,代码行数:13,代码来源:EmailValidationKeyPairProvider_Test.cs

示例4: CreateTranformer

        private ICryptoTransform CreateTranformer(bool forEncryption)
        {
            byte[] key = null;
            byte[] pdbsalt = null;
            byte[] iv = null;

            try
            {
                // Salt byte array.
                pdbsalt = GenerateSalt();

                // Create PasswordDeriveBytes object that will generate
                // a Key for TripleDES algorithm.
                PasswordDeriveBytes pdb = new PasswordDeriveBytes(Key, pdbsalt);

                iv = new byte[8] { 0, 0, 0, 0, 0, 0, 0, 0 };
                // Create a private key for TripleDES algorithm.
                // The iv parameter is not currently used.
                // * http://blogs.msdn.com/shawnfa/archive/2004/04/14/113514.aspx
                key = pdb.CryptDeriveKey("TripleDES", "SHA1", 192, iv);

                if (forEncryption)
                {
                    return TripleDES.Create().CreateEncryptor(key, iv);
                }
                else
                {
                    return TripleDES.Create().CreateDecryptor(key, iv);
                }
            }
            catch (CryptographicException)
            {
                return null;
            }
            finally
            {
                if (key != null)
                {
                    Array.Clear(key, 0, key.Length);
                }
                if (pdbsalt != null)
                {
                    Array.Clear(pdbsalt, 0, pdbsalt.Length);
                }
                if (iv != null)
                {
                    Array.Clear(iv, 0, iv.Length);
                }
            }
        }
开发者ID:ricardoborges,项目名称:OpenSGA,代码行数:50,代码来源:TripleDESEncryptionService.cs

示例5: EncryptionTransform

        public EncryptionTransform(String password) {
            PasswordDeriveBytes passwordBytes = 
                new PasswordDeriveBytes(password + paranoidSaltString,salt);

            // Create a TripleDESCryptoServiceProvider object.
            TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();
            tdes.Mode = CipherMode.ECB; 

            // Create the key and add it to the Key property.
            tdes.Key = passwordBytes.CryptDeriveKey("TripleDES", "SHA1", 192, tdes.IV);

            decrypter = tdes.CreateDecryptor();
            encrypter = tdes.CreateEncryptor();
        }
开发者ID:psyCHOder,项目名称:conferencexp,代码行数:14,代码来源:EncryptionTransform.cs

示例6: decriptUrl

        public static string decriptUrl(string Valor)
        {
            string Chave = "#[email protected]!s#";

            Valor = Valor.Replace("MAIS", "+");
            TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
            des.IV = new byte[8];
            PasswordDeriveBytes pdb = new PasswordDeriveBytes(Chave, new byte[-1 + 1]);
            des.Key = pdb.CryptDeriveKey("RC2", "MD5", 128, new byte[8]);
            byte[] encryptedBytes = Convert.FromBase64String(Valor);
            System.IO.MemoryStream MS = new System.IO.MemoryStream(Valor.Length);
            CryptoStream decStreAM = new CryptoStream(MS, des.CreateDecryptor(), CryptoStreamMode.Write);
            decStreAM.Write(encryptedBytes, 0, encryptedBytes.Length);
            decStreAM.FlushFinalBlock();
            byte[] plainBytes = new byte[Convert.ToInt32(MS.Length - 1) + 1];
            MS.Position = 0;
            MS.Read(plainBytes, 0, Convert.ToInt32(MS.Length));
            decStreAM.Close();
            return System.Text.Encoding.UTF8.GetString(plainBytes);
        }
开发者ID:willianleal,项目名称:AgenciaNoticiaN,代码行数:20,代码来源:Util.cs

示例7: SimpleSymmetricDecrypt

        public static string SimpleSymmetricDecrypt(this string data, string key)
        {
            TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
                des.IV = new byte[8];
                PasswordDeriveBytes pdb = new PasswordDeriveBytes(key, new byte[0]);
                des.Key = pdb.CryptDeriveKey("RC2", "MD5", 128, new byte[8]);

                byte[] encryptedBytes = Convert.FromBase64String(data);
                MemoryStream ms = new MemoryStream(data.Length);
                CryptoStream decStream = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);

                decStream.Write(encryptedBytes, 0, encryptedBytes.Length);
                decStream.FlushFinalBlock();

                byte[] plainBytes = new byte[ms.Length];
                ms.Position = 0;
                ms.Read(plainBytes, 0, (int)ms.Length);
                decStream.Close();

                return Encoding.UTF8.GetString(plainBytes);
        }
开发者ID:VKeCRM,项目名称:V2,代码行数:21,代码来源:SymmetricEncryption.cs

示例8: criptUrl

        public static string criptUrl(string valor)
        {
            string chave = "#[email protected]!s#";

            TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
            des.IV = new byte[8];
            PasswordDeriveBytes pdb = new PasswordDeriveBytes(chave, new byte[-1 + 1]);
            des.Key = pdb.CryptDeriveKey("RC2", "MD5", 128, new byte[8]);
            System.IO.MemoryStream ms = new System.IO.MemoryStream((valor.Length * 2) - 1);
            CryptoStream encStream = new CryptoStream(ms, des.CreateEncryptor(), CryptoStreamMode.Write);
            byte[] plainBytes = System.Text.Encoding.UTF8.GetBytes(valor);
            encStream.Write(plainBytes, 0, plainBytes.Length);
            encStream.FlushFinalBlock();
            byte[] encryptedBytes = new byte[Convert.ToInt32(ms.Length - 1) + 1];
            ms.Position = 0;
            ms.Read(encryptedBytes, 0, Convert.ToInt32(ms.Length));
            encStream.Close();
            string c = null;
            c = Convert.ToBase64String(encryptedBytes).Replace("+", "MAIS");
            return c;
        }
开发者ID:willianleal,项目名称:AgenciaNoticiaN,代码行数:21,代码来源:Util.cs

示例9: EncryptBytes

        private static byte[] EncryptBytes(byte[] SecureBytes, string PrivateKey)
        {
            byte[] array = new byte[]
                {
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0
                };

            PasswordDeriveBytes passwordDeriveBytes = new PasswordDeriveBytes(PrivateKey, array);

            byte[] array2 = new byte[]
                {
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0,
                    0
                };

            byte[] key = passwordDeriveBytes.CryptDeriveKey("TripleDES", "MD5", 0, array2);

            TripleDESCryptoServiceProvider tripleDESCryptoServiceProvider = new TripleDESCryptoServiceProvider();
            tripleDESCryptoServiceProvider.Key = key;
            tripleDESCryptoServiceProvider.Mode = CipherMode.ECB;

            return tripleDESCryptoServiceProvider.CreateEncryptor().TransformFinalBlock(SecureBytes, 0, SecureBytes.Length);
        }
开发者ID:LordBlacksun,项目名称:Allegiance-Community-Security-System,代码行数:39,代码来源:Encryption.cs

示例10: Decrypt

 public static string Decrypt(string datastr)
 {
     PasswordDeriveBytes bytes = new PasswordDeriveBytes("qianxun8", null);
     DESCryptoServiceProvider provider = new DESCryptoServiceProvider();
     byte[] rgbKey = bytes.CryptDeriveKey("DES", "SHA1", 0, new byte[8]);
     byte[] buffer = Encoding.Unicode.GetBytes(datastr);
     MemoryStream stream = new MemoryStream();
     CryptoStream stream2 = new CryptoStream(stream, provider.CreateDecryptor(rgbKey, rgbKey), CryptoStreamMode.Write);
     stream2.Write(buffer, 0, buffer.Length);
     stream2.FlushFinalBlock();
     return Encoding.Unicode.GetString(stream.ToArray());
 }
开发者ID:SoMeTech,项目名称:SoMeRegulatory,代码行数:12,代码来源:Public.cs

示例11: TRIPLEDESDecrypt

        public static string TRIPLEDESDecrypt(byte[] Data, string Password, byte[] Key, byte[] IV)
        {
            try
            {
                var pdb = new PasswordDeriveBytes(Password, Key);
                Key = pdb.CryptDeriveKey("TripleDES", "SHA1", 192, IV);
                //Key = pdb.GetBytes(Key.Length);

                // Create a new MemoryStream using the passed
                // array of encrypted data.
                var msDecrypt = new MemoryStream(Data);

                // Create a CryptoStream using the MemoryStream
                // and the passed key and initialization vector (IV).
                var csDecrypt = new CryptoStream(msDecrypt,
                    new TripleDESCryptoServiceProvider().CreateDecryptor(Key, IV),
                    CryptoStreamMode.Read);

                // Create buffer to hold the decrypted data.
                var fromEncrypt = new byte[Data.Length];

                // Read the decrypted data out of the crypto stream
                // and place it into the temporary buffer.
                csDecrypt.Read(fromEncrypt, 0, fromEncrypt.Length);

                //Convert the buffer into a string and return it.
                return new UnicodeEncoding().GetString(fromEncrypt);
            }
            catch (CryptographicException e)
            {
                Console.WriteLine(@"A Cryptographic error occurred: {0}", e.Message);
                return null;
            }
        }
开发者ID:jcrajat,项目名称:DAL_Design,代码行数:34,代码来源:Crypto.cs

示例12: decryptTripleDes

        private string decryptTripleDes(string encryptedBase64, string Password)
        {
            string password = Password;
            shiftLeft(ref password, 7);

            TripleDESCryptoServiceProvider des = new TripleDESCryptoServiceProvider();
            des.IV = new byte[8];
            PasswordDeriveBytes pdb = new PasswordDeriveBytes(password, new byte[0]);
            des.Key = pdb.CryptDeriveKey("RC2", "MD5", 128, new byte[8]);
            byte[] encryptedBytes = Convert.FromBase64String(encryptedBase64);
            MemoryStream ms = new MemoryStream(encryptedBase64.Length);
            CryptoStream decStream = new CryptoStream(ms, des.CreateDecryptor(), CryptoStreamMode.Write);
            decStream.Write(encryptedBytes, 0, encryptedBytes.Length);
            decStream.FlushFinalBlock();
            byte[] plainBytes = new byte[ms.Length];
            ms.Position = 0;
            ms.Read(plainBytes, 0, (int)ms.Length);
            decStream.Close();
            return Encoding.UTF8.GetString(plainBytes);
        }
开发者ID:keremkoseoglu,项目名称:MilkShake,代码行数:20,代码来源:Engine.cs

示例13: Encryptor

        /// <summary>
        /// Public constructor.
        /// </summary>
        public Encryptor()
        {
            // FIXME: AAA - need support for key and salt changing. What's best interface?
            byte[] salt = Esapi.SecurityConfiguration().MasterSalt;
            string pass = Esapi.SecurityConfiguration().MasterPassword;

            // setup algorithms
            encryptAlgorithm = Esapi.SecurityConfiguration().EncryptionAlgorithm;
            signatureAlgorithm = Esapi.SecurityConfiguration().DigitalSignatureAlgorithm;
            randomAlgorithm = Esapi.SecurityConfiguration().RandomAlgorithm;
            hashAlgorithm = Esapi.SecurityConfiguration().HashAlgorithm;

            try
            {
                // Set up encryption and decryption
                SymmetricAlgorithm symmetricAlgorithm = SymmetricAlgorithm.Create(encryptAlgorithm);
                symmetricAlgorithm.GenerateIV();
                iv = symmetricAlgorithm.IV;
                symmetricAlgorithm.Padding = PaddingMode.PKCS7;

                PasswordDeriveBytes passwordDeriveBytes = new PasswordDeriveBytes(pass, salt);
                // FIXME: We are using SHA1 hardcoded here, because for some reason CryptDeriveKey doesn't
                // like other hash algorithms. Also, it appears to not like Rijndael as a encryption algorithm.
                secretKey = passwordDeriveBytes.CryptDeriveKey(encryptAlgorithm, "SHA1", symmetricAlgorithm.KeySize, iv);
                encoding = Esapi.SecurityConfiguration().CharacterEncoding;

                // 13 is the code for DSA
                asymmetricKeyPair = new CspParameters(13);

                // The asymmetric key will be stored in the key container using the name ESAPI.
                asymmetricKeyPair.KeyContainerName = "ESAPI";
                // Set up signing keypair using the master password and salt
                // FIXME: Enhance - make DSA configurable

                RandomNumberGenerator randomNumberGenerator = RNGCryptoServiceProvider.Create(randomAlgorithm);
            }
            catch (Exception e)
            {
                // can't throw this exception in initializer, but this will log it
                new EncryptionException("Encryption failure", "Error creating Encryptor", e);
            }
        }
开发者ID:Effzz,项目名称:owasp-esapi-classicasp,代码行数:45,代码来源:Encryptor.cs

示例14: CryptDeriveKey_TooLongKey

	public void CryptDeriveKey_TooLongKey () 
	{
		PasswordDeriveBytes pd = new PasswordDeriveBytes ("password", null, "MD5", 1000);
		pd.CryptDeriveKey ("AlgName", "MD5", -256, new byte [8]);
	}
开发者ID:Profit0004,项目名称:mono,代码行数:5,代码来源:PasswordDeriveBytesTest.cs

示例15: TRIPLEDESEncrypt

        public static byte[] TRIPLEDESEncrypt(string Data, string Password, byte[] Key, byte[] IV)
        {
            try
            {
                var pdb = new PasswordDeriveBytes(Password, Key);
                Key = pdb.CryptDeriveKey("TripleDES", "SHA1", 192, IV);
                //Key = pdb.GetBytes(Key.Length);

                // Create a MemoryStream.
                var mStream = new MemoryStream();

                // Create a CryptoStream using the MemoryStream
                // and the passed key and initialization vector (IV).
                var cStream = new CryptoStream(mStream,
                    new TripleDESCryptoServiceProvider().CreateEncryptor(Key, IV),
                    CryptoStreamMode.Write);

                // Convert the passed string to a byte array.
                var toEncrypt = new UnicodeEncoding().GetBytes(Data);

                // Write the byte array to the crypto stream and flush it.
                cStream.Write(toEncrypt, 0, toEncrypt.Length);
                cStream.FlushFinalBlock();

                // Get an array of bytes from the
                // MemoryStream that holds the
                // encrypted data.
                var ret = mStream.ToArray();

                // Close the streams.
                cStream.Close();
                mStream.Close();

                // Return the encrypted buffer.
                return ret;
            }
            catch (CryptographicException e)
            {
                Console.WriteLine(@"A Cryptographic error occurred: {0}", e.Message);
                return null;
            }
        }
开发者ID:jcrajat,项目名称:DAL_Design,代码行数:42,代码来源:Crypto.cs


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