How to Encrypt and Decrypt the String in Java Without Using Any Algorithm

We are here to understand the concept of encryption and decryption done for the various strings.

ciphertext

Encryption and Decryption


The concept of encryption is the process of converting electronic data into another equivalent form, called “ciphertext” that cannot be easily understood by anybody except the authorized personnel. Whereas 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 plaintext written by anyone is encrypted using some keys then the result is known as ciphertext.
 
The main and primary purpose of encryption is to protect the data confidentiality of digital data stored on a computer system or transmitted through the internet or other networks. It also takes care of the following security aspects:
  • Authentication: Verification of the 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 and decryption such as RSA algorithm, AES and so on that needs private and public keys. But here we will encrypt the string without using any algorithm.


First of all, we will explain the string encryption.

For encrypting the string we will consider various cases for varying characters in the given string. Just have a look at the code below to understand what is happening to encrypt the string using swa itch case.

Encryption

Code example: Encryption

As we know, the string may contain lowercase letters, uppercase letters, numerals, special symbols and so on. In the following code, we did not write code for uppercase letters.
  1. import java.util.*;  
  2.   
  3. class Encryption  
  4. {  
  5.     public static void main(String args[ ])  
  6.     {  
  7.         String str,Newstr=" ";  
  8.         System.out.print("Enter the String you want to Encrypt: ");  
  9.         try {  
  10.   
  11.         Scanner in=new Scanner(System.in);  
  12.         str=in.nextLine();  
  13.         for (int i=0;i<str.length();i++)  
  14.         {  
  15.             char ch=Character.toLowerCase(str.charAt(i));  
  16.             switch (ch)  
  17.             {  
  18.                 case 'a':  
  19.                     Newstr=Newstr+"{";  
  20.                     break;  
  21.                 case 'b':  
  22.                     Newstr=Newstr+"}";  
  23.                     break;  
  24.                 case 'c':  
  25.                     Newstr=Newstr+"#";  
  26.                     break;  
  27.                 case 'd':  
  28.                     Newstr=Newstr+"~";  
  29.                     break;  
  30.                 case 'e':  
  31.                     Newstr=Newstr+"+";  
  32.                     break;  
  33.                 case 'f':  
  34.                     Newstr=Newstr+"-";  
  35.                     break;  
  36.                 case 'g':  
  37.                     Newstr=Newstr+"*";  
  38.                     break;  
  39.                 case 'h':  
  40.                     Newstr=Newstr+"@";  
  41.                     break;  
  42.                 case 'i':  
  43.                     Newstr=Newstr+"/";  
  44.                     break;  
  45.                 case 'j':  
  46.                     Newstr=Newstr+"\\";  
  47.                     break;  
  48.                 case 'k':  
  49.                     Newstr=Newstr+"?";  
  50.                     break;  
  51.                 case 'l':  
  52.                     Newstr=Newstr+"$";  
  53.                     break;  
  54.                 case 'm':  
  55.                     Newstr=Newstr+"!";  
  56.                     break;  
  57.                 case 'n':  
  58.                     Newstr=Newstr+"^";  
  59.                     break;  
  60.                 case 'o':  
  61.                     Newstr=Newstr+"(";  
  62.                     break;  
  63.                 case 'p':  
  64.                     Newstr=Newstr+")";  
  65.                     break;  
  66.                 case 'q':  
  67.                     Newstr=Newstr+"<";  
  68.                     break;  
  69.                 case 'r':  
  70.                     Newstr=Newstr+">";  
  71.                     break;  
  72.                 case 's' :  
  73.                     Newstr=Newstr+"=";  
  74.                     break;  
  75.                 case 't':  
  76.                     Newstr=Newstr+";";  
  77.                     break;  
  78.                 case 'u':  
  79.                     Newstr=Newstr+",";  
  80.                     break;  
  81.                 case 'v' :  
  82.                     Newstr=Newstr+"_";  
  83.                     break;  
  84.                 case 'w':  
  85.                     Newstr=Newstr+"[";  
  86.                     break;  
  87.                 case 'x' :  
  88.                     Newstr=Newstr+"]";  
  89.                     break;  
  90.                 case 'y':  
  91.                     Newstr=Newstr+":";  
  92.                     break;  
  93.                 case 'z' :  
  94.                     Newstr=Newstr+"\"";  
  95.                     break;  
  96.                 case ' ' :  
  97.                     Newstr=Newstr+" ";  
  98.                     break;  
  99.                 case '.':  
  100.                     Newstr=Newstr+'3';  
  101.                     break;  
  102.                 case ',':  
  103.                     Newstr=Newstr+"1";  
  104.                     break;  
  105.                 case '(':  
  106.                     Newstr=Newstr+'4';  
  107.                     break;  
  108.                 case '\"':  
  109.                     Newstr=Newstr+'5';  
  110.                     break;  
  111.                 case ')' :  
  112.                     Newstr=Newstr+"7";  
  113.                     break;  
  114.                 case '?' :  
  115.                     Newstr= Newstr+"2";  
  116.                     break;  
  117.                 case '!':  
  118.                     Newstr= Newstr+"8";  
  119.                     break;  
  120.                 case '-' :  
  121.                     Newstr= Newstr+"6";  
  122.                     break;  
  123.                 case '%' :  
  124.                     Newstr = Newstr+"9";  
  125.                     break;  
  126.                 case '1':  
  127.                     Newstr=Newstr+"r";  
  128.                     break;  
  129.                 case '2':  
  130.                     Newstr=Newstr+"k";  
  131.                     break;  
  132.                 case '3':  
  133.                     Newstr=Newstr+"b";  
  134.                     break;  
  135.                 case '4':  
  136.                     Newstr = Newstr+"e";  
  137.                     break;  
  138.                 case '5':  
  139.                     Newstr = Newstr+"q";  
  140.                     break;  
  141.                 case '6':  
  142.                     Newstr = Newstr+"h";  
  143.                     break;  
  144.                 case '7':  
  145.                     Newstr = Newstr+"u";  
  146.                     break;  
  147.                 case '8' :  
  148.                     Newstr= Newstr+"y";  
  149.                     break;  
  150.                 case '9':  
  151.                     Newstr = Newstr+"w";  
  152.                     break;  
  153.                 case '0':  
  154.                     Newstr = Newstr+"z";  
  155.                     break;  
  156.                  default:  
  157.                     Newstr=Newstr+"0";  
  158.                     break;  
  159.             }  
  160.         }  
  161.         }  
  162.         catch(Exception ioe)  
  163.         {  
  164.             ioe.printStackTrace();  
  165.         }  
  166.         System.out.println("The encrypted string is: \n" +Newstr);  
  167.     }  
  168. }  

