Пример #1
0
        /// <summary>
        ///     Instantiates a new symmetric encryption object using the specified provider.
        /// </summary>
        //public Symmetric(Provider provider)
        //{
        //    this.Symmetric(provider,true);
        //}
        public Symmetric(Provider provider, bool useDefaultInitializationVector)
        {
            switch (provider)
            {
                case Provider.DES:
                    _crypto = new DESCryptoServiceProvider();
                    break;
                case Provider.RC2:
                    _crypto = new RC2CryptoServiceProvider();
                    break;
                case Provider.Rijndael:
                    _crypto = new RijndaelManaged();
                    break;
                case Provider.TripleDES:
                    _crypto = new TripleDESCryptoServiceProvider();
                    break;
            }

            //-- make sure key and IV are always set, no matter what
            Key = RandomKey();
            if (useDefaultInitializationVector)
            {
                IntializationVector = new Data(_DefaultIntializationVector);
            }
            else
            {
                IntializationVector = RandomInitializationVector();
            }
        }
Пример #2
0
 public string Decrypt(ref string passPhrase, string pHexStream)
 {
     try
     {
         var objSym = new Symmetric(Symmetric.Provider.Rijndael, true);
         var encryptedData = new Data();
         encryptedData.Hex = pHexStream;
         Data decryptedData = default(Data);
         decryptedData = objSym.Decrypt(encryptedData, new Data(passPhrase));
         return decryptedData.Text;
     }
     catch
     {
         return null;
     }
 }
Пример #3
0
        private string Encrypt(ref string passPhrase, string textToEncrypt)
        {
            if (passPhrase.Length > 16)
            {
                passPhrase = passPhrase.Substring(0, 16);
            }

            if (textToEncrypt.Trim().Length == 0)
            {
                //'the Text to encrypt not set!!!
                return string.Empty;
            }

            var skey = new Data(passPhrase);
            var sym = new Symmetric(Symmetric.Provider.Rijndael, true);
            Data encryptedData = sym.Encrypt(new Data(textToEncrypt), skey);
            return encryptedData.ToHex();
        }
Пример #4
0
 /// <summary>
 ///     Ensures that _crypto object has valid Key and IV
 ///     prior to any attempt to encrypt/decrypt anything
 /// </summary>
 private void ValidateKeyAndIv(bool isEncrypting)
 {
     if (_key.IsEmpty)
     {
         if (isEncrypting)
         {
             _key = RandomKey();
         }
         else
         {
             throw new CryptographicException("No key was provided for the decryption operation!");
         }
     }
     if (_iv.IsEmpty)
     {
         if (isEncrypting)
         {
             _iv = RandomInitializationVector();
         }
         else
         {
             throw new CryptographicException("No initialization vector was provided for the decryption operation!");
         }
     }
     _crypto.Key = _key.Bytes;
     _crypto.IV = _iv.Bytes;
 }
Пример #5
0
 /// <summary>
 ///     generates a random Initialization Vector, if one was not provided
 /// </summary>
 public Data RandomInitializationVector()
 {
     _crypto.GenerateIV();
     var d = new Data(_crypto.IV);
     return d;
 }
Пример #6
0
 /// <summary>
 ///     generates a random Key, if one was not provided
 /// </summary>
 public Data RandomKey()
 {
     _crypto.GenerateKey();
     var d = new Data(_crypto.Key);
     return d;
 }
Пример #7
0
 /// <summary>
 ///     Encrypts the stream to memory using provided key and provided initialization vector
 /// </summary>
 public Data Encrypt(Stream s, Data key, Data iv)
 {
     IntializationVector = iv;
     Key = key;
     return Encrypt(s);
 }
Пример #8
0
 /// <summary>
 ///     Encrypts the stream to memory using specified key
 /// </summary>
 public Data Encrypt(Stream s, Data key)
 {
     Key = key;
     return Encrypt(s);
 }
Пример #9
0
        /// <summary>
        ///     Encrypts the specified Data using preset key and preset initialization vector
        /// </summary>
        public Data Encrypt(Data d)
        {
            var ms = new MemoryStream();

            ValidateKeyAndIv(true);

            var cs = new CryptoStream(ms, _crypto.CreateEncryptor(), CryptoStreamMode.Write);
            cs.Write(d.Bytes, 0, d.Bytes.Length);
            cs.Close();
            ms.Close();

            return new Data(ms.ToArray());
        }
Пример #10
0
 /// <summary>
 ///     Encrypts the specified Data using provided key
 /// </summary>
 public Data Encrypt(Data d, Data key)
 {
     Key = key;
     return Encrypt(d);
 }
Пример #11
0
        /// <summary>
        ///     Decrypts the specified data using preset key and preset initialization vector
        /// </summary>
        public Data Decrypt(Data encryptedData)
        {
            try
            {
                var ms = new MemoryStream(encryptedData.Bytes, 0, encryptedData.Bytes.Length);
                var b = new byte[encryptedData.Bytes.Length];

                ValidateKeyAndIv(false);
                var cs = new CryptoStream(ms, _crypto.CreateDecryptor(), CryptoStreamMode.Read);

                try
                {
                    cs.Read(b, 0, encryptedData.Bytes.Length - 1);
                }
                catch (CryptographicException ex)
                {
                    throw new CryptographicException("Unable to decrypt data. The provided key may be invalid.", ex);
                }
                finally
                {
                    cs.Close();
                }
                return new Data(b);
            }
            catch
            {
                return null;
            }
        }
Пример #12
0
 /// <summary>
 ///     Decrypts the specified stream using provided key and preset initialization vector
 /// </summary>
 public Data Decrypt(Stream encryptedStream, Data key)
 {
     Key = key;
     return Decrypt(encryptedStream);
 }
Пример #13
0
 /// <summary>
 ///     Decrypts the specified data using provided key and preset initialization vector
 /// </summary>
 public Data Decrypt(Data encryptedData, Data key)
 {
     Key = key;
     return Decrypt(encryptedData);
 }