This program can be used to encrypt text messages with a 5-digit pass code and can also be used to decrypt the same message with that code. If you try to decrypt the message with any wrong code then the program will give gibberish result. You need the Eclipse IDE to follow this code.
- Create a new class named Code2Decode
- Delete all the text there and paste the following code:
- import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintStream;
import java.util.Scanner;
public class Code2Decode {
public static void main(String[] args) throws FileNotFoundException {
Scanner scan=new Scanner(System.in);
System.out.println("Welcome to Code2Decode.");
System.out.println("Please enter file name to send your coded or decoded message.There is no need of any extension.");
String abcd=scan.next();
System.out.println("Please note that the file would be saved with .txt extension");
System.out.println("Type c for coder,and d for decoder.");
String x=scan.next();
int z=0;
if (x.equals("c")){
z=-1;
}
if (x.equals("d")){
z=1;
}
System.out.println("enter 5-digit pass code.");
int cd=scan.nextInt();
int ef=(cd/10000);
cd=cd-ef*10000;
int gh=(cd/1000);
cd=cd-gh*1000;
int a=(cd/100);
int b=(cd/10)-(a*10);
int d=(cd)-(a*100+b*10);
int dre=(ef*gh)%10;
int n=a*b+d;
n=(n%13)+10;
String s="";
if (x.equals("c")){
System.out.println("Failure in remembering pass code might cause loss of data.");
System.out.println("It is our advise that you and the receiver remember it and never disclose it to others.");
System.out.println("Please use backslash (\\) instead of any spaces in your message");
System.out.println("\nPLAINTEXT?");
}
else{
System.out.println("\nCIPHERTEXT?");}
String abc=scan.next();
int xyz=abc.length();
int p=-1;
goop:{for(int j=1;j<=xyz;j++){
for(int i=dre;i<=n;i++){
p=p+1;
char c = 0;
if(p != abc.length()){
c=abc.charAt(p);
}
if (p>=xyz){
break goop;
}
int q = ((int) c)+z*i;
char e=(char)q;
s=s+e;
}
}
}
if (x.equals("c")){
System.out.println("CIPHERTEXT:");}
else{
System.out.println("PLAINTEXT:");}
System.out.println(s);
System.out.println("\nSent to file "+abcd+".txt");
PrintStream o=new PrintStream(new File(abcd+".txt"));
System.setOut(o);
if (x.equals("c")){
System.out.println("CIPHERTEXT:");}
else{
System.out.println("PLAINTEXT:");}
System.out.println(s);
}
} - Press run to start the program. All the information you need will be there.
No comments:
Post a Comment