7.7. Symmetric Encryption

Functions:

SymEnc(key []byte, iv []byte, plaintext []byte) ([]byte)

Encrypt the plaintext using AES-CBC mode with the provided key and IV.

Returns the ciphertext, which will contain the IV (you do not need to store the IV separately).

This function will panic if the plaintext is not a multiple of the AES block size, which is 16 bytes. You can use the constant userlib.AESBlockSizeBytes in your code.

Parameters
  • key ([]byte) – 16-byte symmetric key for encryption

  • iv ([]byte) – 16-byte initialization vector

  • plaintext ([]byte) – Message to encrypt, length a multiple of 16 bytes

Returns

Ciphertext, length a multiple of 16 bytes

Return type

[]byte

SymDec(key []byte, ciphertext []byte) ([]byte)

Decrypt the ciphertext using the key.

This function will panic if the ciphertext is not a multiple of the AES block size, which is 16 bytes. You can use the constant userlib.AESBlockSizeBytes in your code.

Parameters
  • key ([]byte) – 16-byte symmetric key for decryption

  • ciphertext ([]byte) – Message to decrypt, length a multiple of 16 bytes

Returns

Plaintext, length a multiple of 16 bytes

Return type

[]byte

Warning

Remember: one key, one purpose. If we use a key for HKDF or HMAC, we should not use the same key for symmetric encryption.