Introduction to AES-256
AES-256 (Advanced Encryption Standard with 256-bit key size) is a symmetric encryption algorithm widely adopted due to its exceptional security. This guide covers dozens of useful API examples in various programming languages, along with code snippets and an example application using AES-256.
AES-256 in Python
Python’s cryptography library offers easy-to-use APIs for AES-256 encryption and decryption.
from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes from cryptography.hazmat.backends import default_backend from os import urandom key = urandom(32) # 256-bit key iv = urandom(16) # 128-bit IV plaintext = b'Your data here' cipher = Cipher(algorithms.AES(key), modes.CBC(iv), backend=default_backend()) encryptor = cipher.encryptor() ciphertext = encryptor.update(plaintext) + encryptor.finalize() decryptor = cipher.decryptor() decrypted_text = decryptor.update(ciphertext) + decryptor.finalize() print(decrypted_text)
AES-256 in Node.js
Node.js’s crypto module provides straightforward capabilities to perform AES-256 encryption and decryption.
const crypto = require('crypto'); const key = crypto.randomBytes(32); // 256-bit key const iv = crypto.randomBytes(16); // 128-bit IV const cipher = crypto.createCipheriv('aes-256-cbc', key, iv); let encrypted = cipher.update('Your data here', 'utf-8', 'hex'); encrypted += cipher.final('hex'); const decipher = crypto.createDecipheriv('aes-256-cbc', key, iv); let decrypted = decipher.update(encrypted, 'hex', 'utf-8'); decrypted += decipher.final('utf-8'); console.log(decrypted);
AES-256 in Java
Java’s javax.crypto package facilitates AES-256 encryption and decryption.
import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.spec.IvParameterSpec; import java.security.SecureRandom; import java.util.Base64; public class AES256Example { public static void main(String[] args) throws Exception { KeyGenerator keyGen = KeyGenerator.getInstance("AES"); keyGen.init(256); SecretKey key = keyGen.generateKey(); byte[] iv = new byte[16]; new SecureRandom().nextBytes(iv); IvParameterSpec ivSpec = new IvParameterSpec(iv); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, key, ivSpec); byte[] ciphertext = cipher.doFinal("Your data here".getBytes()); cipher.init(Cipher.DECRYPT_MODE, key, ivSpec); byte[] decryptedText = cipher.doFinal(ciphertext); System.out.println(new String(decryptedText)); } }
Example Application: Secure Data Storage
In this example application, we’ll create a simple program to securely store and retrieve data using AES-256 encryption.
import os from cryptography.hazmat.primitives.ciphers import Cipher, algorithms, modes from cryptography.hazmat.backends import default_backend class SecureStorage: def __init__(self): self.key = os.urandom(32) self.backend = default_backend() def encrypt(self, plaintext): iv = os.urandom(16) cipher = Cipher(algorithms.AES(self.key), modes.CBC(iv), backend=self.backend) encryptor = cipher.encryptor() ciphertext = encryptor.update(plaintext) + encryptor.finalize() return iv + ciphertext def decrypt(self, ciphertext): iv = ciphertext[:16] actual_ciphertext = ciphertext[16:] cipher = Cipher(algorithms.AES(self.key), modes.CBC(iv), backend=self.backend) decryptor = cipher.decryptor() decrypted_text = decryptor.update(actual_ciphertext) + decryptor.finalize() return decrypted_text storage = SecureStorage() encrypted_data = storage.encrypt(b'Sensitive information') print(f'Encrypted: {encrypted_data}') print(f'Decrypted: {storage.decrypt(encrypted_data)}')
With this approach, you can ensure that sensitive data is protected with strong encryption, and only authorized access is possible.
Hash: 5ca148a03b3d97c524ec6a151b7599c82cb91ce5a88d8aec0f2ccd03c6e1216b