Image Encryption and Decryption Using AES
Version 1.0.1 (19.1 KB) by
David Hill
Image encryption and decryption using AES and the Galois Counter Mode (GCM) Block Cipher
Image encryption/decryption using AES and the Galois Counter Mode (GCM) Block Cipher. Accepts AES secret key lengths of 128,192,256 (hexidecimal). Implimentation is based on NIST Special Publication 800-38D.
Encryption
Inputs: secretKey - AES secret key, hexidecimal (128,192, or 256 bits), iv - initialization vector (random 96-bit hexidecimal), imageFile - string of file name (i.e., 'AES_image.jpg'), aad - additional authenticated data (plaintext string).
Outputs: outputCipher - uint8 encrypted matrix of same size as input image, authTag - authentication tag (hexidecimal).
Decryption
Inputs: secretKey (same as above), iv (same as above), outputCipher - encrypted uint8 matrix of image (same size as original), aad - additional authenticated data (plaintext string) must match encrypted aad string, authTag (same as above output).
Outputs: ImageOut - decrypted uint8 matrix (same as original image that can be uiwriten to whatever output format), authTagResult - either true or false (true is decrypted correctly, i.e. authentication tag match)
The initialization vection is not generated by the code and must be provided. It is extremely important that the initialization vection be different for each encryption using the same secret key (see NIST publication for explanation). Depending on the size of the image, encryption/decryption times can be several minutes long.
Example:
secretKey='5b0efc93842eca9a89eb5d652018d5713d02a5881be9241be29b8e80c9db4a4e';
iv='6a75d6d0bc29918ffe1fc1c1';
%Encryption
[outputCipher,authTag]=ImageEncryption(secretKey,iv,'AES_image.jpg','AES image');
%Decryption
[ImageOut,authTagResult]=ImageDecryption(secretKey,iv,outputCipher,'AES image',authTag);
Cite As
David Hill (2023). Image Encryption and Decryption Using AES (https://www.mathworks.com/matlabcentral/fileexchange/107175-image-encryption-and-decryption-using-aes), MATLAB Central File Exchange. Retrieved .
MATLAB Release Compatibility
Created with
R2021b
Compatible with any release
Platform Compatibility
Windows macOS LinuxTags
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Discover Live Editor
Create scripts with code, output, and formatted text in a single executable document.