Common

UUID

HTTP Request

Cryptography

Common

Reflection

IO

AesCrypto Tutorial

This tutorial provides an overview of the AesCrypto class, which is part of the lightweight-component/aj-util library. AesCrypto offers a collection of utility methods for symmetric encryption and decryption using algorithms like AES, DES, 3DES, and PBE.

Introduction

The AesCrypto class provides methods for encrypting and decrypting data using various symmetric encryption algorithms. It implements the Singleton pattern and offers a comprehensive set of tools for secure data encryption.

Methods

1. getInstance()

Returns the singleton instance of the AesCrypto class.

Example:

AesCrypto crypto = AesCrypto.getInstance();

2. getSecretKey(String algorithmName, SecureRandom secure)

Generates a secret key for the specified algorithm using the provided secure random generator and returns it as a Base64 encoded string.

Example:

SecureRandom secureRandom = new SecureRandom();
String secretKey = AesCrypto.getSecretKey("AES", secureRandom);

3. DES_encode(String res, String key) and DES_decode(String res, String key)

Encrypts or decrypts data using the DES algorithm.

Example:

AesCrypto crypto = AesCrypto.getInstance();
String encrypted = crypto.DES_encode("Hello, World!", "mySecretKey");
String decrypted = crypto.DES_decode(encrypted, "mySecretKey");

4. AES_encode(String res, String key) and AES_decode(String res, String key)

Encrypts or decrypts data using the AES algorithm with a 128-bit key size.

Example:

AesCrypto crypto = AesCrypto.getInstance();
String encrypted = crypto.AES_encode("Hello, World!", "mySecretKey");
String decrypted = crypto.AES_decode(encrypted, "mySecretKey");

5. encryptTripleDES(byte[] key, String data) and decryptTripleDES(byte[] key, byte[] data)

Encrypts or decrypts data using the Triple DES (3DES) algorithm.

Example:

byte[] key = new byte[24]; // Generate or obtain a 24-byte key
new SecureRandom().nextBytes(key);

byte[] encrypted = AesCrypto.encryptTripleDES(key, "Hello, World!");
String decrypted = AesCrypto.decryptTripleDES(key, encrypted);

6. initSalt(), encryptPBE(String key, byte[] salt, String data), and decryptPBE(String key, byte[] salt, byte[] data)

Methods for Password-Based Encryption (PBE).

Example:

byte[] salt = AesCrypto.initSalt();
String key = "mySecretPassword";

byte[] encrypted = AesCrypto.encryptPBE(key, salt, "Hello, World!");
String decrypted = AesCrypto.decryptPBE(key, salt, encrypted);

Conclusion

The AesCrypto class provides a comprehensive set of tools for symmetric encryption and decryption. It supports multiple algorithms (AES, DES, 3DES, PBE) and offers a simple interface for secure data handling. Remember to store encryption keys securely and follow best practices for cryptographic implementations.