Inheritance: IDisposable
 public static byte[] Decrypt(this byte[] data,
                              DeriveBytes key,
                              SymmetricAlgorithm algorithmUsing = null,
                              string initialVector = "OFRna73m*aze01xY",
                              int keySize = 256)
 {
     if (data.IsNull())
         return null;
     algorithmUsing = algorithmUsing.NullCheck(() => new RijndaelManaged());
     Guard.NotEmpty(initialVector, "initialVector");
     using (DeriveBytes derivedPassword = key)
     {
         using (SymmetricAlgorithm symmetricKey = algorithmUsing)
         {
             symmetricKey.Mode = CipherMode.CBC;
             byte[] plainTextBytes;
             using (
                 ICryptoTransform decryptor = symmetricKey.CreateDecryptor(derivedPassword.GetBytes(keySize/8),
                                                                           initialVector.ToByteArray()))
             {
                 using (var memStream = new MemoryStream(data))
                 {
                     using (var cryptoStream = new CryptoStream(memStream, decryptor, CryptoStreamMode.Read))
                     {
                         plainTextBytes = cryptoStream.ReadAllBinary();
                     }
                 }
             }
             symmetricKey.Clear();
             return plainTextBytes;
         }
     }
 }
Example #2
0
        private static RijndaelManaged CreateKey(DeriveBytes deriveBytes)
        {
            var result = new RijndaelManaged {Mode = CipherMode.CBC, Padding = PaddingMode.PKCS7};
              result.Key = deriveBytes.GetBytes(result.KeySize / 8);
              result.IV = deriveBytes.GetBytes(result.BlockSize / 8);

              return result;
        }
 /// <summary>
 /// Decrypts a string
 /// </summary>
 /// <param name="Data">Text to be decrypted (Base 64 string)</param>
 /// <param name="Key">Key to use to encrypt the data (can use PasswordDeriveBytes, Rfc2898DeriveBytes, etc. Really anything that implements DeriveBytes)</param>
 /// <param name="EncodingUsing">Encoding that the output string should use (defaults to UTF8)</param>
 /// <param name="AlgorithmUsing">Algorithm to use for decryption (defaults to AES)</param>
 /// <param name="InitialVector">Needs to be 16 ASCII characters long</param>
 /// <param name="KeySize">Can be 64 (DES only), 128 (AES), 192 (AES and Triple DES), or 256 (AES)</param>
 /// <returns>A decrypted string</returns>
 public static string Decrypt(this string Data,
     DeriveBytes Key,
     Encoding EncodingUsing = null,
     SymmetricAlgorithm AlgorithmUsing = null,
     string InitialVector = "OFRna73m*aze01xY",
     int KeySize = 256)
 {
     if (Data.IsNullOrEmpty())
         return "";
     return Convert.FromBase64String(Data)
                   .Decrypt(Key, AlgorithmUsing, InitialVector, KeySize)
                   .ToEncodedString(EncodingUsing);
 }
 /// <summary>
 /// Decrypts a string
 /// </summary>
 /// <param name="Data">Text to be decrypted (Base 64 string)</param>
 /// <param name="Key">
 /// Key to use to encrypt the data (can use PasswordDeriveBytes, Rfc2898DeriveBytes, etc.
 /// Really anything that implements DeriveBytes)
 /// </param>
 /// <param name="EncodingUsing">
 /// Encoding that the output string should use (defaults to UTF8)
 /// </param>
 /// <param name="AlgorithmUsing">Algorithm to use for decryption (defaults to AES)</param>
 /// <param name="InitialVector">Needs to be 16 ASCII characters long</param>
 /// <param name="KeySize">
 /// Can be 64 (DES only), 128 (AES), 192 (AES and Triple DES), or 256 (AES)
 /// </param>
 /// <returns>A decrypted string</returns>
 public static string Decrypt(this string Data,
     DeriveBytes Key,
     Encoding EncodingUsing = null,
     string AlgorithmUsing = "AES",
     string InitialVector = "OFRna73m*aze01xY",
     int KeySize = 256)
 {
     if (string.IsNullOrEmpty(Data))
         return "";
     return Data.FromBase64()
                 .Decrypt(Key, AlgorithmUsing, InitialVector, KeySize)
                 .ToString(EncodingUsing);
 }
 /// <summary>
 ///     Decrypts a string
 /// </summary>
 /// <param name="data">Text to be decrypted (Base 64 string)</param>
 /// <param name="key">Key to use to encrypt the data (can use PasswordDeriveBytes, Rfc2898DeriveBytes, etc. Really anything that implements DeriveBytes)</param>
 /// <param name="encodingUsing">Encoding that the output string should use (defaults to UTF8)</param>
 /// <param name="algorithmUsing">Algorithm to use for decryption (defaults to AES)</param>
 /// <param name="initialVector">Needs to be 16 ASCII characters long</param>
 /// <param name="keySize">Can be 64 (DES only), 128 (AES), 192 (AES and Triple DES), or 256 (AES)</param>
 /// <returns>A decrypted string</returns>
 public static string Decrypt(this string data,
                              DeriveBytes key,
                              Encoding encodingUsing = null,
                              SymmetricAlgorithm algorithmUsing = null,
                              string initialVector = "OFRna73m*aze01xY",
                              int keySize = 256)
 {
     if (data.IsNullOrEmpty())
         return "";
     return Convert.FromBase64String(data)
                   .Decrypt(key, algorithmUsing, initialVector, keySize)
                   .ToEncodedString(encodingUsing);
 }
 /// <summary>
 /// Encrypts a byte array
 /// </summary>
 /// <param name="Data">Data to encrypt</param>
 /// <param name="Key">Key to use to encrypt the data (can use PasswordDeriveBytes, Rfc2898DeriveBytes, etc. Really anything that implements DeriveBytes)</param>
 /// <param name="AlgorithmUsing">Algorithm to use for encryption (defaults to AES)</param>
 /// <param name="InitialVector">Needs to be 16 ASCII characters long</param>
 /// <param name="KeySize">Can be 64 (DES only), 128 (AES), 192 (AES and Triple DES), or 256 (AES)</param>
 /// <param name="EncodingUsing">Encoding that the original string is using (defaults to UTF8)</param>
 /// <returns>An encrypted byte array</returns>
 public static string Encrypt(this string Data,
     DeriveBytes Key,
     Encoding EncodingUsing = null,
     SymmetricAlgorithm AlgorithmUsing = null,
     string InitialVector = "OFRna73m*aze01xY",
     int KeySize = 256)
 {
     if (Data.IsNullOrEmpty())
         return "";
     return Data.ToByteArray(EncodingUsing)
                .Encrypt(Key, AlgorithmUsing, InitialVector, KeySize)
                .ToBase64String();
 }
 public static byte[] Encrypt(this byte[] Data,
     DeriveBytes Key,
     SymmetricAlgorithm AlgorithmUsing = null,
     string InitialVector = "OFRna73m*aze01xY",
     int KeySize = 256)
 {
     if (Data.IsNull())
         return null;
     AlgorithmUsing = AlgorithmUsing.NullCheck(()=>new RijndaelManaged());
     InitialVector.ThrowIfNullOrEmpty("InitialVector");
     using (DeriveBytes DerivedPassword = Key)
     {
         using (SymmetricAlgorithm SymmetricKey = AlgorithmUsing)
         {
             SymmetricKey.Mode = CipherMode.CBC;
             byte[] CipherTextBytes = null;
             using (ICryptoTransform Encryptor = SymmetricKey.CreateEncryptor(DerivedPassword.GetBytes(KeySize / 8), InitialVector.ToByteArray()))
             {
                 using (MemoryStream MemStream = new MemoryStream())
                 {
                     using (CryptoStream CryptoStream = new CryptoStream(MemStream, Encryptor, CryptoStreamMode.Write))
                     {
                         CryptoStream.Write(Data, 0, Data.Length);
                         CryptoStream.FlushFinalBlock();
                         CipherTextBytes = MemStream.ToArray();
                     }
                 }
             }
             SymmetricKey.Clear();
             return CipherTextBytes;
         }
     }
 }
 public static byte[] Decrypt(this byte[] Data,
     DeriveBytes Key,
     SymmetricAlgorithm AlgorithmUsing = null,
     string InitialVector = "OFRna73m*aze01xY",
     int KeySize = 256)
 {
     if (Data.IsNull())
         return null;
     AlgorithmUsing = AlgorithmUsing.NullCheck(()=>new RijndaelManaged());
     InitialVector.ThrowIfNullOrEmpty("InitialVector");
     using (DeriveBytes DerivedPassword = Key)
     {
         using (SymmetricAlgorithm SymmetricKey = AlgorithmUsing)
         {
             SymmetricKey.Mode = CipherMode.CBC;
             byte[] PlainTextBytes = null;
             using (ICryptoTransform Decryptor = SymmetricKey.CreateDecryptor(DerivedPassword.GetBytes(KeySize / 8), InitialVector.ToByteArray()))
             {
                 using (MemoryStream MemStream = new MemoryStream(Data))
                 {
                     using (CryptoStream CryptoStream = new CryptoStream(MemStream, Decryptor, CryptoStreamMode.Read))
                     {
                         PlainTextBytes = CryptoStream.ReadAllBinary();
                     }
                 }
             }
             SymmetricKey.Clear();
             return PlainTextBytes;
         }
     }
 }
 /// <summary>
 /// Encrypts a byte array
 /// </summary>
 /// <param name="Data">Data to encrypt</param>
 /// <param name="Key">
 /// Key to use to encrypt the data (can use PasswordDeriveBytes, Rfc2898DeriveBytes, etc.
 /// Really anything that implements DeriveBytes)
 /// </param>
 /// <param name="AlgorithmUsing">Algorithm to use for encryption (defaults to AES)</param>
 /// <param name="InitialVector">Needs to be 16 ASCII characters long</param>
 /// <param name="KeySize">
 /// Can be 64 (DES only), 128 (AES), 192 (AES and Triple DES), or 256 (AES)
 /// </param>
 /// <returns>An encrypted byte array</returns>
 public static byte[] Encrypt(this byte[] Data,
     DeriveBytes Key,
     string AlgorithmUsing = "AES",
     string InitialVector = "OFRna73m*aze01xY",
     int KeySize = 256)
 {
     var TempManager = IoC.Manager.Bootstrapper.Resolve<Manager>();
     if (TempManager == null)
         return new byte[0];
     return TempManager.Encrypt(Data, Key, AlgorithmUsing, InitialVector, KeySize);
 }
 /// <summary>
 /// Encrypts a byte array
 /// </summary>
 /// <param name="Data">Data to encrypt</param>
 /// <param name="Key">
 /// Key to use to encrypt the data (can use PasswordDeriveBytes, Rfc2898DeriveBytes, etc.
 /// Really anything that implements DeriveBytes)
 /// </param>
 /// <param name="AlgorithmUsing">Algorithm to use for encryption (defaults to AES)</param>
 /// <param name="InitialVector">Needs to be 16 ASCII characters long</param>
 /// <param name="KeySize">
 /// Can be 64 (DES only), 128 (AES), 192 (AES and Triple DES), or 256 (AES)
 /// </param>
 /// <param name="EncodingUsing">
 /// Encoding that the original string is using (defaults to UTF8)
 /// </param>
 /// <returns>An encrypted byte array</returns>
 public static string Encrypt(this string Data,
     DeriveBytes Key,
     Encoding EncodingUsing = null,
     string AlgorithmUsing = "AES",
     string InitialVector = "OFRna73m*aze01xY",
     int KeySize = 256)
 {
     if (string.IsNullOrEmpty(Data))
         return "";
     return Data.ToByteArray(EncodingUsing)
                .Encrypt(Key, AlgorithmUsing, InitialVector, KeySize)
                .ToString(Base64FormattingOptions.None);
 }
 /// <summary>
 /// Encrypts a byte array
 /// </summary>
 /// <param name="Data">Data to be encrypted</param>
 /// <param name="Key">Password to encrypt with</param>
 /// <param name="InitialVector">Needs to be 16 ASCII characters long</param>
 /// <param name="KeySize">
 /// Can be 64 (DES only), 128 (AES), 192 (AES and Triple DES), or 256 (AES)
 /// </param>
 /// <param name="Algorithm">Algorithm</param>
 /// <returns>An encrypted byte array</returns>
 public byte[] Encrypt(byte[] Data, DeriveBytes Key,
     string Algorithm = "AES",
     string InitialVector = "OFRna73m*aze01xY",
     int KeySize = 256)
 {
     Contract.Requires<NullReferenceException>(SymmetricAlgorithms != null, "SymmetricAlgorithms");
     var Found = SymmetricAlgorithms.FirstOrDefault(x => x.CanHandle(Algorithm));
     if (Found == null)
         throw new ArgumentException(Algorithm + " not found");
     return Found.Encrypt(Data, Key, Algorithm, InitialVector, KeySize);
 }
        public static byte[] Encrypt(this byte[] data,
                                     DeriveBytes key,
                                     SymmetricAlgorithm algorithmUsing = null,
                                     string initialVector = "OFRna73m*aze01xY",
                                     int keySize = 256)
        {
            if (data.IsNull())
                return null;
            algorithmUsing = algorithmUsing.NullCheck(() => new RijndaelManaged());

            Guard.NotEmpty(initialVector, "initialVector");
            using (DeriveBytes derivedPassword = key)
            {
                using (SymmetricAlgorithm symmetricKey = algorithmUsing)
                {
                    symmetricKey.Mode = CipherMode.CBC;
                    byte[] cipherTextBytes;
                    using (
                        ICryptoTransform encryptor = symmetricKey.CreateEncryptor(derivedPassword.GetBytes(keySize/8),
                                                                                  initialVector.ToByteArray()))
                    {
                        using (var memStream = new MemoryStream())
                        {
                            using (var cryptoStream = new CryptoStream(memStream, encryptor, CryptoStreamMode.Write))
                            {
                                cryptoStream.Write(data, 0, data.Length);
                                cryptoStream.FlushFinalBlock();
                                cipherTextBytes = memStream.ToArray();
                            }
                        }
                    }
                    symmetricKey.Clear();
                    return cipherTextBytes;
                }
            }
        }
 /// <summary>
 ///     Encrypts a byte array
 /// </summary>
 /// <param name="data">Data to encrypt</param>
 /// <param name="key">Key to use to encrypt the data (can use PasswordDeriveBytes, Rfc2898DeriveBytes, etc. Really anything that implements DeriveBytes)</param>
 /// <param name="algorithmUsing">Algorithm to use for encryption (defaults to AES)</param>
 /// <param name="initialVector">Needs to be 16 ASCII characters long</param>
 /// <param name="keySize">Can be 64 (DES only), 128 (AES), 192 (AES and Triple DES), or 256 (AES)</param>
 /// <param name="encodingUsing">Encoding that the original string is using (defaults to UTF8)</param>
 /// <returns>An encrypted byte array</returns>
 public static string Encrypt(this string data,
                              DeriveBytes key,
                              Encoding encodingUsing = null,
                              SymmetricAlgorithm algorithmUsing = null,
                              string initialVector = "OFRna73m*aze01xY",
                              int keySize = 256)
 {
     if (data.IsNullOrEmpty())
         return "";
     return data.ToByteArray(encodingUsing)
                .Encrypt(key, algorithmUsing, initialVector, keySize)
                .ToBase64String();
 }
 /// <summary>
 /// Decrypts a byte array
 /// </summary>
 /// <param name="Data">Data to be decrypted</param>
 /// <param name="Key">Password to decrypt with</param>
 /// <param name="Algorithm">Algorithm to use for decryption</param>
 /// <param name="InitialVector">Needs to be 16 ASCII characters long</param>
 /// <param name="KeySize">
 /// Can be 64 (DES only), 128 (AES), 192 (AES and Triple DES), or 256 (AES)
 /// </param>
 /// <returns>A decrypted byte array</returns>
 public byte[] Decrypt(byte[] Data, DeriveBytes Key, string Algorithm = "AES", string InitialVector = "OFRna73m*aze01xY", int KeySize = 256)
 {
     if (string.IsNullOrEmpty(InitialVector))
         throw new ArgumentNullException(nameof(InitialVector));
     if (Data == null)
         return null;
     using (SymmetricAlgorithm SymmetricKey = GetProvider(Algorithm))
     {
         byte[] PlainTextBytes = new byte[0];
         if (SymmetricKey != null)
         {
             SymmetricKey.Mode = CipherMode.CBC;
             using (ICryptoTransform Decryptor = SymmetricKey.CreateDecryptor(Key.GetBytes(KeySize / 8), InitialVector.ToByteArray()))
             {
                 using (MemoryStream MemStream = new MemoryStream(Data))
                 {
                     using (CryptoStream CryptoStream = new CryptoStream(MemStream, Decryptor, CryptoStreamMode.Read))
                     {
                         PlainTextBytes = CryptoStream.ReadAllBinary();
                     }
                 }
             }
             SymmetricKey.Clear();
         }
         return PlainTextBytes;
     }
 }
 /// <summary>
 /// Encrypts a byte array
 /// </summary>
 /// <param name="Data">Data to be encrypted</param>
 /// <param name="Key">Password to encrypt with</param>
 /// <param name="InitialVector">Needs to be 16 ASCII characters long</param>
 /// <param name="KeySize">
 /// Can be 64 (DES only), 128 (AES), 192 (AES and Triple DES), or 256 (AES)
 /// </param>
 /// <param name="Algorithm">Algorithm to use</param>
 /// <returns>The encrypted byte array</returns>
 public byte[] Encrypt(byte[] Data, DeriveBytes Key, string Algorithm = "AES", string InitialVector = "OFRna73m*aze01xY", int KeySize = 256)
 {
     if (string.IsNullOrEmpty(InitialVector))
         throw new ArgumentNullException(nameof(InitialVector));
     if (Data == null)
         return null;
     using (SymmetricAlgorithm SymmetricKey = GetProvider(Algorithm))
     {
         byte[] CipherTextBytes = new byte[0];
         if (SymmetricKey != null)
         {
             SymmetricKey.Mode = CipherMode.CBC;
             using (ICryptoTransform Encryptor = SymmetricKey.CreateEncryptor(Key.GetBytes(KeySize / 8), InitialVector.ToByteArray()))
             {
                 using (MemoryStream MemStream = new MemoryStream())
                 {
                     using (CryptoStream CryptoStream = new CryptoStream(MemStream, Encryptor, CryptoStreamMode.Write))
                     {
                         CryptoStream.Write(Data, 0, Data.Length);
                         CryptoStream.FlushFinalBlock();
                         CipherTextBytes = MemStream.ToArray();
                     }
                 }
             }
             SymmetricKey.Clear();
         }
         return CipherTextBytes;
     }
 }
 private static RijndaelManaged GetAesAlgorithm(DeriveBytes bytes)
 {
     RijndaelManaged aes = new RijndaelManaged();
     aes.Mode = CipherMode.CBC;
     aes.Key = bytes.GetBytes(aes.KeySize / 8);
     aes.IV = bytes.GetBytes(aes.BlockSize / 8);
     return aes;
 }