/// <exception cref="ArgumentException">Wrong length of IV.</exception>
        public ICryptoTransform Create(CryptoDirection direction, Mode mode, byte[] iv)
        {
            if (iv.Length != BlockSize)
            {
                throw new ArgumentException("Wrong length of IV.");
            }

            InitRoundKey(direction);

            switch (mode)
            {
            default:
            case Mode.ECB:
                if (direction == CryptoDirection.Encrypt)
                {
                    return(new FROGEncryptTransform(_encryptRoundKeys));
                }
                else
                {
                    return(new FROGDecryptTransform(_decryptRoundKeys));
                }

            case Mode.CBC:
                return(CBC.Get(CreateNice(direction), iv, direction));

            case Mode.CFB:
                return(CFB.Get(CreateNice(CryptoDirection.Encrypt), iv, direction));

            case Mode.OFB:
                return(OFB.Get(CreateNice(CryptoDirection.Encrypt), iv, direction));
            }
        }
예제 #2
0
        /// <summary>
        /// Transforms the value.
        /// </summary>
        /// <param name="value">Value to transform.</param>
        /// <param name="direction">Direction to transform.</param>
        /// <exception cref="KeyNotFoundException">Thrown when the key is not declared.</exception>
        /// <exception cref="VectorNotFoundException">Thrown when the initialisation vector is not declared.</exception>
        /// <exception cref="InvalidFormatException">Thrown when either key or vector does not have a proper length.</exception>
        /// <exception cref="ValueNotFoundException">Thrown when the value to transform is NULL or empty.</exception>
        /// <returns>Returns the value transformed.</returns>
        public string Transform(string value, CryptoDirection direction)
        {
            if (String.IsNullOrWhiteSpace(this.Key))
            {
                throw new KeyNotFoundException("Key has not been defined.");
            }
            if (this.Key.Length != 32)
            {
                throw new InvalidFormatException("Invalid key format.");
            }
            if (String.IsNullOrWhiteSpace(this.Vector))
            {
                throw new VectorNotFoundException("Vector has not been defined.");
            }
            if (this.Vector.Length != 16)
            {
                throw new InvalidFormatException("Invalid vector format.");
            }
            if (String.IsNullOrWhiteSpace(value))
            {
                throw new ValueNotFoundException("Value is NULL or empty.");
            }

            string transformed;

            using (var aes = new AesManaged())
            {
                aes.Key = Encoding.UTF8.GetBytes(this.Key);
                aes.IV  = Encoding.UTF8.GetBytes(this.Vector);

                var buffer = direction == CryptoDirection.Encrypt
                                                                 ? Encoding.UTF8.GetBytes(value)
                                                                 : Convert.FromBase64String(value);
                var transform = direction == CryptoDirection.Encrypt
                                                                        ? aes.CreateEncryptor(aes.Key, aes.IV)
                                                                        : aes.CreateDecryptor(aes.Key, aes.IV);

                using (var ms = direction == CryptoDirection.Encrypt ? new MemoryStream() : new MemoryStream(buffer))
                    using (var cs = new CryptoStream(ms, transform, direction == CryptoDirection.Encrypt
                                                                                                                                        ? CryptoStreamMode.Write
                                                                                                                                        : CryptoStreamMode.Read))
                    {
                        if (direction == CryptoDirection.Encrypt)
                        {
                            cs.Write(buffer, 0, buffer.Length);
                            cs.FlushFinalBlock();
                            transformed = Convert.ToBase64String(ms.ToArray());
                        }
                        else
                        {
                            using (var sr = new StreamReader(cs))
                            {
                                transformed = sr.ReadToEnd();
                            }
                        }
                    }
            }
            return(transformed);
        }
        /// <summary>
        /// Initializes a new instance of the <see cref="SelfMadeAes256Cryptor"/> class.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="initializationVector">The initialization vector.</param>
        /// <param name="cryptoDirection">The crypto direction.</param>
        /// <exception cref="System.NotSupportedException">Key size must be 256 bit!</exception>
        public SelfMadeAes256Cryptor(byte[] key, byte[] initializationVector, CryptoDirection cryptoDirection)
        {
            if (key.Length != AesConstants.KeySize256) throw new NotSupportedException("Key size must be 256 bit!");

            _key = key;
            _initializationVector = initializationVector;
            _cryptoDirection = cryptoDirection;

            _expandedKey = AesHelperMethods.CalculateExpandedKey(_key);
        }
예제 #4
0
 public static INiceCryptoTransform GetNice(ulong key56, CryptoDirection direction)
 {
     if (direction == CryptoDirection.Encrypt)
     {
         return(new DESEncryptTransform(key56));
     }
     else
     {
         return(new DESDecryptTransform(key56));
     }
 }
 private void InitRoundKey(CryptoDirection direction)
 {
     if (direction == CryptoDirection.Encrypt && _encryptRoundKeys is null)
     {
         _encryptRoundKeys = GenerateKey(_key, CryptoDirection.Encrypt);
     }
     if (direction == CryptoDirection.Decrypt && _decryptRoundKeys is null)
     {
         _decryptRoundKeys = GenerateKey(_key, CryptoDirection.Decrypt);
     }
 }
        public INiceCryptoTransform CreateNice(CryptoDirection direction)
        {
            InitRoundKey(direction);

            if (direction == CryptoDirection.Encrypt)
            {
                return(new FROGEncryptTransform(_encryptRoundKeys));
            }
            else
            {
                return(new FROGDecryptTransform(_decryptRoundKeys));
            }
        }
        /// <exception cref="ArgumentException">Wrong key length</exception>
        public static INiceCryptoTransform GetNice(byte[] key, Size stateSize, CryptoDirection direction)
        {
            if (!IsValidKeyLength(key))
            {
                throw new ArgumentException("Wrong key length.");
            }

            if (direction == CryptoDirection.Encrypt)
            {
                return(new RijndaelEncryptTransform(stateSize, key));
            }
            else
            {
                return(new RijndaelDecryptTransform(stateSize, key));
            }
        }
