123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143 |
- /*
- Name: Ajinkya Arun Gurav
- Div : H Roll No:21
- GR No : 121237
- Subject : Network Security Lab
- Lab 2: Implement encryption and decryption using Vegener Cipher
- */
- import java.io.BufferedReader;
- import java.io.IOException;
- import java.io.InputStreamReader;
- public class VegenerCipher {
-
- static String encrypt(String PlainText,int Key[])
- {
- String cipherText="";
- int index=0,offset=0,charAscii=0,CharVal=0,KeyIndex=0;
-
- System.out.println("PlainText Length: "+PlainText.length());
- for(index=0;index<PlainText.length();index++)
- {
- if(PlainText.charAt(index)==' '){
- cipherText+=' ';
- continue;
- }
- charAscii=PlainText.charAt(index)+Key[KeyIndex];
- CharVal=PlainText.charAt(index);
- /*if((charAscii-48)>=10 && CharVal>=48 && CharVal<=57)
- {
- offset=(charAscii-48)%10;
- cipherText+=(char)(offset+48);
- }*/
- if((charAscii-97) >= 26 && CharVal>=97 && CharVal<=122)
- {
- offset=(charAscii-97)%26;
- cipherText+=(char)(offset+97);
- }
- else if((charAscii-65) >= 26 && CharVal>=65 && CharVal<=90)
- {
- offset=(charAscii-65)%26;
- cipherText+=(char)(offset+65);
- }
- else
- {
- cipherText+=((char) (PlainText.charAt(index)+Key[KeyIndex]));
- }
- }
- System.out.println("CipherText is: "+cipherText);
- return cipherText;
- }
- static String decrypt(String cipherText,int Key[])
- {
- String PlainText="";
- int index=0,offset=0,charAscii=0,CharVal=0,KeyIndex=0;
- System.out.println("CipherText Length: "+cipherText.length());
- for(index=0;index<cipherText.length();index++)
- {
- if(cipherText.charAt(index)==' '){
- PlainText+=' ';
- continue;
- }
- charAscii=cipherText.charAt(index)-Key[KeyIndex];
- CharVal=cipherText.charAt(index);
- /*if((charAscii)<48 && CharVal>=48 && CharVal<=57)
- {
- offset=(charAscii-48)%10;
- if(offset<0)
- offset=-(offset);
- if(offset==0)
- PlainText+='0';
- else
- PlainText+=(char)(57-offset+1);
- }*/
- if(charAscii<65 && CharVal>=65 && CharVal<=90)
- {
- offset=(charAscii-65)%26;
- if(offset<0)
- offset=-(offset);
- if(offset==0)
- PlainText+='A';
- else
- PlainText+=(char)(90-offset+1);
- }
- else if((charAscii)<97 && CharVal>=97 && CharVal<=122)
- {
- offset=(charAscii-97)%26;
- if(offset<0)
- offset=-(offset);
- if(offset==0)
- PlainText+='a';
- else
- PlainText+=(char)(122-offset+1);
- }
-
- else
- {
- PlainText+=((char) (cipherText.charAt(index)-Key[KeyIndex]));
- }
- }
- System.out.println("PlainText is: "+PlainText);
- return cipherText;
- }
- public static void main(String args[])
- {
- String ptext=null,cipherText=null;
- int keyLength=0;
- int Keys[]=null;
- BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
-
- try {
- System.out.println("Enter the PlainText");
- ptext=br.readLine();
- System.out.println("Enter the Key length");
- keyLength=Integer.parseInt(br.readLine());
- Keys=new int[keyLength];
- for(int i=0;i<keyLength;i++)
- {
- Keys[i]=Integer.parseInt(br.readLine());
- }
- } catch (IOException e) {
- // TODO Auto-generated catch block
- System.out.println("Exception:"+e.getCause());
- }
- cipherText=encrypt(ptext,Keys);
- decrypt(cipherText,Keys);
- }
- }
- /*
- Output:
- Enter the PlainText
- Ajinkya Gurav
- Enter the Key length
- 3
- 10
- 15
- 13
- PlainText Length: 13
- CipherText is: Ktsxuik Qebkf
- CipherText Length: 13
- PlainText is: Ajinkya Gurav
- */
|