private void FinishEncryption() { TEncryptionParameters EncParams = GetEncryptionParams(Protection); AesManaged EncEngine = TEncryptionUtils.CreateEngine(EncParams); TAgileEncryptionKey DataKey = TAgileEncryptionKey.CreateForWriting(null, EncEngine.KeySize / 8); DataKey.Key = TEncryptionUtils.GetRandom(DataKey.KeySizeInBytes); TAgileEncryptionKey KeyKey = TAgileEncryptionKey.CreateForWriting(Protection.OpenPassword, EncEngine.KeySize / 8); byte[] WorkLen = new byte[8]; BitOps.SetCardinal(WorkLen, 0, WorkingStream.Length - WorkStreamZeroPos); EncryptStream(EncEngine, DataKey, WorkLen); using (MemoryStream ms = new MemoryStream()) { using (TOle2File Ole2File = new TOle2File(GetEmptyEncryptedFile())) { Ole2File.PrepareForWrite(ms, XlsxConsts.EncryptionInfoString, new string[0]); CreateInfoStream(Ole2File, EncEngine, EncParams, KeyKey, DataKey); } ms.Position = 0; using (TOle2File Ole2File = new TOle2File(ms)) { Ole2File.PrepareForWrite(TargetStream, XlsxConsts.ContentString, new string[0]); WorkingStream.Position = 0; TEncryptionUtils.CopyStream(WorkingStream, Ole2File); } } }
private static byte[] GetBytesFromCryptoStream(CryptoStream cs, int MaxSize) { using (MemoryStream ms = new MemoryStream()) //not the most efficient code in the world, but should be used for small things { TEncryptionUtils.CopyStream(cs, ms, MaxSize); return(ms.ToArray()); } }