internal static void Encrypt(CryptoRequest request) { if (request.Key.Length != 32) { throw new ArgumentException("Key must be 32 bytes long."); } if (request.IV == null || request.IV.Length != 16) { throw new ArgumentException("IV must be 16 bytes in length"); } CryptoContainer container = null; if (!request.SkipValidations) { container = CryptoContainer.CreateForEncryption(request); container.WriteEmptyHeaderData(); } using (var aes = new AesManaged()) { aes.IV = request.IV; aes.Key = request.Key; aes.Padding = PaddingMode.ISO10126; aes.BlockSize = 128; if (request.SkipValidations) { aes.Padding = PaddingMode.PKCS7; } using (var encryptor = aes.CreateEncryptor()) { CryptoStream cs = new CryptoStream(request.OutData, encryptor, CryptoStreamMode.Write); int bufferSize = aes.BlockSize; byte[] buffer = new byte[bufferSize]; int read = 0; while ((read = request.InData.Read(buffer, 0, bufferSize)) > 0) { cs.Write(buffer, 0, read); cs.Flush(); } cs.FlushFinalBlock(); } } if (!request.SkipValidations) { container.WriteChecksAndEmbeddedData(); } }
public CryptoContainer ValidateEncryption() { if (Key.Length != 32) { throw new ArgumentException("Key must be 32 bytes long."); } if (IV == null || IV.Length != 16) { throw new ArgumentException("IV must be 16 bytes in length"); } CryptoContainer container = null; if (!SkipValidations) { container = CryptoContainer.CreateForEncryption(this); container.WriteEmptyHeaderData(); } return(container); }