示例#1
0
		// Construct a Skip32 Cipher based on the supplied key.
		public Skip32Cipher(string key, Skip32CipherKeyFormat format)
		{
			if (String.IsNullOrEmpty(key))
				throw new ArgumentNullException("key", "Key must not be null or empty");

			byte[] bytes;
			if (format == Skip32CipherKeyFormat.Base64)
			{
				bytes = Convert.FromBase64String(key);
			}
			else if (format == Skip32CipherKeyFormat.HexString)
			{
				if (key.Length != KeySize * 2)
					throw new ArgumentOutOfRangeException("key", String.Format("Hexadecimal key strings must be {0} characters", KeySize * 2));
				bytes = new byte[KeySize];
				for (int i = 0; i < KeySize; ++i)
				{
					string sub = key.Substring(i * 2, 2);
					int byteValue = Int32.Parse(sub, System.Globalization.NumberStyles.HexNumber);
					System.Diagnostics.Debug.Assert(byteValue >= 0);
					System.Diagnostics.Debug.Assert(byteValue <= 255);
					bytes[i] = Convert.ToByte(byteValue);
				}
			}
			else
				throw new ArgumentOutOfRangeException("format", "Invalid key format: " + format);

			if (bytes.Length != KeySize)
				throw new ArgumentOutOfRangeException("key", String.Format("Key must resolve to {0} bytes", KeySize));

			// Save for later
			_key = bytes;
		}
        // Construct a Skip32 Cipher based on the supplied key.
        public Skip32Cipher(string key, Skip32CipherKeyFormat format)
        {
            if (String.IsNullOrEmpty(key))
            {
                throw new ArgumentNullException("key", "Key must not be null or empty");
            }

            byte[] bytes;
            if (format == Skip32CipherKeyFormat.Base64)
            {
                bytes = Convert.FromBase64String(key);
            }
            else if (format == Skip32CipherKeyFormat.HexString)
            {
                if (key.Length != KeySize * 2)
                {
                    throw new ArgumentOutOfRangeException("key", String.Format("Hexadecimal key strings must be {0} characters", KeySize * 2));
                }
                bytes = new byte[KeySize];
                for (int i = 0; i < KeySize; ++i)
                {
                    string sub       = key.Substring(i * 2, 2);
                    int    byteValue = Int32.Parse(sub, System.Globalization.NumberStyles.HexNumber);
                    System.Diagnostics.Debug.Assert(byteValue >= 0);
                    System.Diagnostics.Debug.Assert(byteValue <= 255);
                    bytes[i] = Convert.ToByte(byteValue);
                }
            }
            else
            {
                throw new ArgumentOutOfRangeException("format", "Invalid key format: " + format);
            }

            if (bytes.Length != KeySize)
            {
                throw new ArgumentOutOfRangeException("key", String.Format("Key must resolve to {0} bytes", KeySize));
            }

            // Save for later
            _key = bytes;
        }