コード例 #1
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);
         }
     }
 }
コード例 #2
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);
         }
     }
 }
コード例 #3
0
 /// <summary>
 /// Encrypts a byte array
 /// </summary>
 /// <param name="Data">Data to be encrypted</param>
 /// <param name="Key">Password to encrypt with</param>
 /// <param name="AlgorithmUsing">Algorithm to use for encryption (defaults to AES)</param>
 /// <param name="Salt">Salt to encrypt with</param>
 /// <param name="HashAlgorithm">Can be either SHA1 or MD5</param>
 /// <param name="PasswordIterations">Number of iterations to do</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, string Key,
                              SymmetricAlgorithm AlgorithmUsing = null, string Salt  = "Kosher",
                              string HashAlgorithm = "SHA1", int PasswordIterations  = 2,
                              string InitialVector = "OFRna73m*aze01xY", int KeySize = 256)
 {
     if (Data.IsNull())
     {
         return(null);
     }
     AlgorithmUsing = AlgorithmUsing.NullCheck(new RijndaelManaged());
     Key.ThrowIfNullOrEmpty("Key");
     Salt.ThrowIfNullOrEmpty("Salt");
     HashAlgorithm.ThrowIfNullOrEmpty("HashAlgorithm");
     InitialVector.ThrowIfNullOrEmpty("InitialVector");
     using (PasswordDeriveBytes DerivedPassword = new PasswordDeriveBytes(Key, Salt.ToByteArray(), HashAlgorithm, PasswordIterations))
     {
         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();
                         MemStream.Close();
                         CryptoStream.Close();
                     }
                 }
             }
             SymmetricKey.Clear();
             return(CipherTextBytes);
         }
     }
 }
コード例 #4
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="AlgorithmUsing">Algorithm to use for decryption</param>
 /// <param name="Salt">Salt to decrypt with</param>
 /// <param name="HashAlgorithm">Can be either SHA1 or MD5</param>
 /// <param name="PasswordIterations">Number of iterations to do</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 static byte[] Decrypt(this byte[] Data, string Key,
                              SymmetricAlgorithm AlgorithmUsing = null, string Salt  = "Kosher",
                              string HashAlgorithm = "SHA1", int PasswordIterations  = 2,
                              string InitialVector = "OFRna73m*aze01xY", int KeySize = 256)
 {
     if (Data.IsNull())
     {
         return(null);
     }
     AlgorithmUsing = AlgorithmUsing.NullCheck(new RijndaelManaged());
     Key.ThrowIfNullOrEmpty("Key");
     Salt.ThrowIfNullOrEmpty("Salt");
     HashAlgorithm.ThrowIfNullOrEmpty("HashAlgorithm");
     InitialVector.ThrowIfNullOrEmpty("InitialVector");
     using (PasswordDeriveBytes DerivedPassword = new PasswordDeriveBytes(Key, Salt.ToByteArray(), HashAlgorithm, PasswordIterations))
     {
         using (SymmetricAlgorithm SymmetricKey = AlgorithmUsing)
         {
             SymmetricKey.Mode = CipherMode.CBC;
             byte[] PlainTextBytes = new byte[Data.Length];
             int    ByteCount      = 0;
             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))
                     {
                         ByteCount = CryptoStream.Read(PlainTextBytes, 0, PlainTextBytes.Length);
                         MemStream.Close();
                         CryptoStream.Close();
                     }
                 }
             }
             SymmetricKey.Clear();
             Array.Resize(ref PlainTextBytes, ByteCount);
             return(PlainTextBytes);
         }
     }
 }
コード例 #5
0
        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);
                }
            }
        }