The Java Cipher class provides the functionality of a cryptographic cipher for data encryption and decryption. It's a part of the Java Cryptography Architecture (JCA) and allows you to perform block and stream encryption using various algorithms such as AES, DES, etc.
Below is a simple example of how to use the Cipher class to encrypt and decrypt data using AES algorithm:
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
public class CipherExample {
public static void main(String[] args) throws Exception {
// Generate a key
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(128); // for AES-128
SecretKey secretKey = keyGen.generateKey();
// Create Cipher instance
Cipher cipher = Cipher.getInstance("AES");
// Encrypt
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
String originalText = "Hello, World!";
byte[] encryptedBytes = cipher.doFinal(originalText.getBytes());
// Decrypt
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decryptedBytes = cipher.doFinal(encryptedBytes);
String decryptedText = new String(decryptedBytes);
System.out.println("Original: " + originalText);
System.out.println("Decrypted: " + decryptedText);
}
}
How do I avoid rehashing overhead with std::set in multithreaded code?
How do I find elements with custom comparators with std::set for embedded targets?
How do I erase elements while iterating with std::set for embedded targets?
How do I provide stable iteration order with std::unordered_map for large datasets?
How do I reserve capacity ahead of time with std::unordered_map for large datasets?
How do I erase elements while iterating with std::unordered_map in multithreaded code?
How do I provide stable iteration order with std::map for embedded targets?
How do I provide stable iteration order with std::map in multithreaded code?
How do I avoid rehashing overhead with std::map in performance-sensitive code?
How do I merge two containers efficiently with std::map for embedded targets?