Cryptography/CafeLib.Cryptography/AesEncryption.cs
/// <summary>
/// Applies AES encryption to data with the specified key.
/// iv can be specified (must be 16 bytes) or left null.
/// The actual iv used is added to the first 16 bytes of the output result unless noIV = true.
/// Padding is PKCS7.
/// Mode is CBC.
/// </summary>
/// <param name="data">The data to encrypt.</param>
/// <param name="key">The encryption key to use. The same key must be used to decrypt.</param>
/// <param name="iv">null or 16 bytes of random initialization vector data</param>
/// <param name="noInitVector">If true, the initialization vector used is not prepended to the encrypted result.</param>
/// <returns>16 bytes of IV followed by encrypted data bytes.</returns>
Applies AES encryption to data with the specified key.
iv can be specified (must be 16 bytes) or left null.
The actual iv used is added to the first 16 bytes of the output result unless noIV = true.
Padding is PKCS7.
Mode is CBC.
Applies AES encryption to data with the specified key . iv can be specified ( must be 16 bytes ) or left null . The actual iv used is added to the first 16 bytes of the output result unless noIV = true . Padding is PKCS7 . Mode is CBC .
public static byte[] Encrypt(ReadOnlyByteSpan data, byte[] key, byte[] iv = null, bool noInitVector = false)
        {
            iv ??= GenerateIV();
            var aes = CreateAes(key, iv, true);
            var aesData = aes.DoFinal(data);
            return noInitVector ? aesData : iv.Concat(aesData).ToArray();
        }
public static byte [ ] Encrypt ( ReadOnlyByteSpan data , byte [ ] key , byte [ ] iv = null , bool noInitVector = false ) { iv ??= GenerateIV ( ) ; var aes = CreateAes ( key , iv , true ) ; var aesData = aes . DoFinal ( data ) ; return noInitVector ? aesData : iv . Concat ( aesData ) . ToArray ( ) ; }
Applies AES encryption to data with the specified key.
Applies AES encryption to data with the specified key .
16 bytes of IV followed by encrypted data bytes.
16 bytes of IV followed by encrypted data bytes .
data
ReadOnlyByteSpan
The data to encrypt.
The data to encrypt .
key
byte[]
The encryption key to use. The same key must be used to decrypt.
The encryption key to use . The same key must be used to decrypt .
iv
byte[]
null or 16 bytes of random initialization vector data
null or 16 bytes of random initialization vector data
noInitVector
bool
If true, the initialization vector used is not prepended to the encrypted result.
If true the initialization vector used is not prepended to the encrypted result .