Esempio n. 1
0
        /// <summary>
        /// Initializes a new instance of the <see cref="AesCipher"/> class.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="mode">The mode.</param>
        /// <param name="padding">The padding.</param>
        /// <exception cref="ArgumentNullException"><paramref name="key"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException">Keysize is not valid for this algorithm.</exception>
        public AesCipher(byte[] key, CipherMode mode, CipherPadding padding)
            : base(key, 16, mode, padding)
        {
            var keySize = key.Length * 8;

            if (!(keySize == 256 || keySize == 192 || keySize == 128))
            {
                throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, "KeySize '{0}' is not valid for this algorithm.", keySize));
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Initializes a new instance of the <see cref="CastCipher"/> class.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="mode">The mode.</param>
        /// <param name="padding">The padding.</param>
        /// <exception cref="ArgumentNullException"><paramref name="key"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException">Keysize is not valid for this algorithm.</exception>
        public CastCipher(byte[] key, CipherMode mode, CipherPadding padding)
            : base(key, 8, mode, padding)
        {
            var keySize = key.Length * 8;

            if (!(keySize >= 40 && keySize <= 128 && keySize % 8 == 0))
            {
                throw new ArgumentException(string.Format("KeySize '{0}' is not valid for this algorithm.", keySize));
            }

            SetKey(key);
        }
Esempio n. 3
0
        private int _x0, _x1, _x2, _x3;    // registers

        /// <summary>
        /// Initializes a new instance of the <see cref="SerpentCipher"/> class.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="mode">The mode.</param>
        /// <param name="padding">The padding.</param>
        /// <exception cref="ArgumentNullException"><paramref name="key"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException">Keysize is not valid for this algorithm.</exception>
        public SerpentCipher(byte[] key, CipherMode mode, CipherPadding padding)
            : base(key, 16, mode, padding)
        {
            var keySize = key.Length * 8;

            if (!(keySize == 128 || keySize == 192 || keySize == 256))
            {
                throw new ArgumentException(string.Format("KeySize '{0}' is not valid for this algorithm.", keySize));
            }

            _workingKey = MakeWorkingKey(key);
        }
Esempio n. 4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="BlowfishCipher"/> class.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="mode">The mode.</param>
        /// <param name="padding">The padding.</param>
        /// <exception cref="ArgumentNullException"><paramref name="key"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException">Keysize is not valid for this algorithm.</exception>
        public BlowfishCipher(byte[] key, CipherMode mode, CipherPadding padding)
            : base(key, 8, mode, padding)
        {
            var keySize = key.Length * 8;

            if (keySize < 1 || keySize > 448)
            {
                throw new ArgumentException(string.Format("KeySize '{0}' is not valid for this algorithm.", keySize));
            }

            _s0 = new uint[SboxSk];
            _s1 = new uint[SboxSk];
            _s2 = new uint[SboxSk];
            _s3 = new uint[SboxSk];
            _p  = new uint[PSize];

            SetKey(key);
        }
Esempio n. 5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="TwofishCipher"/> class.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="mode">The mode.</param>
        /// <param name="padding">The padding.</param>
        /// <exception cref="ArgumentNullException"><paramref name="key"/> is <c>null</c>.</exception>
        /// <exception cref="ArgumentException">Keysize is not valid for this algorithm.</exception>
        public TwofishCipher(byte[] key, CipherMode mode, CipherPadding padding)
            : base(key, 16, mode, padding)
        {
            var keySize = key.Length * 8;

            if (!(keySize == 128 || keySize == 192 || keySize == 256))
            {
                throw new ArgumentException(string.Format("KeySize '{0}' is not valid for this algorithm.", keySize));
            }

            //  TODO:   Refactor this algorithm

            // calculate the MDS matrix
            var m1 = new int[2];
            var mX = new int[2];
            var mY = new int[2];

            for (var i = 0; i < MAX_KEY_BITS; i++)
            {
                var j = P[0 + i] & 0xff;
                m1[0] = j;
                mX[0] = Mx_X(j) & 0xff;
                mY[0] = Mx_Y(j) & 0xff;

                j     = P[(1 * 256) + i] & 0xff;
                m1[1] = j;
                mX[1] = Mx_X(j) & 0xff;
                mY[1] = Mx_Y(j) & 0xff;

                gMDS0[i] = m1[P_00] | mX[P_00] << 8 | mY[P_00] << 16 | mY[P_00] << 24;

                gMDS1[i] = mY[P_10] | mY[P_10] << 8 | mX[P_10] << 16 | m1[P_10] << 24;

                gMDS2[i] = mX[P_20] | mY[P_20] << 8 | m1[P_20] << 16 | mY[P_20] << 24;

                gMDS3[i] = mX[P_30] | m1[P_30] << 8 | mY[P_30] << 16 | mX[P_30] << 24;
            }

            _k64Cnt = key.Length / 8; // pre-padded ?
            SetKey(key);
        }
Esempio n. 6
0
 /// <summary>
 /// Initializes a new instance of the <see cref="DesCipher"/> class.
 /// </summary>
 /// <param name="key">The key.</param>
 /// <param name="mode">The mode.</param>
 /// <param name="padding">The padding.</param>
 /// <exception cref="ArgumentNullException"><paramref name="key"/> is <c>null</c>.</exception>
 public DesCipher(byte[] key, CipherMode mode, CipherPadding padding)
     : base(key, 8, mode, padding)
 {
 }