public BufferReference encrypt(IV iv, BufferReference content) { _provider.IV = iv.Data; // don't see a way how we can reuse MemoryStream / CryptoStream here, need // to implement based on TransFormBlock, but even then we need to create the // Encryptor for each instance. _outputStream.SetLength(0); writeIV(_provider.IV, _outputStream); using (var encryptor = _provider.CreateEncryptor()) { // note: cryptostream closes our output stream, which is fine for us!) using (var stream = new CryptoStream(_outputStream, encryptor, CryptoStreamMode.Write)) { stream.Write(content.Buffer, content.Offset, content.Length); stream.FlushFinalBlock(); return _outputStream.asBufferReference(); } } }
public static BufferReference encrypt(Key key, IV? iv_, BufferReference content) { using (var service = new EncryptionService(key)) { var iv = iv_ ?? service.createRandomIV(); return service.encrypt(iv, content); } }
public static BufferReference encrypt(string serializedKey, IV? iv_, BufferReference content) { return encrypt(Key.deserialize(serializedKey), iv_, content); }