Sure, here are three common ways to implement encryption in Java along with code examples:
1.Using Java Cryptography Architecture (JCA) with Cipher Class:
This method uses the Cipher class from the JCA to perform encryption and decryption.
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.nio.charset.StandardCharsets;
import java.util.Base64;
public class JCACipherExample {
public static void main(String[] args) throws Exception {
// Generate a secret key
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(256); // key size
SecretKey secretKey = keyGenerator.generateKey();
// Create Cipher instance
Cipher cipher = Cipher.getInstance("AES");
// Encrypt data
String plainText = "Hello, world!";
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encryptedBytes = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));
String encryptedText = Base64.getEncoder().encodeToString(encryptedBytes);
System.out.println("Encrypted text: " + encryptedText);
// Decrypt data
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedText));
String decryptedText = new String(decryptedBytes, StandardCharsets.UTF_8);
System.out.println("Decrypted text: " + decryptedText);
}
}
2.Using Java Cryptography Extension (JCE) with Bouncy Castle Provider:
Bouncy Castle is a popular cryptographic library that extends the capabilities of the JCA and JCE.
import org.bouncycastle.jce.provider.BouncyCastleProvider;
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.nio.charset.StandardCharsets;
import java.security.Security;
import java.util.Base64;
public class BouncyCastleExample {
public static void main(String[] args) throws Exception {
// Add Bouncy Castle provider
Security.addProvider(new BouncyCastleProvider());
// Generate a secret key
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES", "BC");
keyGenerator.init(256); // key size
SecretKey secretKey = keyGenerator.generateKey();
// Create Cipher instance
Cipher cipher = Cipher.getInstance("AES", "BC");
// Encrypt data
String plainText = "Hello, world!";
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encryptedBytes = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));
String encryptedText = Base64.getEncoder().encodeToString(encryptedBytes);
System.out.println("Encrypted text: " + encryptedText);
// Decrypt data
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedText));
String decryptedText = new String(decryptedBytes, StandardCharsets.UTF_8);
System.out.println("Decrypted text: " + decryptedText);
}
}
3.Using Java KeyStore (JKS) for Key Management:
This method involves storing keys in a Java KeyStore and using them for encryption and decryption.
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.nio.charset.StandardCharsets;
import java.security.KeyStore;
import java.security.KeyStore.SecretKeyEntry;
import java.util.Base64;
public class KeyStoreExample {
public static void main(String[] args) throws Exception {
// Create a KeyStore
KeyStore keyStore = KeyStore.getInstance(KeyStore.getDefaultType());
char[] password = "password".toCharArray();
keyStore.load(null, password);
// Generate a secret key
KeyGenerator keyGenerator = KeyGenerator.getInstance("AES");
keyGenerator.init(256); // key size
SecretKey secretKey = keyGenerator.generateKey();
SecretKeyEntry secretKeyEntry = new SecretKeyEntry(secretKey);
// Store the key in KeyStore
keyStore.setEntry("mykey", secretKeyEntry, new KeyStore.PasswordProtection(password));
keyStore.store(new FileOutputStream("keystore.jks"), password);
// Load the KeyStore
keyStore.load(new FileInputStream("keystore.jks"), password);
SecretKey secretKeyFromStore = (SecretKey) keyStore.getKey("mykey", password);
// Create Cipher instance
Cipher cipher = Cipher.getInstance("AES");
// Encrypt data
String plainText = "Hello, world!";
cipher.init(Cipher.ENCRYPT_MODE, secretKeyFromStore);
byte[] encryptedBytes = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8));
String encryptedText = Base64.getEncoder().encodeToString(encryptedBytes);
System.out.println("Encrypted text: " + encryptedText);
// Decrypt data
cipher.init(Cipher.DECRYPT_MODE, secretKeyFromStore);
byte[] decryptedBytes = cipher.doFinal(Base64.getDecoder().decode(encryptedText));
String decryptedText = new String(decryptedBytes, StandardCharsets.UTF_8);
System.out.println("Decrypted text: " + decryptedText);
}
}
These examples demonstrate three different approaches to implement encryption in Java using the Java Cryptography Architecture (JCA), the Bouncy Castle library, and the Java KeyStore (JKS) for key management. Each approach has its advantages and use cases.