0% found this document useful (1 vote)
3K views

Vernam Cipher Program in Java

This document contains the code for a Vernam cipher encryption program in Java. It takes in a plain text and key as user input, converts each character to its corresponding number based on its position in the alphabet, adds that number to the number for the corresponding character in the key, takes the result modulo 26 to wrap around the alphabet, and outputs the encrypted cipher text. It uses arrays to store the alphabet, plain text numbers, and cipher text letters.

Uploaded by

Harsh Ghuriani
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (1 vote)
3K views

Vernam Cipher Program in Java

This document contains the code for a Vernam cipher encryption program in Java. It takes in a plain text and key as user input, converts each character to its corresponding number based on its position in the alphabet, adds that number to the number for the corresponding character in the key, takes the result modulo 26 to wrap around the alphabet, and outputs the encrypted cipher text. It uses arrays to store the alphabet, plain text numbers, and cipher text letters.

Uploaded by

Harsh Ghuriani
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

import java.util.

*;
import java.lang.*;
public class VernamCypher
{
public static void main(String args[])
{
String plainText="",plainText1="";
String cipherText="",cipherText1="";
Scanner sc=new Scanner(System.in);
System.out.println("enter the plain text");
plainText=sc.next();
System.out.println("enter the ket text");
String key=sc.next();
int alpha[]=new int[26],k=97;
char alphabets[]=new char[26];
for(int i=0;i<26;i++)
{
alpha[i]=i;
alphabets[i]=(char)k++;
}

int number[]=new int[plainText.length()];

/*for(int i=0;i<26;i++)
{
System.out.print(" "+alphabets[i]);

}
System.out.println(plainText);
*/

char c,ch;
for(int i=0;i<plainText.length();i++)
{
c=plainText.charAt(i);

for(int j=0;j<26;j++)
{

//System.out.println(alpha[j]);
if(c==alphabets[j])
{
//System.out.println("c: "+c+" alpha: "+alpha[j]);
number[i]=alpha[j];
break;
}
}
for(int j=0;j<26;j++)
{
ch=key.charAt(i);
if(ch==alphabets[j])
{
number[i]+=alpha[j];

//System.out.println("ch: "+ch+" alpha: "+alpha[j]);


break;
}
}

for(int i=0;i<number.length;i++)
{
if(number[i]>25)
number[i]-=26;
//System.out.print(number[i]);
System.out.print(alphabets[number[i]]);
}
}
}

You might also like