Search This Blog

Friday, May 28, 2021

Encrypt and Decrypt secret messages source code (java)

Encrypt and Decrypt



 import java.util.Locale;

import java.util.Scanner;

import java.util.concurrent.TimeUnit;



public class ED {

    static void sleep(int a) throws InterruptedException {

        TimeUnit.SECONDS.sleep(a);

    }

   static String encrypt(String a) throws InterruptedException {

       System.out.println("Encrypting....");

        sleep(3);


       String b=a.replace("a","05");

       String b1=b.replace("b","06");

       String b2=b1.replace("c","0a");

       String b3=b2.replace("d","0b");

       String b4=b3.replace("e","01");

       String b5=b4.replace("f","02");

       String b6=b5.replace("g","03");

       String b7=b6.replace("h","04");

       String b8=b7.replace("i","0c");

       String b9=b8.replace("j","0d");

       String b10=b9.replace("k","0e");

       String b11=b10.replace("l","0f");

       String b12=b11.replace("m","0g");

       String b13=b12.replace("n","0h");

       String b14=b13.replace("o","0i");

       String b15=b14.replace("p","0j");

       String b16=b15.replace("q","0k");

       String b17=b16.replace("r","0l");

       String b18=b17.replace("s","0m");

       String b19=b18.replace("t","0n");

       String b20=b19.replace("u","0o");

       String b21=b20.replace("v","0p");

       String b22=b21.replace("w","0q");

       String b23=b22.replace("x","0r");

       String b24=b23.replace("y","0s");

       String b25=b24.replace("z","0t");

       String b26=b25.replace(" ","#m");

       return b26;

    }

    static String decrypt(String a) throws InterruptedException {

        System.out.println("Decrypting....");

        sleep(3);

        String b=a.replace("05","a");

        String b1=b.replace("06","b");

        String b2=b1.replace("0a","c");

        String b3=b2.replace("0b","d");

        String b4=b3.replace("01","e");

        String b5=b4.replace("02","f");

        String b6=b5.replace("03","g");

        String b7=b6.replace("04","h");

        String b8=b7.replace("0c","i");

        String b9=b8.replace("0d","j");

        String b10=b9.replace("0e","k");

        String b11=b10.replace("0f","l");

        String b12=b11.replace("0g","m");

        String b13=b12.replace("0h","n");

        String b14=b13.replace("0i","o");

        String b15=b14.replace("0j","p");

        String b16=b15.replace("0k","q");

        String b17=b16.replace("0l","r");

        String b18=b17.replace("0m","s");

        String b19=b18.replace("0n","t");

        String b20=b19.replace("0o","u");

        String b21=b20.replace("0p","v");

        String b22=b21.replace("0q","w");

        String b23=b22.replace("0r","x");

        String b24=b23.replace("0s","y");

        String b25=b24.replace("0t","z");

        String b26=b25.replace("#m"," ");

        return b26;

    }

    public static void main(String[] args) throws InterruptedException {

        Scanner sc=new Scanner(System.in);

        String k=sc.nextLine();

        String j=sc.next();


        String i=k.toLowerCase(Locale.ROOT);

        if (j.equals("encrypt")){

            System.out.println(encrypt(i));

        }

        else if (j.equals("decrypt")){

            System.out.println(decrypt(i));

        }

        else {

            System.out.println("Operation not possible\nPlease enter a valid");

        }

    }

}


Trending Articles