Encrypt and Decrypt the String in Java without using any algorithm

Hello guys,
We are here to understand the concept of encryption and decryption done for the string in Java.
Encryption & Decryption
The concept encryption is the process of converting electronic data into it’s another equivalent form, called as “ciphertext” that cannot be easily understood by anybody except the authorized personnel. While the decryption is the reverse process of encryption.
Data: The term data can be simply defined as the information translated into a form that is more convenient to process.
Ciphertext: When the plaintexts written by anybody get encrypted through some keys then the resultant is known as ciphertext.
The main and primary purpose of encryption is to protect the data confidentiality of digital data stored on computer system or transmitted through internet or other networks. It also takes care of the following security aspects:
Authentication: Verification of message can be done by its origin.
Integrity: Ensures that the content of the message has not been changed during the transmission.
Non-repudiation: The message sender cannot deny for the sent message.
There are various algorithms for encryption & decryption such as RSA algorithm, AES, etc. which needs private and public keys. But here we are going to encrypt the string without using any algorithm.
First of all we will discuss about the encrypting the string.
For encrypting the string we are going to consider different cases for different characters in the given string. Just have a look at the code below to understand what is happening to encrypt the string using switch case.
Code example: Encryption of String
As we know that, the string may contain lowercase letters, uppercase letters, numerals, special symbols, etc. In the below code, we did not made the cases for uppercase letters.
import java.util.*;
class Encryption
{
public static void main(String args[ ])
{
String str,Newstr=" ";
System.out.print("Enter the String you want to Encrypt: ");
try {
Scanner in=new Scanner(System.in);
str=in.nextLine();
for (int i=0;i<str.length();i++)
{
char ch=Character.toLowerCase(str.charAt(i));
switch (ch)
{
case 'a':
Newstr=Newstr+"{";
break;
case 'b':
Newstr=Newstr+"}";
break;
case 'c':
Newstr=Newstr+"#";
break;
case 'd':
Newstr=Newstr+"~";
break;
case 'e':
Newstr=Newstr+"+";
break;
case 'f':
Newstr=Newstr+"-";
break;
case 'g':
Newstr=Newstr+"*";
break;
case 'h':
Newstr=Newstr+"@";
break;
case 'i':
Newstr=Newstr+"/";
break;
case 'j':
Newstr=Newstr+"\\";
break;
case 'k':
Newstr=Newstr+"?";
break;
case 'l':
Newstr=Newstr+"$";
break;
case 'm':
Newstr=Newstr+"!";
break;
case 'n':
Newstr=Newstr+"^";
break;
case 'o':
Newstr=Newstr+"(";
break;
case 'p':
Newstr=Newstr+")";
break;
case 'q':
Newstr=Newstr+"<";
break;
case 'r':
Newstr=Newstr+">";
break;
case 's' :
Newstr=Newstr+"=";
break;
case 't':
Newstr=Newstr+";";
break;
case 'u':
Newstr=Newstr+",";
break;
case 'v' :
Newstr=Newstr+"_";
break;
case 'w':
Newstr=Newstr+"[";
break;
case 'x' :
Newstr=Newstr+"]";
break;
case 'y':
Newstr=Newstr+":";
break;
case 'z' :
Newstr=Newstr+"\"";
break;
case ' ' :
Newstr=Newstr+" ";
break;
case '.':
Newstr=Newstr+'3';
break;
case ',':
Newstr=Newstr+"1";
break;
case '(':
Newstr=Newstr+'4';
break;
case '\"':
Newstr=Newstr+'5';
break;
case ')' :
Newstr=Newstr+"7";
break;
case '?' :
Newstr= Newstr+"2";
break;
case '!':
Newstr= Newstr+"8";
break;
case '-' :
Newstr= Newstr+"6";
break;
case '%' :
Newstr = Newstr+"9";
break;
case '1':
Newstr=Newstr+"r";
break;
case '2':
Newstr=Newstr+"k";
break;
case '3':
Newstr=Newstr+"b";
break;
case '4':
Newstr = Newstr+"e";
break;
case '5':
Newstr = Newstr+"q";
break;
case '6':
Newstr = Newstr+"h";
break;
case '7':
Newstr = Newstr+"u";
break;
case '8' :
Newstr= Newstr+"y";
break;
case '9':
Newstr = Newstr+"w";
break;
case '0':
Newstr = Newstr+"z";
break;
default:
Newstr=Newstr+"0";
break;
}
}
}
catch(Exception ioe)
{
ioe.printStackTrace();
}
System.out.println("The encrypted string is: " +Newstr);
}
}
Output 1:
Output 2:
Output 3:
Now for decryption we need to just reverse the process. For demonstration, we are working only for the strings having uppercase letters and numerals and rest you can do it yourself for practice.
Just look at the following code and outputs.
Code example: Decryption of String
import java.util.*;
class Encoder
{
public static void main(String args[ ])
{
String str,Newstr=" ";
System.out.print("Enter the String you want to Decrypt: ");
try {
Scanner in=new Scanner(System.in);
str=in.nextLine();
for (int i=0;i<str.length();i++)
{
char ch=Character.toLowerCase(str.charAt(i));
switch (ch)
{
case '{':
Newstr=Newstr+"A";
break;
case '}':
Newstr=Newstr+"B";
break;
case '#':
Newstr=Newstr+"C";
break;
case '~':
Newstr=Newstr+"D";
break;
case '+':
Newstr=Newstr+"E";
break;
case '-':
Newstr=Newstr+"F";
break;
case '*':
Newstr=Newstr+"G";
break;
case '@':
Newstr=Newstr+"H";
break;
case '/':
Newstr=Newstr+"I";
break;
case '\\':
Newstr=Newstr+"J";
break;
case '?':
Newstr=Newstr+"K";
break;
case '$':
Newstr=Newstr+"L";
break;
case '!':
Newstr=Newstr+"M";
break;
case '^':
Newstr=Newstr+"N";
break;
case '(':
Newstr=Newstr+"O";
break;
case ')':
Newstr=Newstr+"P";
break;
case '<':
Newstr=Newstr+"Q";
break;
case '>':
Newstr=Newstr+"R";
break;
case '=' :
Newstr=Newstr+"S";
break;
case ';':
Newstr=Newstr+"T";
break;
case ',':
Newstr=Newstr+"U";
break;
case '_' :
Newstr=Newstr+"V";
break;
case '[':
Newstr=Newstr+"W";
break;
case ']' :
Newstr=Newstr+"X";
break;
case ':':
Newstr=Newstr+"Y";
break;
case '\"' :
Newstr=Newstr+"Z";
break;
case '1':
Newstr=Newstr+"r";
break;
case '2':
Newstr=Newstr+"k";
break;
case '3':
Newstr=Newstr+"b";
break;
case '4':
Newstr = Newstr+"e";
break;
case '5':
Newstr = Newstr+"q";
break;
case '6':
Newstr = Newstr+"h";
break;
case '7':
Newstr = Newstr+"u";
break;
case '8' :
Newstr= Newstr+"y";
break;
case '9':
Newstr = Newstr+"w";
break;
case '0':
Newstr = Newstr+"z";
break;
case '.':
Newstr = Newstr+" ";
break;
default:
Newstr=Newstr+"0";
break;
}
}
}
catch(Exception ioe)
{
ioe.printStackTrace();
}
System.out.println("The decrypted string is: " +Newstr);
}
}
You can try your hand using various symbols as key for encryption or decryption. It all depends on the sender, how he/she encrypt the data.
Keep visiting here for more tweaks like this 😉
Thank you for your patience 🙂
Discussion