import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import java.nio.charset.StandardCharsets; import java.util.Base64; public class SymmetricEncryptionExample { public static void main(String[] args) throws Exception { // Generate a secret key for encryption and decryption KeyGenerator keyGen = KeyGenerator.getInstance("AES"); keyGen.init(256); SecretKey secretKey = keyGen.generateKey(); // Create a cipher object for encryption Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, secretKey); // Encrypt the plaintext using the cipher object String plaintext = "This is a secret message."; byte[] ciphertext = cipher.doFinal(plaintext.getBytes(StandardCharsets.UTF_8)); // Encode the ciphertext as a Base64 string for transport/storage String encodedCiphertext = Base64.getEncoder().encodeToString(ciphertext); System.out.println("Encoded ciphertext: " + encodedCiphertext); } }
import javax.crypto.Cipher; import java.nio.charset.StandardCharsets; import java.security.KeyPair; import java.security.KeyPairGenerator; import java.security.PrivateKey; import java.security.PublicKey; import java.util.Base64; public class AsymmetricEncryptionExample { public static void main(String[] args) throws Exception { // Generate a key pair for encryption and decryption KeyPairGenerator keyGen = KeyPairGenerator.getInstance("RSA"); keyGen.initialize(2048); KeyPair keyPair = keyGen.generateKeyPair(); PublicKey publicKey = keyPair.getPublic(); PrivateKey privateKey = keyPair.getPrivate(); // Create a cipher object for encryption Cipher cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding"); cipher.init(Cipher.ENCRYPT_MODE, publicKey); // Encrypt the plaintext using the cipher object String plaintext = "This is a secret message."; byte[] ciphertext = cipher.doFinal(plaintext.getBytes(StandardCharsets.UTF_8)); // Encode the ciphertext as a Base64 string for transport/storage String encodedCiphertext = Base64.getEncoder().encodeToString(ciphertext); System.out.println("Encoded ciphertext: " + encodedCiphertext); // Create a cipher object for decryption cipher = Cipher.getInstance("RSA/ECB/OAEPWithSHA-256AndMGF1Padding"); cipher.init(Cipher.DECRYPT_MODE, privateKey); // Decrypt the ciphertext using the cipher object byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encodedCiphertext)); String decryptedPlaintext = new String(decryptedBytes, StandardCharsets.UTF_8); System.out.println("Decrypted plaintext: " + decryptedPlaintext); } }In this example, a key pair is generated using the RSA algorithm with a key size of 2048 bits. The plaintext message is then encrypted using a cipher object that is configured to use the RSA/ECB/OAEPWithSHA-256AndMGF1Padding algorithm. The resulting ciphertext is then encoded as a Base64 string for transport or storage. This example also demonstrates how to decrypt the ciphertext using a cipher object configured for decryption with the private key. This example also uses the javax.crypto package library.