private int j;                          // Key indexer

        public RC2Transform(RC2 rc2Algo, bool encryption, byte[] key, byte[] iv)
            : base(rc2Algo, encryption, iv)
        {
            int t1 = rc2Algo.EffectiveKeySize;

            if (key == null)
            {
                key = KeyBuilder.Key(rc2Algo.KeySize >> 3);
            }
            else
            {
                key = (byte[])key.Clone();
                t1  = Math.Min(t1, key.Length << 3);
            }

            int t = key.Length;

            if (!KeySizes.IsLegalKeySize(rc2Algo.LegalKeySizes, (t << 3)))
            {
                string msg = Locale.GetText("Key is too small ({0} bytes), it should be between {1} and {2} bytes long.",
                                            t, 5, 16);
                throw new CryptographicException(msg);
            }

            // Expand key into a byte array, then convert to word
            // array since we always access the key in 16bit chunks.
            byte[] L = new byte [128];

            int t8 = ((t1 + 7) >> 3);             // divide by 8
            int tm = 255 % (2 << (8 + t1 - (t8 << 3) - 1));

            for (int i = 0; i < t; i++)
            {
                L [i] = key [i];
            }
            for (int i = t; i < 128; i++)
            {
                L [i] = (byte)(pitable [(L [i - 1] + L [i - t]) & 0xff]);
            }

            L [128 - t8] = pitable [L [128 - t8] & tm];

            for (int i = 127 - t8; i >= 0; i--)
            {
                L [i] = pitable [L [i + 1] ^ L [i + t8]];
            }

            K = new UInt16 [64];
            int pos = 0;

            for (int i = 0; i < 64; i++)
            {
                K [i] = (UInt16)(L [pos++] + (L [pos++] << 8));
            }
        }
Example #2
0
        static internal byte[] GetStrongKey()
        {
            int size = DESTransform.BLOCK_BYTE_SIZE * 3;

            byte[] key = KeyBuilder.Key(size);
            while (TripleDES.IsWeakKey(key))
            {
                key = KeyBuilder.Key(size);
            }
            return(key);
        }
        internal static byte[] GetStrongKey()
        {
            int size = DESTransform.BLOCK_BYTE_SIZE * 3;

            byte[] array = KeyBuilder.Key(size);
            while (TripleDES.IsWeakKey(array))
            {
                array = KeyBuilder.Key(size);
            }
            return(array);
        }
Example #4
0
        public RC2Transform(RC2 rc2Algo, bool encryption, byte[] key, byte[] iv) : base(rc2Algo, encryption, iv)
        {
            int num = rc2Algo.EffectiveKeySize;

            if (key == null)
            {
                key = KeyBuilder.Key(rc2Algo.KeySize >> 3);
            }
            else
            {
                key = (byte[])key.Clone();
                num = Math.Min(num, key.Length << 3);
            }
            int num2 = key.Length;

            if (!KeySizes.IsLegalKeySize(rc2Algo.LegalKeySizes, num2 << 3))
            {
                string text = Locale.GetText("Key is too small ({0} bytes), it should be between {1} and {2} bytes long.", new object[]
                {
                    num2,
                    5,
                    16
                });
                throw new CryptographicException(text);
            }
            byte[] array = new byte[128];
            int    num3  = num + 7 >> 3;
            int    num4  = 255 % (2 << 8 + num - (num3 << 3) - 1);

            for (int i = 0; i < num2; i++)
            {
                array[i] = key[i];
            }
            for (int j = num2; j < 128; j++)
            {
                array[j] = RC2Transform.pitable[(int)(array[j - 1] + array[j - num2] & byte.MaxValue)];
            }
            array[128 - num3] = RC2Transform.pitable[(int)array[128 - num3] & num4];
            for (int k = 127 - num3; k >= 0; k--)
            {
                array[k] = RC2Transform.pitable[(int)(array[k + 1] ^ array[k + num3])];
            }
            this.K = new ushort[64];
            int num5 = 0;

            for (int l = 0; l < 64; l++)
            {
                this.K[l] = (ushort)((int)array[num5++] + ((int)array[num5++] << 8));
            }
        }
 /// <summary>Initializes a new instance of the <see cref="T:System.Security.Cryptography.Rfc2898DeriveBytes" /> class using a password, a salt size, and number of iterations to derive the key.</summary>
 /// <param name="password">The password used to derive the key. </param>
 /// <param name="saltSize">The size of the random salt that you want the class to generate. </param>
 /// <param name="iterations">The number of iterations for the operation. </param>
 /// <exception cref="T:System.ArgumentException">The specified salt size is smaller than 8 bytes or the iteration count is less than 1. </exception>
 /// <exception cref="T:System.ArgumentNullException">The password or salt is null. </exception>
 /// <exception cref="T:System.ArgumentOutOfRangeException">
 ///   <paramref name="iterations " />is out of range. This parameter requires a non-negative number.</exception>
 public Rfc2898DeriveBytes(string password, int saltSize, int iterations)
 {
     if (password == null)
     {
         throw new ArgumentNullException("password");
     }
     if (saltSize < 0)
     {
         throw new ArgumentOutOfRangeException("invalid salt length");
     }
     this.Salt           = KeyBuilder.Key(saltSize);
     this.IterationCount = iterations;
     this._hmac          = new HMACSHA1(Encoding.UTF8.GetBytes(password));
 }
Example #6
0
 public HMACSHA384()
     : this(KeyBuilder.Key(8))
 {
     ProduceLegacyHmacValues = legacy_mode;
 }
Example #7
0
 public override void GenerateKey()
 {
     KeyValue = KeyBuilder.Key(KeySizeValue >> 3);
 }
Example #8
0
 public override void GenerateIV()
 {
     IVValue = KeyBuilder.IV(BlockSizeValue >> 3);
 }
Example #9
0
 public HMACMD5()
     : this(KeyBuilder.Key(8))
 {
 }
Example #10
0
 public HMACSHA1()
     : this(KeyBuilder.Key(8))
 {
 }
 public HMACRIPEMD160()
     : this(KeyBuilder.Key(8))
 {
 }
Example #12
0
 public HMACSHA512()
     : this(KeyBuilder.Key(128))
 {
 }
Example #13
0
 /// <summary>Initializes a new instance of the <see cref="T:System.Security.Cryptography.HMACSHA512" /> class with a randomly generated key.</summary>
 public HMACSHA512() : this(KeyBuilder.Key(8))
 {
     this.ProduceLegacyHmacValues = HMACSHA512.legacy_mode;
 }
Example #14
0
 public override void GenerateIV()
 {
     IVValue = KeyBuilder.IV(DESTransform.BLOCK_BYTE_SIZE);
 }
Example #15
0
 public HMACSHA384()
     : this(KeyBuilder.Key(128))
 {
 }
Example #16
0
 public HMACSHA256()
     : this(KeyBuilder.Key(64))
 {
 }