コード例 #1
0
        private int j;                          // Key indexer

        public RC2Transform(RC2 rc2Algo, bool encryption, byte[] key, byte[] iv)
            : base(rc2Algo, encryption, iv)
        {
#if ONLY_1_1
            if (key == null)
            {
                throw new ArgumentNullException("key");
            }
#endif
            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));
            }
        }
コード例 #2
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));
            }
        }
コード例 #3
0
ファイル: SymmetricAlgorithm.cs プロジェクト: xxponline/mono
 public bool ValidKeySize(int bitLength)
 {
     return(KeySizes.IsLegalKeySize(LegalKeySizesValue, bitLength));
 }