Esempio n. 1
0
        public TwofishEncryption(int keyLen, ref byte[] key, ref byte[] iv, CipherMode cMode,
                                 EncryptionDirection direction)
        {
            // convert our key into an array of ints
            for (var i = 0; i < key.Length / 4; i++)
            {
                Key[i] = (uint)(key[i * 4 + 3] << 24) | (uint)(key[i * 4 + 2] << 16) | (uint)(key[i * 4 + 1] << 8) |
                         key[i * 4 + 0];
            }

            cipherMode = cMode;

            // we only need to convert our IV if we are using CBC
            if (cipherMode == CipherMode.CBC)
            {
                for (var i = 0; i < 4; i++)
                {
                    IV[i] = (uint)(iv[i * 4 + 3] << 24) | (uint)(iv[i * 4 + 2] << 16) | (uint)(iv[i * 4 + 1] << 8) |
                            iv[i * 4 + 0];
                }
            }

            encryptionDirection = direction;
            reKey(keyLen, ref Key);
        }
Esempio n. 2
0
        private static ICryptoTransform GetEncryptorOrDecryptor(string password, EncryptionDirection direction)
        {
            Rfc2898DeriveBytes _passwordBytes =
                new Rfc2898DeriveBytes(password, Encoding.UTF8.GetBytes("I Dont Care About Salt In This Case"), 1000,
                                       HashAlgorithmName.SHA512);

            byte[]     keyBytes = _passwordBytes.GetBytes(32);
            AesManaged managed  = new AesManaged
            {
                KeySize = 256,
                Padding = PaddingMode.Zeros,
                Mode    = CipherMode.CBC
            };

            switch (direction)
            {
            case EncryptionDirection.Encrypt:
                return(managed.CreateEncryptor(keyBytes, Encoding.UTF8.GetBytes("I Dont Care About Vector Bytes In This Case", 0, managed.BlockSize / 8)));

            case EncryptionDirection.Decrypt:
                return(managed.CreateDecryptor(keyBytes, Encoding.UTF8.GetBytes("I Dont Care About Vector Bytes In This Case", 0, managed.BlockSize / 8)));

            default:
                throw new ArgumentOutOfRangeException(nameof(direction), direction, null);
            }
        }
Esempio n. 3
0
        public void Initialize(uint seed, bool use_md5)
        {
            int keyLen = 128;

            _cipher_table = new byte[0x100];
            byte[] key = new byte[16];
            key[0] = key[4] = key[8] = key[12] = (byte)((seed >> 24) & 0xff);
            key[1] = key[5] = key[9] = key[13] = (byte)((seed >> 16) & 0xff);
            key[2] = key[6] = key[10] = key[14] = (byte)((seed >> 8) & 0xff);
            key[3] = key[7] = key[11] = key[15] = (byte)(seed & 0xff);

            byte[] iv = new byte[0];


            // convert our key into an array of ints
            for (int i = 0; i < key.Length / 4; i++)
            {
                Key[i] = (uint)(key[i * 4 + 3] << 24) | (uint)(key[i * 4 + 2] << 16) | (uint)(key[i * 4 + 1] << 8) |
                         key[i * 4 + 0];
            }

            cipherMode = CipherMode.ECB;

            // we only need to convert our IV if we are using CBC
            if (cipherMode == CipherMode.CBC)
            {
                for (int i = 0; i < 4; i++)
                {
                    IV[i] = (uint)(iv[i * 4 + 3] << 24) | (uint)(iv[i * 4 + 2] << 16) | (uint)(iv[i * 4 + 1] << 8) |
                            iv[i * 4 + 0];
                }
            }

            encryptionDirection = EncryptionDirection.Decrypting;
            reKey(keyLen, ref Key);

            for (int i = 0; i < 256; ++i)
            {
                _cipher_table[i] = (byte)i;
            }


            _send_pos = 0;

            refreshCipherTable();

            if (use_md5)
            {
                MD5 md5 = new MD5CryptoServiceProvider();
                _xor_data = md5.ComputeHash(_cipher_table, 0, 256);
                md5.Dispose();
            }
        }
