Encryption is process of encoding meaningful information using mathematical algorithms using a standard key, so that only authorised parties can only decode/decrypt it.
Authorised parties usually shares the secret keys each other so that encrypted information can only be decrypted by them.
Plain Text + Key -> Encrypted text
Encrypted Text – Key -> Plain Text
Algorithm Used: AES
Key Size: 128
Below is the implementation of simple AES 128 bit symmetric encryption algorthim in java.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
package my.tutorialflow; import java.util.Base64; import javax.crypto.Cipher; import javax.crypto.spec.IvParameterSpec; import javax.crypto.spec.SecretKeySpec; public class EncryptionDecryption { public static void main(String[] args) { Encryption encryption = new Encryption(); String encryptedString = encryption.encrypt("This is a plain Message from https://tutorialflow.com"); System.out.println("Encryption Starts"); System.out.println("******************************"); System.out.println("Encrypted Value:" + encryptedString); System.out.println("******************************"); Decryption decryption = new Decryption(); System.out.println("Decryption Starts"); String decryptedString = decryption.decrypt(encryptedString); System.out.println("******************************"); System.out.println("Decrypted Value:" + decryptedString); System.out.println("******************************"); } } class Encryption { public Encryption(){} public String encrypt(String plainMessage) { String initialBufferValue16Digit = "a1b2c3d4e5f6g7h8"; String keyString16Digit = "1234567890123456"; String resultEncryptedString = ""; try { IvParameterSpec initializationVector = new IvParameterSpec(initialBufferValue16Digit.getBytes("UTF-8")); SecretKeySpec secretKey = new SecretKeySpec(keyString16Digit.getBytes(), "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING"); cipher.init(Cipher.ENCRYPT_MODE, secretKey, initializationVector); byte[] encryptedString = cipher.doFinal(plainMessage.getBytes()); resultEncryptedString = Base64.getEncoder().encodeToString(encryptedString); }catch(Exception e){ e.printStackTrace(); } return resultEncryptedString; } } class Decryption { public Decryption(){} public String decrypt(String encryptedMessage) { String initialBufferValue16Digit = "a1b2c3d4e5f6g7h8"; String keyString16Digit = "1234567890123456"; String resultPlainString = ""; try { IvParameterSpec initializationVector = new IvParameterSpec(initialBufferValue16Digit.getBytes("UTF-8")); SecretKeySpec secretKey = new SecretKeySpec(keyString16Digit.getBytes(), "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5PADDING"); cipher.init(Cipher.DECRYPT_MODE, secretKey, initializationVector); byte[] encryptedInBytes = Base64.getDecoder().decode(encryptedMessage); byte[] decryptedString = cipher.doFinal(encryptedInBytes); resultPlainString = new String(decryptedString); }catch(Exception e){ e.printStackTrace(); } return resultPlainString; } } |
Result:
1 2 3 4 5 6 7 8 |
Encryption Starts ****************************** Encrypted Value:9L954bsL7XH3Rap/dC1tx3AvUOin1VG/EXrR4b2kwmefyKvkQS/EikjprVRlaVegk4MSrSJg4H9FDC55gENW8A== ****************************** Decryption Starts ****************************** Decrypted Value:This is a plain Message from https://tutorialflow.com ****************************** |