Ejemplo n.º 1
0
 /// <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);
     }
 }
Ejemplo n.º 2
0
 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);
         }
     }
 }
Ejemplo n.º 3
0
 /// <summary>
 /// Decrypts 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[] 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();
                         MemStream.Close();
                         CryptoStream.Close();
                     }
                 }
             }
             SymmetricKey.Clear();
             return(PlainTextBytes);
         }
     }
 }
Ejemplo n.º 4
0
 /// <summary>
 /// Decrypts the specified data.
 /// </summary>
 /// <param name="data">The data.</param>
 /// <param name="key">The key.</param>
 /// <param name="keySize">
 /// Size of the key. Can be 64 (DES only), 128 (AES), 192 (AES and Triple DES), or 256 (AES)
 /// </param>
 /// <returns>The decrypted data.</returns>
 public byte[] Decrypt(byte[] data, PasswordDeriveBytes key, byte[] initialVector, int keySize)
 {
     using SymmetricAlgorithm SymmetricKey = GetAlgorithm();
     byte[] PlainTextBytes = Array.Empty <byte>();
     if (SymmetricKey != null)
     {
         SymmetricKey.Mode = CipherMode.CBC;
         using (ICryptoTransform Decryptor = SymmetricKey.CreateDecryptor(key.GetBytes(keySize / 8), initialVector))
         {
             using MemoryStream MemStream    = new MemoryStream(data);
             using CryptoStream CryptoStream = new CryptoStream(MemStream, Decryptor, CryptoStreamMode.Read);
             PlainTextBytes = CryptoStream.ReadAllBinary();
         }
         SymmetricKey.Clear();
     }
     return(PlainTextBytes);
 }