Various outputs for various strings


Output 1

various strings

Output 2

decryption

Now for decryption we need to do just the reverse process. For demonstration, we are showing only code for the strings having uppercase letters and lowercase as decrypted text and the rest you can do yourself for practice.

lock

Just look at the following code and outputs.

Code example: Decryption
  1. import java.util.*;  
  2.   
  3. class Encoder  
  4. {  
  5.     public static void main(String args[ ])  
  6.     {  
  7.         String str,Newstr=" ";  
  8.         System.out.print("Enter the String you want to Decrypt: ");  
  9.         try {  
  10.         Scanner in=new Scanner(System.in);  
  11.         str=in.nextLine();  
  12.         for (int i=0;i<str.length();i++)  
  13.         {  
  14.             char ch=Character.toLowerCase(str.charAt(i));  
  15.             switch (ch)  
  16.             {  
  17.                 case '{':  
  18.                     Newstr=Newstr+"A";  
  19.                     break;  
  20.                 case '}':  
  21.                     Newstr=Newstr+"B";  
  22.                     break;  
  23.                 case '#':  
  24.                     Newstr=Newstr+"C";  
  25.                     break;  
  26.                 case '~':  
  27.                     Newstr=Newstr+"D";  
  28.                     break;  
  29.                 case '+':  
  30.                     Newstr=Newstr+"E";  
  31.                     break;  
  32.                 case '-':  
  33.                     Newstr=Newstr+"F";  
  34.                     break;  
  35.                 case '*':  
  36.                     Newstr=Newstr+"G";  
  37.                     break;  
  38.                 case '@':  
  39.                     Newstr=Newstr+"H";  
  40.                     break;  
  41.                 case '/':  
  42.                     Newstr=Newstr+"I";  
  43.                     break;  
  44.                 case '\\':  
  45.                     Newstr=Newstr+"J";  
  46.                     break;  
  47.                 case '?':  
  48.                     Newstr=Newstr+"K";  
  49.                     break;  
  50.                 case '$':  
  51.                     Newstr=Newstr+"L";  
  52.                     break;  
  53.                 case '!':  
  54.                     Newstr=Newstr+"M";  
  55.                     break;  
  56.                 case '^':  
  57.                     Newstr=Newstr+"N";  
  58.                     break;  
  59.                 case '(':  
  60.                     Newstr=Newstr+"O";  
  61.                     break;  
  62.                 case ')':  
  63.                     Newstr=Newstr+"P";  
  64.                     break;  
  65.                 case '<':  
  66.                     Newstr=Newstr+"Q";  
  67.                     break;  
  68.                 case '>':  
  69.                     Newstr=Newstr+"R";  
  70.                     break;  
  71.                 case '=' :  
  72.                     Newstr=Newstr+"S";  
  73.                     break;  
  74.                 case ';':  
  75.                     Newstr=Newstr+"T";  
  76.                     break;  
  77.                 case ',':  
  78.                     Newstr=Newstr+"U";  
  79.                     break;  
  80.                 case '_' :  
  81.                     Newstr=Newstr+"V";  
  82.                     break;  
  83.                 case '[':  
  84.                     Newstr=Newstr+"W";  
  85.                     break;  
  86.                 case ']' :  
  87.                     Newstr=Newstr+"X";  
  88.                     break;  
  89.                 case ':':  
  90.                     Newstr=Newstr+"Y";  
  91.                     break;  
  92.                 case '\"' :  
  93.                     Newstr=Newstr+"Z";  
  94.                     break;       
  95.                 case '1':  
  96.                     Newstr=Newstr+"r";  
  97.                     break;  
  98.                 case '2':  
  99.                     Newstr=Newstr+"k";  
  100.                     break;  
  101.                 case '3':  
  102.                     Newstr=Newstr+"b";  
  103.                     break;  
  104.                 case '4':  
  105.                     Newstr = Newstr+"e";  
  106.                     break;  
  107.                 case '5':  
  108.                     Newstr = Newstr+"q";  
  109.                     break;  
  110.                 case '6':  
  111.                     Newstr = Newstr+"h";  
  112.                     break;  
  113.                 case '7':  
  114.                     Newstr = Newstr+"u";  
  115.                     break;  
  116.                 case '8' :  
  117.                     Newstr= Newstr+"y";  
  118.                     break;  
  119.                 case '9':  
  120.                     Newstr = Newstr+"w";  
  121.                     break;  
  122.                 case '0':  
  123.                     Newstr = Newstr+"z";  
  124.                     break;  
  125.                  default:  
  126.                     Newstr=Newstr+"0";  
  127.                     break;  
  128.             }  
  129.         }  
  130.         }  
  131.         catch(Exception ioe)  
  132.         {  
  133.             ioe.printStackTrace();  
  134.         }  
  135.         System.out.println("The decrypted string is: " +Newstr);  
  136.     }  
  137. }  
Output 1

Output 1

Output 2

Output 2

You can try your hand at the use of various symbols as the key for encryption or decryption. It all depends on the sender, how he/she encrypts the data.

Thank you, keep learning and sharing.