/// <summary>
        /// Decrypts the array block.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <returns></returns>
        public byte[] DecryptArrayBlock(byte[] data)
        {
            var bBloblSize = new byte[Marshal.SizeOf(typeof(int))];

            byte[] bEncryptedNonce;
            byte[] bEncryptedData;

            using (var ms = new MemoryStream(data))
            {
                ms.Read(bBloblSize, 0, bBloblSize.Length);
                bEncryptedNonce = new byte[BitConverter.ToInt32(bBloblSize, 0)];
                ms.Read(bEncryptedNonce, 0, bEncryptedNonce.Length);

                ms.Read(bBloblSize, 0, bBloblSize.Length);
                bEncryptedData = new byte[BitConverter.ToInt32(bBloblSize, 0)];
                ms.Read(bEncryptedData, 0, bEncryptedData.Length);
            }

            NonceContainer nonceContainer;

            using (var ff = new FastBinaryFormatter())
            {
                nonceContainer = ff.Deserialize <NonceContainer>(DecryptBytes(bEncryptedNonce));
            }

            return(CryptoServices.AesDecrypt(bEncryptedData, nonceContainer.Nonce));
        }
        /// <summary>
        /// Encrypts the array block.
        /// </summary>
        /// <param name="data">The data.</param>
        /// <returns></returns>
        public byte[] EncryptArrayBlock(byte[] data)
        {
            var nonceContainer = new NonceContainer(CryptoServices.CreateNonce(128));

            byte[] bEncryptedNonce;
            using (var ff = new FastBinaryFormatter())
            {
                bEncryptedNonce = EncryptBytes(ff.Serialize(nonceContainer));
            }

            var bEncryptedData = CryptoServices.AesEncrypt(data, nonceContainer.Nonce);

            using (var ms = new MemoryStream())
            {
                var bBlobSize = BitConverter.GetBytes(bEncryptedNonce.Length);
                ms.Write(bBlobSize, 0, bBlobSize.Length);
                ms.Write(bEncryptedNonce, 0, bEncryptedNonce.Length);

                bBlobSize = BitConverter.GetBytes(bEncryptedData.Length);
                ms.Write(bBlobSize, 0, bBlobSize.Length);
                ms.Write(bEncryptedData, 0, bEncryptedData.Length);

                return(ms.ToArray());
            }
        }
 /// <summary>
 /// Decrypts the specified key.
 /// </summary>
 /// <param name="key">The key.</param>
 /// <param name="IV">The iv.</param>
 /// <param name="data">The data.</param>
 /// <param name="cm">The cm.</param>
 /// <param name="symName">Name of the sym.</param>
 /// <returns></returns>
 public static byte[] Decrypt(byte[] key, byte[] IV, byte[] data, CipherMode cm, string symName)
 {
     using (var sa = CryptoServices.CreateSymmetricAlgorithm(symName))
     {
         return(Decrypt(key, IV, data, cm, sa));
     }
 }
Exemple #4
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Symmetric"/> class.
        /// </summary>
        /// <param name="passPhrase">The pass phrase.</param>
        /// <param name="iv">The iv.</param>
        /// <exception cref="System.ArgumentException">
        /// Bad pass phrase
        /// or
        /// Bad initialization vector
        /// </exception>
        public Symmetric(string passPhrase, string iv)
        {
            Disposed = false;

            if (null == passPhrase || 8 > passPhrase.Length)
            {
                throw new ArgumentException("Bad pass phrase");
            }

            if (null == iv || 8 > iv.Length)
            {
                throw new ArgumentException("Bad initialization vector");
            }

            _csp         = new RijndaelManaged();
            _csp.Mode    = CipherMode.CBC;
            _csp.Padding = PaddingMode.PKCS7;
            _blockSize   = (_csp.BlockSize / 8) * BLOCK_MULTIPLIER;

            Encoding encoder = new UTF8Encoding(false, true);

            _ivBytes = CryptoServices.ComputeHash(encoder.GetBytes(iv), "SHA256");
            Array.Resize(ref _ivBytes, 16);

            var pdb = new PasswordDeriveBytes(passPhrase, _ivBytes, "SHA256", 100);

            _keyBytes = pdb.GetBytes(KEY_SIZE);
        }
        /// <summary>
        /// Encrypts the specified key.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="data">The data.</param>
        /// <param name="cm">The cm.</param>
        /// <param name="symName">Name of the sym.</param>
        /// <param name="hashName">Name of the hash.</param>
        /// <returns></returns>
        public static byte[] Encrypt(byte[] key, byte[] data, CipherMode cm, string symName, string hashName)
        {
            var bSize         = BitConverter.GetBytes(data.Length);
            var dataToEncrypt = new byte[data.Length + bSize.Length];

            Array.Copy(bSize, 0, dataToEncrypt, 0, bSize.Length);
            Array.Copy(data, 0, dataToEncrypt, bSize.Length, data.Length);

            using (var sa = CryptoServices.CreateSymmetricAlgorithm(symName))
            {
                try
                {
                    using (var ha = CryptoServices.CreateHashAlgorithm(hashName))
                    {
                        try
                        {
                            var IV    = sa.IV;
                            var rKey  = CryptoServices.InsureVector(ha.ComputeHash(key), sa.Key.Length, ha);
                            var rIV   = CryptoServices.InsureVector(ha.ComputeHash(IV), sa.IV.Length, ha);
                            var eData = Encrypt(rKey, rIV, dataToEncrypt, cm, sa);
                            var rData = new byte[IV.Length + eData.Length];

                            Array.Copy(IV, 0, rData, 0, IV.Length);
                            Array.Copy(eData, 0, rData, IV.Length, eData.Length);

                            return(rData);
                        }
                        finally
                        {
                            ha.Clear();
                        }
                    }
                }
                finally
                {
                    sa.Clear();
                }
            }
        }
        /// <summary>
        /// Decrypts the specified key.
        /// </summary>
        /// <param name="key">The key.</param>
        /// <param name="data">The data.</param>
        /// <param name="cm">The cm.</param>
        /// <param name="symName">Name of the sym.</param>
        /// <param name="hashName">Name of the hash.</param>
        /// <returns></returns>
        public static byte[] Decrypt(byte[] key, byte[] data, CipherMode cm, string symName, string hashName)
        {
            using (var sa = CryptoServices.CreateSymmetricAlgorithm(symName))
            {
                try
                {
                    using (var ha = CryptoServices.CreateHashAlgorithm(hashName))
                    {
                        try
                        {
                            var IV    = new byte[sa.IV.Length];
                            var eData = new byte[data.Length - IV.Length];

                            Array.Copy(data, 0, IV, 0, IV.Length);
                            Array.Copy(data, IV.Length, eData, 0, eData.Length);

                            var rKey = CryptoServices.InsureVector(ha.ComputeHash(key), sa.Key.Length, ha);
                            var rIV  = CryptoServices.InsureVector(ha.ComputeHash(IV), sa.IV.Length, ha);

                            var tmpDecryptData = Decrypt(rKey, rIV, eData, cm, sa);
                            var rData          = new byte[BitConverter.ToInt32(tmpDecryptData, 0)];
                            Array.Copy(tmpDecryptData, Marshal.SizeOf(rData.Length), rData, 0, rData.Length);

                            return(rData);
                        }
                        finally
                        {
                            ha.Clear();
                        }
                    }
                }
                finally
                {
                    sa.Clear();
                }
            }
        }