/// <summary> /// Decrypts a stream using the supplied password /// </summary> /// <param name="password">The password to encrypt with</param> /// <param name="input">The stream with encrypted data</param> /// <param name="output">The unencrypted output stream</param> public static void Decrypt(string password, Stream input, Stream output) { int a; byte[] buffer = new byte[1024 * 4]; SharpAESCrypt c = new SharpAESCrypt(password, input, OperationMode.Decrypt); while ((a = c.Read(buffer, 0, buffer.Length)) != 0) output.Write(buffer, 0, a); }
/// <summary> /// Encrypts a stream using the supplied password /// </summary> /// <param name="password">The password to decrypt with</param> /// <param name="input">The stream with unencrypted data</param> /// <param name="output">The encrypted output stream</param> public static void Encrypt(string password, Stream input, Stream output) { int a; byte[] buffer = new byte[1024 * 4]; SharpAESCrypt c = new SharpAESCrypt(password, output, OperationMode.Encrypt); while ((a = input.Read(buffer, 0, buffer.Length)) != 0) c.Write(buffer, 0, a); c.FlushFinalBlock(); }
/// <summary> /// For Unit testing: Decrypt a stream using the supplied password with changing (small) buffer sizes /// </summary> /// <param name="password">The password to decrypt with</param> /// <param name="input">The input stream</param> /// <param name="output">The output stream</param> private static void UnitStreamDecrypt(string password, Stream input, Stream output, int bufferSizeSelect, int useThreads) { var r = new Random(); var partBufs = Math.Min(bufferSizeSelect, 1024); var buffers = new byte[partBufs][]; for (int bs = 1; bs < partBufs; bs++) buffers[bs] = new byte[bs]; buffers[0] = new byte[bufferSizeSelect]; int a; using (input = new NonFulfillingReaderStream(input)) { var c = new SharpAESCrypt(password, input, OperationMode.Decrypt, true); c.MaxCryptoThreads = useThreads; do { var bufLen = r.Next(bufferSizeSelect) + 1; var useBuf = bufLen < partBufs ? buffers[bufLen] : buffers[0]; a = c.Read(useBuf, 0, bufLen); output.Write(useBuf, 0, a); } while (a != 0); } }