The Labs \ Source Viewer \ SSCLI \ System.Security.Cryptography \ CipherMode

  1. // ==++==
  2. //
  3. //
  4. // Copyright (c) 2006 Microsoft Corporation. All rights reserved.
  5. //
  6. // The use and distribution terms for this software are contained in the file
  7. // named license.txt, which can be found in the root of this distribution.
  8. // By using this software in any fashion, you are agreeing to be bound by the
  9. // terms of this license.
  10. //
  11. // You must not remove this notice, or any other, from this software.
  12. //
  13. //
  14. // ==--==
  15. //
  16. // Crypto.cs
  17. //
  18. namespace System.Security.Cryptography
  19. {
  20.     using Microsoft.Win32;
  21.     using System.Runtime.Serialization;
  22.     using System.Globalization;
  23.    
  24.     // This enum represents cipher chaining modes: cipher block chaining (CBC),
  25.     // electronic code book (ECB), output feedback (OFB), cipher feedback (CFB),
  26.     // and ciphertext-stealing (CTS). Not all implementations will support all modes.
  27.     [Serializable()]
  28.     [System.Runtime.InteropServices.ComVisible(true)]
  29.     public enum CipherMode
  30.     {
  31.         // Please keep in sync with wincrypt.h
  32.         CBC = 1,
  33.         ECB = 2,
  34.         OFB = 3,
  35.         CFB = 4,
  36.         CTS = 5
  37.     }
  38.    
  39.     // This enum represents the padding method to use for filling out short blocks.
  40.     // "None" means no padding (whole blocks required).
  41.     // "PKCS7" is the padding mode defined in RFC 2898, Section 6.1.1, Step 4, generalized
  42.     // to whatever block size is required.
  43.     // "Zeros" means pad with zero bytes to fill out the last block.
  44.     // "ISO 10126" is the same as PKCS5 except that it fills the bytes before the last one with
  45.     // random bytes. "ANSI X.923" fills the bytes with zeros and puts the number of padding
  46.     // bytes in the last byte.
  47.    
  48.     [Serializable()]
  49.     [System.Runtime.InteropServices.ComVisible(true)]
  50.     public enum PaddingMode
  51.     {
  52.         None = 1,
  53.         PKCS7 = 2,
  54.         Zeros = 3,
  55.         ANSIX923 = 4,
  56.         ISO10126 = 5
  57.     }
  58.    
  59.     // This structure is used for returning the set of legal key sizes and
  60.     // block sizes of the symmetric algorithms.
  61.     // Note: this class should be sealed, otherwise someone could sub-class it and the read-only
  62.     // properties we depend on can have setters. Ideally, we should have a struct here (value type)
  63.     // but we use what we have now and try to close the hole allowing someone to specify an invalid key size
  64.     [System.Runtime.InteropServices.ComVisible(true)]
  65.     public sealed class KeySizes
  66.     {
  67.         private int m_minSize;
  68.         private int m_maxSize;
  69.         private int m_skipSize;
  70.        
  71.         public int MinSize {
  72.             get { return m_minSize; }
  73.         }
  74.        
  75.         public int MaxSize {
  76.             get { return m_maxSize; }
  77.         }
  78.        
  79.         public int SkipSize {
  80.             get { return m_skipSize; }
  81.         }
  82.        
  83.         public KeySizes(int minSize, int maxSize, int skipSize)
  84.         {
  85.             m_minSize = minSize;
  86.             m_maxSize = maxSize;
  87.             m_skipSize = skipSize;
  88.         }
  89.     }
  90.    
  91.     [Serializable()]
  92.     [System.Runtime.InteropServices.ComVisible(true)]
  93.     public class CryptographicException : SystemException
  94.     {
  95.         private const int FORMAT_MESSAGE_IGNORE_INSERTS = 512;
  96.         private const int FORMAT_MESSAGE_FROM_SYSTEM = 4096;
  97.         private const int FORMAT_MESSAGE_ARGUMENT_ARRAY = 8192;
  98.        
  99.         public CryptographicException() : base(Environment.GetResourceString("Arg_CryptographyException"))
  100.         {
  101.             SetErrorCode(__HResults.CORSEC_E_CRYPTO);
  102.         }
  103.        
  104.         public CryptographicException(string message) : base(message)
  105.         {
  106.             SetErrorCode(__HResults.CORSEC_E_CRYPTO);
  107.         }
  108.        
  109.         public CryptographicException(string format, string insert) : base(String.Format(CultureInfo.CurrentCulture, format, insert))
  110.         {
  111.             SetErrorCode(__HResults.CORSEC_E_CRYPTO);
  112.         }
  113.        
  114.         public CryptographicException(string message, Exception inner) : base(message, inner)
  115.         {
  116.             SetErrorCode(__HResults.CORSEC_E_CRYPTO);
  117.         }
  118.        
  119.         public CryptographicException(int hr) : this(Win32Native.GetMessage(hr))
  120.         {
  121.             if ((hr & 2147483648u) != 2147483648u)
  122.                 hr = (hr & 65535) | unchecked((int)2147942400u);
  123.             SetErrorCode(hr);
  124.         }
  125.        
  126.         protected CryptographicException(SerializationInfo info, StreamingContext context) : base(info, context)
  127.         {
  128.         }
  129.        
  130.         // This method is only called from inside the VM.
  131.         private static void ThrowCryptogaphicException(int hr)
  132.         {
  133.             throw new CryptographicException(hr);
  134.         }
  135.     }
  136.    
  137.     [Serializable()]
  138.     [System.Runtime.InteropServices.ComVisible(true)]
  139.     public class CryptographicUnexpectedOperationException : CryptographicException
  140.     {
  141.         public CryptographicUnexpectedOperationException() : base()
  142.         {
  143.             SetErrorCode(__HResults.CORSEC_E_CRYPTO_UNEX_OPER);
  144.         }
  145.        
  146.         public CryptographicUnexpectedOperationException(string message) : base(message)
  147.         {
  148.             SetErrorCode(__HResults.CORSEC_E_CRYPTO_UNEX_OPER);
  149.         }
  150.        
  151.         public CryptographicUnexpectedOperationException(string format, string insert) : base(String.Format(CultureInfo.CurrentCulture, format, insert))
  152.         {
  153.             SetErrorCode(__HResults.CORSEC_E_CRYPTO_UNEX_OPER);
  154.         }
  155.        
  156.         public CryptographicUnexpectedOperationException(string message, Exception inner) : base(message, inner)
  157.         {
  158.             SetErrorCode(__HResults.CORSEC_E_CRYPTO_UNEX_OPER);
  159.         }
  160.        
  161.         protected CryptographicUnexpectedOperationException(SerializationInfo info, StreamingContext context) : base(info, context)
  162.         {
  163.         }
  164.     }
  165. }

Developer Fusion