예제 #8
0
        public static ICryptoTransform Get(ulong key56, byte[] IV, Mode mode, CryptoDirection direction)
        {
            switch (mode)
            {
            default:
            case Mode.ECB:
                return(Get(key56, direction));

            case Mode.CBC:
                return(CBC.Get(GetNice(key56, direction), IV, direction));

            case Mode.CFB:
                return(CFB.Get(GetNice(key56, CryptoDirection.Encrypt), IV, direction));

            case Mode.OFB:
                return(OFB.Get(GetNice(key56, CryptoDirection.Encrypt), IV, direction));
            }
        }
        /// <summary>
        /// Процедура форматирования ключа
        /// </summary>
        private static byte[][][] FormatExpandedKey(byte[] expandedKey, CryptoDirection direction)
        {
            int bytesInKey = 288;                // 16 + 256 + 16

            byte[][][] result = new byte[8][][]; // indices: round, key(16, 256, 16), byteIndex
            for (int i = 0; i < 8; i++)
            {
                // 1
                byte[] key1 = new byte[16];
                byte[] key2 = new byte[256];
                byte[] key3 = new byte[16];

                Array.Copy(expandedKey, i * bytesInKey, key1, 0, 16);
                Array.Copy(expandedKey, i * bytesInKey + 16, key2, 0, 256);
                Array.Copy(expandedKey, i * bytesInKey + 272, key3, 0, 16);

                // 2
                Format(key2);
                if (direction == CryptoDirection.Decrypt)
                {
                    key2 = Invert(key2);
                }

                // 3.a
                Format(key3);
                // 3.b
                MakeSingleCycle(key3);
                // 3.c
                for (int j = 0; j < 16; j++)
                {
                    if (key3[j] == j + 1)
                    {
                        key3[j] = (byte)((j + 2) % 16);
                    }
                }

                result[i] = new byte[3][]
                {
                    key1, key2, key3
                };
            }
            return(result);
        }
예제 #10
0
    private ICryptoTransform GetCrytoTransfomer(CryptoDirection direction, Byte[] key, Byte[] IV)
    {
        SymmetricAlgorithm algorithm = GetAlgorithm();

        algorithm.Mode = CipherMode.CBC;

        if (key == null)
        {
            key = algorithm.Key;
        }
        else
        {
            algorithm.Key = key;
        }
        if (IV == null)
        {
            IV = algorithm.IV;
        }
        else
        {
            algorithm.IV = IV;
        }
        switch (direction)
        {
        case CryptoDirection.Encrypt:
            return(algorithm.CreateEncryptor());

            break;

        case CryptoDirection.Decrypt:
            return(algorithm.CreateDecryptor());

            break;

        default:
            throw new ArgumentException("Invalid Crypto Direction");
            break;
        }
    }
        /// <summary>
        /// Key expansion procedure
        /// </summary>
        /// <returns>keys with indices: round index, key index (16b, 256b, 16b), byte in key index</returns>
        private static byte[][][] GenerateKey(byte[] key, CryptoDirection direction)
        {
            // 1
            byte[] keyExpanded = Expand(key, 2304);
            // 2
            byte[] masterKeyExpanded = Expand(_masterKey, 2304);
            // 3
            for (int i = 0; i < 2304; i++)
            {
                keyExpanded[i] = (byte)(keyExpanded[i] ^ masterKeyExpanded[i]);
            }
            // 4
            byte[][][] preliminaryKey = FormatExpandedKey(keyExpanded, CryptoDirection.Encrypt);
            // 5
            byte[] iv = new byte[BlockSize];
            Array.Copy(keyExpanded, iv, BlockSize);
            iv[0] ^= (byte)key.Length;

            byte[] result = TransformEmptyText(preliminaryKey, iv);
            // 6
            return(FormatExpandedKey(result, direction));
        }
예제 #12
0
 /// <exception cref="ArgumentException">Wrong length of IV</exception>
 public static ICryptoTransform Get(INiceCryptoTransform transform, byte[] IV, CryptoDirection direction)
 {
     if (direction == CryptoDirection.Encrypt)
     {
         return(new CFBEncryptTransform(transform, IV));
     }
     else
     {
         return(new CFBDecryptTransform(transform, IV));
     }
 }
 /// <summary>
 /// Initializes a new instance of the <see cref="SelfMadeCaesarCryptor"/> class.
 /// </summary>
 /// <param name="key">The key.</param>
 /// <param name="cryptoDirection">The crypto direction.</param>
 public SelfMadeCaesarCryptor(int key, CryptoDirection cryptoDirection)
 {
     _key = key;
     _cryptoDirection = cryptoDirection;
 }
예제 #14
0
        private ICryptoTransform GetCrytoTransfomer(CryptoDirection direction, Byte[] key, Byte[] IV)
        {
            SymmetricAlgorithm algorithm = GetAlgorithm();
            algorithm.Mode = CipherMode.CBC;

            if (key == null)
                key = algorithm.Key;
            else
                algorithm.Key = key;
            if (IV == null)
                IV = algorithm.IV;
            else
                algorithm.IV = IV;
            switch (direction)
            {
                case CryptoDirection.Encrypt:
                    return algorithm.CreateEncryptor();
                    break;
                case CryptoDirection.Decrypt:
                    return algorithm.CreateDecryptor();
                    break;
                default:
                    throw new ArgumentException("Invalid Crypto Direction");
                    break;
            }
        }