Wednesday, December 19, 2012

Mono_Cipher in java


import java.util.*;

public class Mono_Cipher{
    static String plain_txt,cipher_txt,crypt_txt;
public char[] key={'c','x','r','u','y','i','o','p','q','w','e','a','s','d','f','g','h','j','k','l','z','v','b','n','m','t'};

public void mono_encrypt(){
Scanner sc=new Scanner(System.in);
System.out.println("Enter Your Text");
plain_txt=sc.nextLine();

cipher_txt="";
int l=plain_txt.length();

for(int i=0;i<l;i++){
int tempAscii=(int)plain_txt.charAt(i);

if(tempAscii>96  && tempAscii<123){
tempAscii-=97;
}
cipher_txt+=key[tempAscii];

crypt_txt=cipher_txt;
}
System.out.println("Crypt text is"+"  "+crypt_txt);
}

public static void main(String args[]){
Mono_Cipher m=new Mono_Cipher();

m.mono_encrypt();
}
}

Saturday, December 8, 2012

Ceaser_cipher in java


import java.util.*;

public class Ceaser_Cipher{
   public static String plain_txt,cipher_txt,crypt_txt;
int key;

public void encrypt(){


Scanner sc=new Scanner(System.in);
   System.out.print("Enter Your Text=");
   plain_txt=sc.nextLine();
System.out.print("Enter the Key=");
key=sc.nextInt();
cipher_txt="";
int l=plain_txt.length();

for(int i=0;i<l;i++){
int tempAscii=(int)plain_txt.charAt(i)+key;

if(tempAscii>122){
tempAscii=tempAscii-26;
}
else{
tempAscii=tempAscii;
}

cipher_txt+=(char)tempAscii;
}
crypt_txt=cipher_txt;
System.out.println("encrypt text is"+"   "+crypt_txt);
}
public void decrypt(){
int le=crypt_txt.length();
cipher_txt="";

for(int j=0;j<le;j++){
int tempAscii=(int)crypt_txt.charAt(j)-key;

if(tempAscii<97){
tempAscii=tempAscii+26;
}
else{
tempAscii=tempAscii;
}

cipher_txt+=(char)tempAscii;
}
plain_txt=cipher_txt;
System.out.println("The original text is"+"  "+plain_txt);
}
public void brute_force(){
int len=crypt_txt.length();

for(int key=1;key<=26;key++){
String temp="";

for(int a=0;a<len;a++){
int tempAscii=(int)crypt_txt.charAt(a);
tempAscii-=key;

if(tempAscii<97){
tempAscii=tempAscii+26;
}
else{
tempAscii=tempAscii;
}
temp=temp+(char)tempAscii;
}
Scanner sc=new Scanner(System.in);
System.out.println("we got"+"    "+temp+"  "+"as a decrypt text.please say 1 if it is meaningful");
int res=sc.nextInt();
if(res==1){
    break;

}
}
}




public static void main(String args[]){
Ceaser_Cipher c=new Ceaser_Cipher();

c.encrypt();
c.decrypt();
c.brute_force();
}
}






Output is:

Enter Your Text=welcome
Enter the Key=4
encrypt text is   aipgsqi
The original text is  welcome
we got    zhofrph  as a decrypt text.please say 1 if it is meaningful
2
we got    ygneqog  as a decrypt text.please say 1 if it is meaningful
2
we got    xfmdpnf  as a decrypt text.please say 1 if it is meaningful
2
we got    welcome  as a decrypt text.please say 1 if it is meaningful