File Exchange

image thumbnail

Matlab AES Encryption Decryption Example

version 1.0.0 (1.59 KB) by Daniel
Advanced Encryption Standard helper class.


Updated 15 Oct 2019

View License

AES Encryption Decryption Example, works for strings and for structures.

Based on:

"Data Encryption Standard (DES) encryption algorithm is considered highly insecure; messages encrypted using DES have been decrypted by brute force within a single day by machines such as the Electronic Frontier Foundation’s (EFF) Deep Crack."

"A more secure encryption algorithm is AES – Advanced Encryption Standard which is a symmetric encryption algorithm. AES encryption is used by U.S. for securing sensitive but unclassified material, so we can say it is secure enough."

1. AES Encryption and Decryption
Let’s see an example of using AES encryption in Matlab program.

classdef AES < handle
%UNTITLED Summary of this class goes here
% Detailed explanation goes here

properties (Access = private)

function obj = AES(secret, algorithm)
%AES Construct an instance of this class
% algorithm options are
import java.lang.String;
import java.util.Arrays;
import javax.crypto.Cipher;

key = String(secret).getBytes("UTF-8");
sha = MessageDigest.getInstance(algorithm);
key = sha.digest(key);
key = Arrays.copyOf(key, 16);
obj.secretKey = javaObject('javax.crypto.spec.SecretKeySpec',key, "AES");
obj.cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");

function encrypted = encrypt(obj, strToEncrypt)
%ENCRYPT Summary of this method goes here
% Detailed explanation goes here
import java.util.Base64;
import java.lang.String;
import javax.crypto.Cipher;

obj.cipher.init(Cipher.ENCRYPT_MODE, obj.secretKey);
encrypted = string(Base64.getEncoder().encodeToString(obj.cipher.doFinal(String(strToEncrypt).getBytes("UTF-8"))));

function encrypted = encryptStructuredData(obj, structuredData)
encrypted = obj.encrypt(jsonencode(structuredData));

function decrypted = decryptStructuredData(obj, encryptedStructuredData)
decrypted = jsondecode(obj.decrypt(encryptedStructuredData));

function decrypted = decrypt(obj, strToDecrypt)
%DECRYPT Summary of this method goes here
% Detailed explanation goes here
import javax.crypto.Cipher;
import java.lang.String;
import java.util.Base64;

obj.cipher.init(Cipher.DECRYPT_MODE, obj.secretKey);
decrypted = string(String(obj.cipher.doFinal(Base64.getDecoder().decode(strToDecrypt))));

2. Encryption and decryption example
Let’s test if we are able to get the decrypted string back from encrypted string.

secretKey = "ssshhhhhhhhhhh!!!!";
algorithm = "SHA-1";
aes = AES(secretKey, algorithm);

originalString = "";
encryptedString = aes.encrypt(originalString);
decryptedString = aes.decrypt(encryptedString) ;



Use the following functions for Matlab structures:
- encryptStructuredData(structureToEncrypt)
- decryptStructuredData(encryptedStructure)

Drop me your question and comments below.
Happy Learning !!

Cite As

Daniel (2021). Matlab AES Encryption Decryption Example (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (11)

samah samir

i want the code of simplified AES PLEASE

Everett Weber

getting confusing error when trying to run AES.m. Wonderd if someone could help.
Not enough input arguments.

Error in AES (line 19)
key = String(secret).getBytes("UTF-8");

but when I run from the matlab command prompt the following 2 lines, they work without error. Any ideas?
import java.lang.String;
key = String("test").getBytes("UTF-8");

For reference
>> version -java

ans =

'Java 1.8.0_181-b13 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode'

mohamad mokyed

May I ask if you have way to measure the security level of any encryption algorithm?

Sriram Desaraju

Taha Hamid

Hi Daniel,

I'm trying to run this code but am facing some issues. First I got the same problem as duong mai so I installed Java 8 as you suggested but now I am getting the following error:
No constructor 'java.lang.String' with matching signature found.

Error in AES (line 23)
key = String(secret).getBytes(string('UTF-8'));

Any suggestions would be appreciated. I am running MATLAB 2016b and when I check the java version through MATLAB command window, I get Java 1.8.0_251-b08.

mohammad ibrahim

please ignore my previous question. Now I understand how does this work
many thansk for this work

mohammad ibrahim

when I create the object obj = AES(secretKey, algorithm);
how do I get the "secretKey" knowing that it was created by another user ?

many thanks


Dear Duong Mai, Java 8 provides java.util.Base64. Please make sure your MATLAB either has java 8 internally or make sure you have included all java dependencies to your project. The following might help you solve your problem:

duong mai

Thanks for your code. Could you fix my error, when I ran the command: encryptedString = aes.encrypt(originalString); There is an error: Error: File: AES.m Line: 30 Column: 20. Arguments to IMPORT must either end with ".*" or else specify a fully qualified class name: "java.util.Base64" fails this test.
And when I add ".*", then run the command again, there is a new error: ??? Undefined variable "Base64" or class "Base64.getEncoder".


Hi @Sorin. Can you provide a breaking example that your small modification fixes?


Hi Daniel,
Thanks for this excellent work :-). I just made a small modification l.43 to work with structures (Matlab 2017b):
%decrypted = jsondecode(obj.decrypt(encryptedStructuredData));
decrypted = jsondecode(char(obj.decrypt(encryptedStructuredData)));

MATLAB Release Compatibility
Created with R2019b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!