Introduction
Symmetric encryption, also known as key encryption, is an encryption method in which the same key is used for encryption and decryption. This encryption method is fast and efficient and suitable for encrypting large amounts of data. The most commonly used symmetric encryption algorithm is Advanced Encryption Standard (AES).
Java provides strong support for symmetric encryption, including classes in the javax.crypto package, such as SecretKey, Cipher, and KeyGenerator.
The Java Cipher class in the javax.crypto package provides cryptographic functions for encryption and decryption. It forms the core of the Java Cryptozoology Extensions (JCE) framework.
In Java, the Cipher class provides the function of symmetric encryption, and the KeyGenerator class is used to generate symmetric encryption keys.
The Chinese translation ofLet us look at an example of simple symmetric encryption AES implemented in Java−
import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import java.nio.charset.StandardCharsets; import java.util.Base64; public class Main { public static void main(String[] args) throws Exception { // Generate key SecretKey secretKey = KeyGenerator.getInstance("AES").generateKey(); // Original message String originalMessage = "Hello, world!"; // Create Cipher instance and initialize it to ENCRYPT_MODE Cipher cipher = Cipher.getInstance("AES"); cipher.init(Cipher.ENCRYPT_MODE, secretKey); // Encrypt the message byte[] encryptedMessage = cipher.doFinal(originalMessage.getBytes(StandardCharsets.UTF_8)); // Convert the encrypted message to Base64 encoded string String encodedMessage = Base64.getEncoder().encodeToString(encryptedMessage); System.out.println("Original Message: " + originalMessage); System.out.println("Encrypted Message: " + encodedMessage); // Reinitialize the cipher to DECRYPT_MODE cipher.init(Cipher.DECRYPT_MODE, secretKey); // Decrypt the message byte[] decryptedMessage = cipher.doFinal(Base64.getDecoder().decode(encodedMessage)); System.out.println("Decrypted Message: " + new String(decryptedMessage, StandardCharsets.UTF_8)); } }
When you run the program, you will see output similar to the following -
Original Message: Hello, world! Encrypted Message: iWohhm/c89uBVaJ3j4YFkA== Decrypted Message: Hello, world!
In the above code, we first use the KeyGenerator class to generate the secret key for AES encryption.
Then we create an instance of the Cipher class for AES and initialize it to ENCRYPT_MODE using the secret key.
Next, we define a raw message "Hello, world!" and encrypt it using Cipher's doFinal method. We also convert the encrypted message bytes into a Base64 encoded string to make it easier to process.
Then we print the original message and the encrypted message to the console.
To demonstrate decryption, we use the same key to reinitialize the password to DECRYPT_MODE and decrypt the encrypted message. Finally, we print the decrypted message to the console.
Since a unique secret key is generated every time you run the program, the encrypted message will be different each time.
The important thing to note here is that the decrypted message is the same as the original message, which indicates that our encryption and decryption process is working properly.
Symmetric encryption is a powerful tool for maintaining confidentiality, but it's important to remember that your data is only as secure as your keys. If an unauthorized person obtains your secret key, they can decrypt your data. Therefore, it is crucial to keep the secret key safe.
Implementing symmetric encryption in Java is a simple process, thanks to the javax.crypto package. Understanding how to use the Cipher and KeyGenerator classes to encrypt and decrypt data can significantly improve the security of your Java applications.
The above is the detailed content of Symmetric encryption cryptography in Java. For more information, please follow other related articles on the PHP Chinese website!