Наследование: SymmetricAlgorithm
Пример #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
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="key">密钥</param>
        /// <param name="iv">矢量</param>
        public RC2(string key, string iv)
        {
            this.key = key;
            this.iv  = iv;

            mCrypto         = new RC2CryptoServiceProvider();
            mCrypto.Key     = GetLegalKey();
            mCrypto.IV      = GetLegalIV();
            mCrypto.Mode    = CipherMode.CBC;//CBC模式加密
            mCrypto.Padding = PaddingMode.PKCS7;
        }
Пример #3
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));
            }
        }
Пример #4
0
		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));
		}
Пример #5
0
 public RC2CryptoServiceProvider()
 {
     _impl = RC2.Create();
     _impl.FeedbackSize = 8;
 }
Пример #6
0
 /// <summary>Creates an instance of a cryptographic object to perform the <see cref="T:System.Security.Cryptography.RC2" /> algorithm.</summary>
 /// <returns>An instance of a cryptographic object.</returns>
 /// <exception cref="T:System.Reflection.TargetInvocationException">The algorithm was used with Federal Information Processing Standards (FIPS) mode enabled, but is not FIPS compatible.</exception>
 // Token: 0x0600224A RID: 8778 RVA: 0x00079284 File Offset: 0x00077484
 public new static RC2 Create()
 {
     return(RC2.Create("System.Security.Cryptography.RC2"));
 }