Best practices for working with Java Cryptography Architecture (JCA) and Java Cryptography Extension (JCE) ensure secure and efficient encryption, decryption, and secure data management in Java applications.
Java Cryptography, JCA, JCE, best practices, encryption, secure coding
// Example of using JCE for AES encryption
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.crypto.spec.SecretKeySpec;
import java.util.Base64;
public class AESExample {
public static void main(String[] args) throws Exception {
// Generate a new AES key
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
keyGen.init(128); // for AES-128
SecretKey secretKey = keyGen.generateKey();
// Encrypt a sample text
Cipher cipher = Cipher.getInstance("AES");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
String plainText = "Hello, World!";
byte[] encryptedText = cipher.doFinal(plainText.getBytes());
// Convert to base64 for easy representation
String encryptedBase64 = Base64.getEncoder().encodeToString(encryptedText);
System.out.println("Encrypted Text (Base64): " + encryptedBase64);
// Decrypt the sample text
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decryptedText = cipher.doFinal(Base64.getDecoder().decode(encryptedBase64));
System.out.println("Decrypted Text: " + new String(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?