Last active
December 22, 2015 22:09
-
-
Save demlaip/6538422 to your computer and use it in GitHub Desktop.
Implementation of vernam cipher
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.*; | |
import java.math.*; | |
class vernam | |
{ | |
public static void main(String args[]) throws IOException | |
{ | |
BufferedReader br=new BufferedReader(new InputStreamReader(System.in)); | |
System.out.println("Enter the string"); | |
String st = br.readLine(); | |
System.out.println("enter the padding code"); | |
int pd[]=new int[7]; | |
int j=0,i=0; | |
for(i=0;i<7;i++) | |
pd[i]=Integer.parseInt(br.readLine()); | |
char ch[]=new char[st.length()]; | |
ch=st.toCharArray(); | |
int num; | |
int send[][]=new int[st.length()][7]; | |
int receive[][]=new int[st.length()][7]; | |
int con[]=new int[7]; | |
System.out.println("Send code:"); | |
for(i=0;i<st.length();i++) | |
{ | |
j=6; | |
while((int)(ch[i])!=0) | |
{ | |
con[j]=(int)(ch[i])%2; | |
ch[i]=(char)((int)(ch[i])/2); | |
send[i][j]=con[j]^pd[j]; | |
System.out.print(send[i][j]); | |
j--; | |
} | |
System.out.println(); | |
} | |
System.out.println("Received code:"); | |
for(i=0;i<st.length();i++) | |
{ | |
j=6; | |
while(j>-1) | |
{ | |
receive[i][j]=send[i][j]^pd[j]; | |
System.out.print(receive[i][j]); | |
j--; | |
} | |
System.out.println(); | |
} | |
int sum; | |
System.out.println("Original data:"); | |
for(i=0;i<st.length();i++) | |
{ | |
sum=0; | |
j=0; | |
for(int k=6;k>=0;k--) | |
{ | |
sum+=Math.pow(2,k)*receive[i][j]; | |
j++; | |
} | |
System.out.print((char)(sum)); | |
} | |
} | |
} | |
/*OUTPUT: | |
C:\jdk1.3\bin>javac vernam.java | |
C:\jdk1.3\bin>java vernam | |
Enter the string | |
sharvari | |
enter the padding code | |
1111111 | |
Send code: | |
0011000 | |
1110100 | |
0111100 | |
1011000 | |
1001000 | |
0111100 | |
1011000 | |
0110100 | |
Received code: | |
1100111 | |
0001011 | |
1000011 | |
0100111 | |
0110111 | |
1000011 | |
0100111 | |
1001011 | |
Original data: | |
sharvari*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment