AES Decrypt
The AES (Advanced Encryption Standard) decryption algorithm works as follows:
Ciphertext Input: The ciphertext (encrypted data) is provided, along with the secret key and initialization vector (IV).
Key Expansion: The secret key is used to generate a series of round keys, which are used in the decryption process.
Inverse Cipher Operations: AES uses a sequence of operations to reverse the encryption steps, which include:
Inverse SubBytes: Each byte in the ciphertext is replaced with its inverse value from a predefined lookup table (S-box).
Inverse ShiftRows: The rows of the state (a matrix representation of the data) are shifted back to their original positions.
Inverse MixColumns: The columns of the state are transformed using a matrix inverse operation to restore the original data.
AddRoundKey: A round key derived from the secret key is XOR-ed with the state to undo its effect.
Repetition of Rounds: The decryption process repeats these steps for a set number of rounds (depending on the key size: 128, 192, or 256 bits).
Final Round: The final round excludes the
Inverse MixColumns
operation, and the result is the original plaintext.
In this decryption code, we:
Extract the IV and ciphertext from the input.
Apply AES decryption using the secret key and the IV.
The result is the decrypted plaintext, which can then be converted back to a human-readable format (UTF-8).
AES is a symmetric encryption algorithm, meaning the same key is used for both encryption and decryption.
// Some code
```typescriptreact
import CryptoJS from 'crypto-js';
// Encrypt using CryptoJS with AES
export function encrypt(text: string, secretKey: string) {
const iv = CryptoJS.lib.WordArray.random(16); // Generate random IV
const encrypted = CryptoJS.AES.encrypt(text, CryptoJS.enc.Hex.parse(secretKey), {
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
});
// Return the iv and the encrypted data, separated by a colon
return iv.toString(CryptoJS.enc.Hex) + ':' + encrypted.ciphertext.toString(CryptoJS.enc.Hex);
}
// Decrypt using CryptoJS with AES
export function decrypt(encryptedData: string, secretKey: string) {
const [ivHex, encryptedText] = encryptedData.split(':');
const iv = CryptoJS.enc.Hex.parse(ivHex);
const encryptedBytes = CryptoJS.enc.Hex.parse(encryptedText);
const decrypted = CryptoJS.AES.decrypt(
{ ciphertext: encryptedBytes } as any, // Decrypt using the ciphertext
CryptoJS.enc.Hex.parse(secretKey),
{
iv: iv,
mode: CryptoJS.mode.CBC,
padding: CryptoJS.pad.Pkcs7
}
);
// Return the decrypted text
return decrypted.toString(CryptoJS.enc.Utf8);
}
// Create a 32-byte random key and return it in Hex format
export function createSecretKey() {
const secretKey = CryptoJS.lib.WordArray.random(32); // AES-256 requires a 32-byte key
const secretKeyHex = secretKey.toString(CryptoJS.enc.Hex);
return secretKeyHex;
}
// Restore the key from Hex format to WordArray
export function restoreSecretKey(secretKeyHex: string) {
const secretKey = CryptoJS.enc.Hex.parse(secretKeyHex);
return secretKey;
}
```
Last updated