
There are many ways to encrypt and decrypt String in java. I will discuss how to encrypt or decrypt data using Cipher class. Cipher class is the part of Java Cryptographic Extension (JCE) framework.
The Cipher class is part of javax.crypto package.
How it works?
Encryption = cleartext + secret key +AES algorithm = ciphertext(encrypted text)
Decryption = ciphertext + secret key +AES algorithm = cleartext
Types of encryption
-
Symmetric
Same secret key for encryption and decryption.
-
Asymmetric
Public/private key pair for encryption and decryption, encryption with public key and decryption with same pare private key example – RSA
Typers of ciphers
-
Block Cipher
Process entire block at a time.
-
Stream Cipher
Process incoming data unit by unit, unit size can be 1 byte or a bit.
Standard Cipher implementations.
Cipher object can be created from the below implementations.
Algorithm/mode/padding (key size)
AES/CBC/NoPadding (128)
AES/CBC/PKCS5Padding (128)
AES/ECB/NoPadding (128)
AES/ECB/PKCS5Padding (128)
DES/CBC/NoPadding (56)
DES/CBC/PKCS5Padding (56)
DES/ECB/NoPadding (56)
DES/ECB/PKCS5Padding (56)
DESede/CBC/NoPadding (168)
DESede/CBC/PKCS5Padding (168)
DESede/ECB/NoPadding (168)
DESede/ECB/PKCS5Padding (168)
RSA/ECB/PKCS1Padding (1024, 2048)
RSA/ECB/OAEPWithSHA-1AndMGF1Padding (1024, 2048)
RSA/ECB/OAEPWithSHA-256AndMGF1Padding (1024, 2048)
AES– Advanced Encryption Standard
DES – Data Encryption Standard
DESede – (3DES) Triple DES
RSA – Rivest-Shamir-Adleman
CBC– cipher block chaining
ECB – electronic code book
How to create cipher Object?
Cipher cipherObject = Cipher.getInstance(“DES/CBC/PKCS5Padding”);
Modes of cipher object
- ENCRYPT_MODE – data encryption
- DECRYPT_MODE – decrypt the encrypted data.
- WRAP_MODE – wrap the key into byte to transport securely.
- UNWRAP_MODE – Unwrap the wrap key into java object.
Sample Code
EncryptionDecryption .java
package com.javaant; public class EncryptionDecryption { public static void main(String args[]) { System.out.println("string after encription of (Nirmal Dhara) :: "+CipherUtils.getEncryptedString("Nirmal Dhara")); System.out.println("String after decription of (8TymgE7S7Px6uZXScZlrRQ==) :: "+CipherUtils.getDecriyptedString("8TymgE7S7Px6uZXScZlrRQ==")); } }
CipherUtils .java
package com.javaant; import javax.crypto.Cipher; import javax.crypto.spec.SecretKeySpec; import org.apache.commons.codec.binary.Base64; public class CipherUtils { private static byte[] key = { 0x24, 0x68, 0x78, 0x71, 0x49, 0x73, 0x41, 0x24, 0x28, 0x78, 0x41, 0x49, 0x63, 0x41,0x73, 0x9};// "this Is A SecretKey you can change it, size is 16"; public static String encrypt(String strToEncrypt) { try { Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING"); final SecretKeySpec secretKey = new SecretKeySpec(key, "AES"); cipher.init(Cipher.ENCRYPT_MODE, secretKey); final String encryptedString = Base64.encodeBase64String(cipher .doFinal(strToEncrypt.getBytes())); return encryptedString; } catch (Exception e) { e.printStackTrace(); } return null; } public static String decrypt(String strToDecrypt) { try { Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5PADDING"); final SecretKeySpec secretKey = new SecretKeySpec(key, "AES"); cipher.init(Cipher.DECRYPT_MODE, secretKey); final String decryptedString = new String(cipher.doFinal(Base64.decodeBase64(strToDecrypt))); return decryptedString; } catch (Exception e) { e.printStackTrace(); } return null; } public static String getEncryptedString(String str) { final String strToEncrypt = str; final String encryptedStr = CipherUtils.encrypt(strToEncrypt.trim()); return encryptedStr; } public static String getDecriyptedString(String str) { final String strToDecrypt = str; final String decryptedStr = CipherUtils.decrypt(strToDecrypt.trim()); return decryptedStr; } }
Dear Nirmal,
I read very useful message from this site . truly i appreciate am very proud of you. better to add some mini project that’s good thinking
Thanks to you and i am very glad to know that you like this site. Sure, I have that plan also.very soon i will post some projects.
Hi Nirmal Dhara,
How to encypt and decrypt java bean object using cipher , sealedobject using RSA algorithm , i don’t want to string .
Thanks,
RamiReddy.
Hi Thanks for the comments, I will post that very soon. If you want now please follow the below steps and Encrypt and Decrypt the object.
1. The class you want to Encrypt make Serializable.
2. Make Cipher Object for Encryption and Decryption.
3. Make a SealedObject object using The class you want to Encrypt and Cipher class object
Above 3 steps for Encrypt an object.
For Decryption
1. use SealedObject and Cipher object.
2. Get object and typecast to your class type.
2. use the methods or user class.
Example
Cipher encryptionObject;// Check my previous post how to create
Cipher DecryptionObject.
EncriptThisClass so = new EncriptThisClass();
SealedObject sealedObject = new SealedObject(so, encryptionObject);
EncriptThisClass o = (EncriptThisClass) sealedObject.getObject(DecryptionObject);
o.Test();
/// this class object your are going to encrypt
public static class EncriptThisClass implements Serializable {
private static final long serialVersionUID = 1L;
public void Test() {
System.out.println(“Object encription example”);
}
}
Hope it will help you.