Java Cipher class overview
Creating a Cipher object
Cipher cipher = Cipher.getInstance("AES");
Cipher Modes
- ECB - Electronic Codebook
- CBC - Cipher Block Chaining
- CFB - Cipher Feedback
- OFB - Output Feedback
- CTR - Counter
Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
Initializing a Cipher
- Encryption / decryption cipher operation mode.
- Encryption / decryption key.
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
SecretKey secretKey = new SecretKeySpec(keyBytes, ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
SecretKey secretKey = new SecretKeySpec(keyBytes, ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, secretKey);
Encrypting and Decrypting Data Example
package net.javaguides.examples.security;
import javax.crypto.*;
import javax.crypto.spec.SecretKeySpec;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
/**
*
* @author Ramesh Fadatare
*
*/
public class JavaCipherClassDemo {
private static final String ALGORITHM = "AES";
private static final String TRANSFORMATION = "AES/ECB/PKCS5Padding";
public String encryptMessage(byte[] message, byte[] keyBytes) throws InvalidKeyException, NoSuchPaddingException,
NoSuchAlgorithmException, BadPaddingException, IllegalBlockSizeException {
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
SecretKey secretKey = new SecretKeySpec(keyBytes, ALGORITHM);
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] encryptedMessage = cipher.doFinal(message);
return new String(encryptedMessage);
}
public String decryptMessage(byte[] encryptedMessage, byte[] keyBytes) throws NoSuchPaddingException,
NoSuchAlgorithmException, InvalidKeyException, BadPaddingException, IllegalBlockSizeException {
Cipher cipher = Cipher.getInstance(TRANSFORMATION);
SecretKey secretKey = new SecretKeySpec(keyBytes, ALGORITHM);
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] clearMessage = cipher.doFinal(encryptedMessage);
return new String(clearMessage);
}
public static void main(String[] args) throws InvalidKeyException, NoSuchPaddingException, NoSuchAlgorithmException,
BadPaddingException, IllegalBlockSizeException {
String encKeyString = "1234567890123456";
String message = "Java Guides";
JavaCipherClassDemo cipherClassDemo = new JavaCipherClassDemo();
String encryptedstr = cipherClassDemo.encryptMessage(message.getBytes(), encKeyString.getBytes());
String decryptedStr = cipherClassDemo.decryptMessage(encryptedstr.getBytes(), encKeyString.getBytes());
System.out.println("Original String -> " + message);
System.out.println("Encrypted String -> " + encryptedstr);
System.out.println("Decrypted String -> " + decryptedStr);
}
}
Original String -> Java Guides
Encrypted String -> øgY};Ù’}±Ýë00®H³
Decrypted String -> Java Guides
References
Continue Learning with Java Guides
Want to take your programming skills to the next level? Explore my practical Udemy courses on Java, Spring Boot, Microservices, Full-Stack Development, Docker, and Generative AI. You can also follow my structured learning roadmaps to become a professional developer.
By Ramesh Fadatare - Udemy Instructor Partner
Comments
Post a Comment
Leave Comment