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


C# RijndaelManaged.ValidKeySize方法代码示例

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


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

示例1: IsValidKey

 static bool IsValidKey(byte[] key)
 {
     using (var rijndael = new RijndaelManaged())
     {
         var bitLength = key.Length * 8;
         return rijndael.ValidKeySize(bitLength);
     }
 }
开发者ID:xqfgbc,项目名称:NServiceBus,代码行数:8,代码来源:RijndaelEncryptionService.cs

示例2: SaveToRegistry

 /// <summary>
 /// Given an open registry key, save this set of site parameters as a subkey under
 /// that registry, encrypting the data as we go.
 /// </summary>
 /// <param name="registryKey">The parent registry key</param>
 /// <returns>True for success, false for failure</returns>
 public bool SaveToRegistry(RegistryKey registryKey)
 {
     // This only makes sense if the registry key exists, i.e. it is open:
     if (registryKey != null)
     {
         // Asbestos underpants:
         try
         {
             // Serialize the parameters binary data:
             BinaryFormatter bf = new BinaryFormatter();
             MemoryStream ms = new MemoryStream();
             bf.Serialize(ms, this);
             ms.Close();
             byte[] serializedParams = ms.ToArray();
             // Generate the Rijndael object, encryption key and initialization vector:
             RijndaelManaged rijndael = new RijndaelManaged();
             if (rijndael.ValidKeySize(256)) rijndael.KeySize = 256;
             rijndael.Padding = PaddingMode.PKCS7;
             byte[] cryptKey = GenerateEncryptionKey(Key);
             byte[] iv = GenerateIV(rijndael.BlockSize / 8, Key);
             // Encrypt the site parameters:
             ICryptoTransform encryptor = rijndael.CreateEncryptor(cryptKey, iv);
             ms = new MemoryStream();
             CryptoStream cs = new CryptoStream(ms, encryptor, CryptoStreamMode.Write);
             cs.Write(serializedParams, 0, serializedParams.Length);
             cs.FlushFinalBlock();
             cs.Close();
             ms.Close();
             // Now convert the data to Base64 and save it to the registry using the
             // generated key
             registryKey.SetValue(Key, ms.ToArray(), RegistryValueKind.Binary);
             return true;
         }
         // If anything failed, let the user know:
         catch { return false; }
     }
     // If the registry key wasn't open, there's no use continuing:
     else return false;
 }
开发者ID:gpfjeff,项目名称:cryptnos-for-windows,代码行数:45,代码来源:SiteParameters.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

示例4: ReadFromRegistry

 /// <summary>
 /// Given an open registry key and a site key value, read the site parameters from
 /// the registry and return a SiteParameters object.
 /// </summary>
 /// <param name="registryKey">An open registry key</param>
 /// <param name="siteKey">A site key string</param>
 /// <returns>A SiteParameters object, or null on failure</returns>
 public static SiteParameters ReadFromRegistry(RegistryKey registryKey, string siteKey)
 {
     // Asbestos underpants:
     try
     {
         // This only works if the registry key is open and the site key is
         // something meaningful:
         if (registryKey != null && !String.IsNullOrEmpty(siteKey))
         {
             // Look for the site key value in the registry key and convert it from
             // Base64 to a byte array:
             byte[] encryptedParams = (byte[])registryKey.GetValue(siteKey);
             // Set up our decryption engine.  Create the Rijndael object, its
             // encryption key, and its initialization vector.
             RijndaelManaged rijndael = new RijndaelManaged();
             if (rijndael.ValidKeySize(256)) rijndael.KeySize = 256;
             rijndael.Padding = PaddingMode.PKCS7;
             byte[] cryptKey = GenerateEncryptionKey(siteKey);
             byte[] iv = GenerateIV(rijndael.BlockSize / 8, siteKey);
             // Decrypt the raw bytes read from the registry:
             ICryptoTransform decryptor = rijndael.CreateDecryptor(cryptKey, iv);
             MemoryStream ms = new MemoryStream(encryptedParams);
             CryptoStream cs = new CryptoStream(ms, decryptor, CryptoStreamMode.Read);
             byte[] decryptedBytes = new byte[encryptedParams.Length];
             cs.Read(decryptedBytes, 0, decryptedBytes.Length);
             cs.Close();
             ms.Close();
             // Reset the memory stream to read the raw bytes and use a binary
             // formatter to deserialize the object:
             ms = new MemoryStream(decryptedBytes);
             BinaryFormatter bf = new BinaryFormatter();
             SiteParameters sp = (SiteParameters)bf.Deserialize(ms);
             return sp;
         }
         // If the registry key wasn't open or the site key wasn't meaningful, there's
         // nothing to do:
         else return null;
     }
     // If anything blows up, don't return anything we can use:
     catch { return null; }
 }
开发者ID:gpfjeff,项目名称:cryptnos-for-windows,代码行数:48,代码来源:SiteParameters.cs

示例5: IsValidKey

        static bool IsValidKey(byte[] key)
        {
            using (var rijndael = new RijndaelManaged())
            {
                var bitLength = key.Length*8;

                var maxValidKeyBitLength = rijndael.LegalKeySizes.Max(keyLength => keyLength.MaxSize);
                if (bitLength < maxValidKeyBitLength)
                {
                    Log.WarnFormat("Encryption key is {0} bits which is less than the maximum allowed {1} bits. Consider using a {1}-bit encryption key to obtain the maximum cipher strength", bitLength, maxValidKeyBitLength);
                }

                return rijndael.ValidKeySize(bitLength);
            }
        }
开发者ID:Particular,项目名称:NServiceBus,代码行数:15,代码来源:RijndaelEncryptionService.cs


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