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


C# TripleDESCryptoServiceProvider.ValidKeySize方法代码示例

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


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

示例1: DecryptStringWith3DES

        public static string DecryptStringWith3DES(string data, string key, string iv)
        {
            UnicodeEncoding unicode = new UnicodeEncoding();
            Byte[] Bytes = Convert.FromBase64String(data);

            MemoryStream mem = new MemoryStream(100);
            TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();

            Byte[] KeyBytes = unicode.GetBytes(key);
            Byte[] tmpBytes = new Byte[16];
            Array.Copy(KeyBytes, tmpBytes, KeyBytes.Length < 16 ? KeyBytes.Length : 16);
            KeyBytes = tmpBytes;

            if(tdes.ValidKeySize(KeyBytes.Length*8))
                System.Diagnostics.Debug.WriteLine("Key size valid");
            if(TripleDESCryptoServiceProvider.IsWeakKey(KeyBytes))
                System.Diagnostics.Debug.WriteLine("Key weak");
            CryptoStream CrStream = new CryptoStream(mem, tdes.CreateDecryptor(KeyBytes, unicode.GetBytes(iv)), CryptoStreamMode.Write);

            for(int i = 0; i < Bytes.Length; i++)
                CrStream.WriteByte(Bytes[i]);

            CrStream.FlushFinalBlock();

            string result = unicode.GetString(mem.GetBuffer(), 0, (int)mem.Length);
            CrStream.Dispose();
            return result;
        }
开发者ID:cyotek,项目名称:translateclient,代码行数:28,代码来源:CryptoTools.cs

示例2: CryptBytes

 /// <summary>
 /// Encrypt or decrypt a byte array using the TripleDESCryptoServiceProvider crypto provider and Rfc2898DeriveBytes to build the key and initialization vector.
 /// </summary>
 /// <param name="password">The password String to use in encrypting or decrypting.</param>
 /// <param name="inBytes">The array of bytes to encrypt.</param>
 /// <param name="encrypt">True to encrypt, False to decrypt.</param>
 /// <returns></returns>
 /// <remarks></remarks>
 public static byte[] CryptBytes(String password, byte[] inBytes, bool encrypt)
 {
     // Make a triple DES service provider.
     var desProvider = new TripleDESCryptoServiceProvider();
     // Find a valid key size for this provider.
     var keySize = 0;
     for (var i = 1024; i >= 1; i--)
         if (desProvider.ValidKeySize(i))
         {
             keySize = i;
             break;
         }
     // Get the block size for this provider.
     var blockSize = desProvider.BlockSize;
     // Generate the key and initialization vector.
     byte[] key = null;
     byte[] iv = null;
     byte[] salt = { 0x10, 0x20, 0x12, 0x23, 0x37, 0xA4, 0xC5, 0xA6, 0xF1, 0xF0, 0xEE, 0x21, 0x22, 0x45 };
     MakeKeyAndIv(password, salt, keySize, blockSize, ref key, ref iv);
     // Make the encryptor or decryptor.
     var cryptoTransform = encrypt
                               ? desProvider.CreateEncryptor(key, iv)
                               : desProvider.CreateDecryptor(key, iv);
     byte[] result;
     // Create the output stream.
     using (var streamOut = new MemoryStream())
     {
         // Attach a crypto stream to the output stream.
         var streamCrypto = new CryptoStream(streamOut, cryptoTransform, CryptoStreamMode.Write);
         // Write the bytes into the CryptoStream.
         streamCrypto.Write(inBytes, 0, inBytes.Length);
         try
         {
             streamCrypto.FlushFinalBlock();
         }
         catch (CryptographicException)
         {
             // Ignore this one. The password is bad.
         }
         // Save the result.
         result = streamOut.ToArray();
         // Close the stream.
         try
         {
             streamCrypto.Close();
         }
         catch (CryptographicException)
         {
             // Ignore this one. The password is bad.
         }
         streamOut.Close();
     }
     return result;
 }
开发者ID:erashid,项目名称:Extensions,代码行数:62,代码来源:ByteArrayExtension.cs

示例3: ValidateKeySize

 public static bool ValidateKeySize( EncryptionAlgorithm algID, int Lenght)
 {
     switch (algID)
     {
         case EncryptionAlgorithm.DES:
             DES des = new DESCryptoServiceProvider();
             return des.ValidKeySize(Lenght);
         case EncryptionAlgorithm.Rc2:
             RC2 rc = new RC2CryptoServiceProvider();
             return rc.ValidKeySize(Lenght);
         case EncryptionAlgorithm.Rijndael:
             Rijndael rj = new RijndaelManaged();
             return rj.ValidKeySize(Lenght);
         case EncryptionAlgorithm.TripleDes:
             TripleDES tDes = new TripleDESCryptoServiceProvider();
             return tDes.ValidKeySize(Lenght);
         default:
             throw new CryptographicException("Algorithm " + algID + " Not Supported!");
     }
 }
开发者ID:pakoito,项目名称:web,代码行数:20,代码来源:Encryption.cs


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