Matlab AES Encryption Decryption Example

版本 1.0.0 (1.6 KB) 作者: Daniel
Advanced Encryption Standard helper class.
2.1K 次下载
更新时间 2019/10/15

查看许可证

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

Based on: https://howtodoinjava.com/security/java-aes-encryption-example/

"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)
secretKey
cipher
end

methods
function obj = AES(secret, algorithm)
%AES Construct an instance of this class
% algorithm options are https://docs.oracle.com/javase/9/docs/specs/security/standard-names.html#messagedigest-algorithms
import java.security.MessageDigest;
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");
end

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"))));
end

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

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

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))));
end
end
end

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 = "howtodoinjava.com";
encryptedString = aes.encrypt(originalString);
decryptedString = aes.decrypt(encryptedString) ;

disp(originalString);
disp(encryptedString);
disp(decryptedString);

Output:

howtodoinjava.com
Tg2Nn7wUZOQ6Xc+1lenkZTQ9ZDf9a2/RBRiqJBCIX6o=
howtodoinjava.com

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

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

引用格式

Daniel (2024). Matlab AES Encryption Decryption Example (https://www.mathworks.com/matlabcentral/fileexchange/73037-matlab-aes-encryption-decryption-example), MATLAB Central File Exchange. 检索来源 .

MATLAB 版本兼容性
创建方式 R2019b
兼容任何版本
平台兼容性
Windows macOS Linux
类别
Help CenterMATLAB Answers 中查找有关 Encryption / Cryptography 的更多信息

Community Treasure Hunt

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

Start Hunting!

版本 已发布 发行说明
1.0.0