Esempio n. 4
0
        public TwofishEncryption(int keyLen, ref byte[] key, ref byte[] iv, CipherMode cMode, EncryptionDirection direction)
        {
            for (int i = 0; i < key.Length / 4; i++)
            {
                Key[i] = (uint)(key[i * 4 + 3] << 24) | (uint)(key[i * 4 + 2] << 16) | (uint)(key[i * 4 + 1] << 8) | (uint)(key[i * 4 + 0]);
            }

            cipherMode = cMode;

            if (cipherMode == CipherMode.CBC)
            {
                for (int i = 0; i < 4; i++)
                {
                    IV[i] = (uint)(iv[i * 4 + 3] << 24) | (uint)(iv[i * 4 + 2] << 16) | (uint)(iv[i * 4 + 1] << 8) | (uint)(iv[i * 4 + 0]);
                }
            }

            encryptionDirection = direction;
            reKey(keyLen, ref Key);
        }
Esempio n. 5
0
        public TwofishEncryption(int keyLen, ref byte[] key, ref byte[] iv, CipherMode cMode, EncryptionDirection direction)
        {
            // convert our key into an array of ints
            for (int i = 0; i < key.Length / 4; i++)
            {
                Key[i] = (uint)(key[i * 4 + 3] << 24) | (uint)(key[i * 4 + 2] << 16) | (uint)(key[i * 4 + 1] << 8) | (uint)(key[i * 4 + 0]);
            }

            cipherMode = cMode;

            // we only need to convert our IV if we are using CBC
            if (cipherMode == CipherMode.CBC)
            {
                for (int i = 0; i < 4; i++)
                {
                    IV[i] = (uint)(iv[i * 4 + 3] << 24) | (uint)(iv[i * 4 + 2] << 16) | (uint)(iv[i * 4 + 1] << 8) | (uint)(iv[i * 4 + 0]);
                }
            }

            encryptionDirection = direction;
            reKey(keyLen, ref Key);
        }
Esempio n. 6
0
            public TwofishEncryption(int keyLen, ref byte[] key, ref byte[] iv, CipherMode cMode,
                                     EncryptionDirection direction)
            {
                // convert our key into an array of ints
                for (int i = 0; i < key.Length / 4; i++)
                {
                    Key[i] =
                        (uint)(key[i * 4 + 3] << 24) |
                        (uint)(key[i * 4 + 2] << 16) |
                        (uint)(key[i * 4 + 1] << 8) |
                        (uint)(key[i * 4 + 0]);
                }

                cipherMode = cMode;

                // we only need to convert our IV if we are using CBC
                if (cipherMode == CipherMode.CBC)
                {
                    for (int i = 0; i < 4; i++)
                    {
                        IV[i] =
                            (uint)(iv[i * 4 + 3] << 24) |
                            (uint)(iv[i * 4 + 2] << 16) |
                            (uint)(iv[i * 4 + 1] << 8) |
                            (uint)(iv[i * 4 + 0]);
                    }
                }

                encryptionDirection = direction;
                reKey(keyLen, ref Key);

#if DEBUG2
                for (int i = 0; i < Key.Length; i++)
                {
                    Debug.WriteLine(i.ToString("00") + " " + Key[i].ToString("x"));
                }
#endif
            }
Esempio n. 7
0
 public TwofishTransform(Int32 keySize, Byte[] key, Byte[] initializationVector, Int32 blockSize, CipherMode mode, PaddingMode paddingMode, EncryptionDirection encryptionDirection)
     : base(blockSize, mode, paddingMode, encryptionDirection)
 {
     OutputWhitening      = ((InputWhitening + BlockSizeInBytes) / 4);
     RoundSubkeys         = ((OutputWhitening + BlockSizeInBytes) / 4);
     TotalSubkeys         = ((RoundSubkeys + 2) * MaximumRoundCount);
     InitializationVector = new UInt32[initializationVector is null ? 0 : (initializationVector.Length / 4)];
 protected CryptographicTransform(Int32 blockSize, CipherMode mode, PaddingMode paddingMode, EncryptionDirection encryptionDirection)
     : base(ConcurrencyControlMode.SingleThreadLock)
 {
     BlockSizeInBytes    = Convert.ToByte((blockSize.RejectIf().IsLessThanOrEqualTo(0, nameof(blockSize)) / 8));
     EncryptionDirection = encryptionDirection.RejectIf().IsEqualToValue(EncryptionDirection.Unspecified, nameof(encryptionDirection));
     Mode        = mode;
     PaddingMode = paddingMode;
 }