Java Example Program for Custom Encryption Decryption Logic

In this tutorial, We will show you how to create simple Java Example Program for Custom Encryption Decryption Logic. The program is tested and output is published in the same article.

CustomEncryptDecrypt.java

package com.dineshkrish;
import java.util.HashMap;
import java.util.Map;
public class CustomEncryptDecrypt {
private Map<String, String> m = new HashMap<String, String>();
private Map<String, String> mm = new HashMap<String, String>();
private String[] keys = { "a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r",
"s", "t", "u", "v", "w", "x", "y", "z", " " };
private String[] values = { "D", "9", "I", "8", "N", "7", "E", "6", "S", "5", "H", "4", "R", "3", "K", "2", "A",
"1", "@", "$", "%", "&", "*", "!", "0", "X", "Q" };
public CustomEncryptDecrypt() {
setMap();
setDMap();
}
public void setMap() {
for (int i = 0; i < keys.length; i++) {
m.put(keys[i], values[i]);
}
}
public void setDMap() {
for (int i = 0; i < values.length; i++) {
mm.put(values[i], keys[i]);
}
}
public String encrypt(String value) {
String data = "";
char[] cc = value.toCharArray();
int len = cc.length;
int count = 0;
for (int i = 0; i < m.size(); i++) {
if (count < len) {
data = data + m.get("" + cc[i] + "");
count++;
}
}
return data;
}
public String decrypt(String value) {
String data = "";
char[] cc = value.toCharArray();
int len = cc.length;
int count = 0;
for (int i = 0; i < mm.size(); i++) {
if (count < len) {
data = data + mm.get("" + cc[i] + "");
count++;
}
}
return data;
}
}

TestEncryptDecrypt.java

package com.dineshkrish;
import java.util.Scanner;
public class TestEncryptDecrypt {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
CustomEncryptDecrypt crypto = new CustomEncryptDecrypt();
System.out.println("Enter the String to perform Encryption and Decryption : ");
String strToEncrypt = scanner.nextLine();
String strToDecrypt = crypto.encrypt(strToEncrypt);
System.out.println("The string \""+strToEncrypt+"\" Encrypted as : "+strToDecrypt);
System.out.println("The string \""+strToDecrypt+"\" Decrypted as : "+crypto.decrypt(strToDecrypt));
}
}

Output

Enter the String to perform Encryption and Decryption :
i love java
The string “i love java” Encrypted as : SQ4K&NQ5D&D
The string “SQ4K&NQ5D&D” Decrypted as : i love java

Tags:

No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *