Sunday, 18 October 2015

Practical No-2(b)


AIM- Write a program to Implement vernam cipher(one time pad) in Java

import java.lang.Math;
public class vernamcipher
 {
public static void main(String args[])
{
String text = new String("vernam");
char[] arText = text.toCharArray();
String cipher = new String("abcdef");
char[] arCipher = cipher.toCharArray();
char[] encoded = new char[5];
System.out.println("Encoded " + text + " to be… ");
for (int i = 0; i < arText.length; i++)
{
encoded[i] = (char) (arText[i] ^ arCipher[i]);
System.out.print(encoded[i]);
}
System.out.println("\nDecoded to be… ");
for (int i = 0; i < encoded.length; i++)
{
char temp = (char) (encoded[i] ^ arCipher[i]);
System.out.print(temp);
}
}

}


No comments:

Post a Comment