0% found this document useful (0 votes)
87 views

VERNER Assignment

This document describes the implementation of the Vernam cipher encryption algorithm in Java. It begins by explaining the Vernam cipher technique which converts plaintext characters into ciphertext characters using a keyword. It then provides an example of encrypting the plaintext "HELLO" with the key "MONEY" to produce the ciphertext "TSYPM". The document proceeds to show the Java code for encrypting and decrypting text using the Vernam cipher method. It includes two classes - one with encryption and decryption methods and another class with a main method to test the encryption and decryption of sample plaintext and ciphertext.

Uploaded by

Faith
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
87 views

VERNER Assignment

This document describes the implementation of the Vernam cipher encryption algorithm in Java. It begins by explaining the Vernam cipher technique which converts plaintext characters into ciphertext characters using a keyword. It then provides an example of encrypting the plaintext "HELLO" with the key "MONEY" to produce the ciphertext "TSYPM". The document proceeds to show the Java code for encrypting and decrypting text using the Vernam cipher method. It includes two classes - one with encryption and decryption methods and another class with a main method to test the encryption and decryption of sample plaintext and ciphertext.

Uploaded by

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

IMPLEMENTATION OF VERNAM CIPHER

USING JAVA

BY

NDENU FAITH IFUNANYA


EU170306-1292

DEPARTMENT OF INFORMATION AND


COMMUNICATION TECHNOLOGY

2ND FEBRUARY, 2022


 Vernam Cipher is a method of encrypting alphabetic plain text. It is one of the Substitution
techniques which converts plain text into ciphertext. In this mechanism, it assigns a number
to each character of the Plain-Text.

The assignment is as follows:

A B C D E F G H I J K L M

0 1 2 3 4 5 6 7 8 9 10 11 12

N O P Q R S T U V W X Y Z

13 14 15 16 17 18 19 20 21 22 23 24 25

The relation between the key and plain text

In this algorithm, the length of the key should be equal to that of plain text.

Example

INPUT

Plain Text = HELLO

Key = MONEY

OUTPUT

Cipher Text = TSYPM

Converting the plain text to cipher text:-

Plain text — H E L L O → 7 4 11 11 14

Key — M O N E Y → 12 14 13 4 24

Plain text + key = 19 18 24 15 38

→ 19 18 24 15 12 (=38 – 26)

Cipher Text → T S Y P M
 Converting the Cipher text to Message:-

Cipher Text — T S Y P M → 19 18 24 15 12

Key — M O N E Y→ 12 14 13 4 24

Cipher text - key → 7 4 11 11 -12 (-12 + 26)

→ 7 4 11 11 14

Message → H E L L O

Implementing the Vernam cipher encryption algorithm on java using the example
above
package com.fandy;
// Java program Implementing One Time Pad Algorithm

// Importing required classes

// Main class
public class GFG {

// Method 1
// Returning encrypted text
public static String stringEncryption(String text,
String key) {

// Initializing cipherText
String cipherText = "";

// Initialize cipher array of key length


// which stores the sum of corresponding no.'s
// of plainText and key.
int cipher[] = new int[key.length()];

for (int i = 0; i < key.length(); i++) {


cipher[i] = text.charAt(i) - 'A' + key.charAt(i)
- 'A';
}

// If the sum is greater than 25 subtract 26 from it


// and store that resulting value
for (int i = 0; i < key.length(); i++) {
if (cipher[i] > 25) {
cipher[i] = cipher[i] - 26;
}
}

// Converting the no.'s into integers

// Convert these integers to corresponding


// characters and add them up to cipherText
for (int i = 0; i < key.length(); i++) {
int x = cipher[i] + 'A';
cipherText += (char) x;
}

// Returning the cipherText


return cipherText;
}

// Method 2
// Returning plain text
public static String stringDecryption(String s,
String key) {
// Initializing plain text
String plainText = "";

// Initializing integer array of key length


// which stores difference of corresponding no.'s of
// each character of cipherText and key
int plain[] = new int[key.length()];

// Running for loop for each character


// subtracting and storing in the array
for (int i = 0; i < key.length(); i++) {
plain[i]
= s.charAt(i) - 'A' - (key.charAt(i) - 'A');
}

// If the difference is less than 0


// add 26 and store it in the array.
for (int i = 0; i < key.length(); i++) {
if (plain[i] < 0) {
plain[i] = plain[i] + 26;
}
}

// Converting int to corresponding char


// add them up to plainText
for (int i = 0; i < key.length(); i++) {
int x = plain[i] + 'A';
plainText += (char) x;
}

// Returning plainText
return plainText;
}
}
Creating another class Hello and then import the GFG.stringDecryption classes

package com.fandy;

import static com.fandy.GFG.stringDecryption;


import static com.fandy.GFG.stringEncryption;

public class HELLO {

public static void main(String[] args) {


String plainText = "faith";
String key = "doors";
String encryptedText = stringEncryption(
plainText.toUpperCase(), key.toUpperCase());
System.out.println("Cipher Text - "
+ encryptedText);
System.out.println(
"Message - "
+ stringDecryption(encryptedText,
key.toUpperCase()));
}
}

OUTPUT RESULT

"C:\Program Files\Java\jdk-16.0.1\bin\java.exe" "-javaagent:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition


2021.1.1\lib\idea_rt.jar=54215:C:\Program Files\JetBrains\IntelliJ IDEA Community Edition 2021.1.1\bin" -
Dfile.encoding=UTF-8 -classpath C:\Users\FANDY\IdeaProjects\javatutorial\out\production\javatutorial
com.fandy.HELLO

Cipher Text - IOWKZ

Message - FAITH

Process finished with exit code 0


package com.fandy;

public class VEP {

public static String stringEncryption(String text, String key) {

String cipherText = "";

int cipher[] = new int[key.length()];

for (int i = 0; i < key.length(); i++) {

cipher[i] = text.charAt(i) - 'A' + key.charAt(i)

- 'A';

for (int i = 0; i < key.length(); i++) {

if (cipher[i] > 25) {

cipher[i] = cipher[i] - 26;

for (int i = 0; i < key.length(); i++) {

int x = cipher[i] + 'A';

cipherText += (char) x;

return cipherText;

}
public static String stringDecryption(String s, String key)

String plainText = "";

int plain[] = new int[key.length()];

for (int i = 0; i < key.length(); i++) {

plain[i]

= s.charAt(i) - 'A' - (key.charAt(i) - 'A');

for (int i = 0; i < key.length(); i++) {

if (plain[i] < 0) {

plain[i] = plain[i] + 26;

for (int i = 0; i < key.length(); i++) {

int x = plain[i] + 'A';

plainText += (char) x;

return plainText;

public static void main(String[] args) {

String plainText = "love";

String key = "door";

String encryptedText = stringEncryption(


plainText.toUpperCase(), key.toUpperCase());

System.out.println("Cipher Text - "

+ encryptedText);

System.out.println(

"Message - "

+ stringDecryption(encryptedText,

key.toUpperCase()));

You might also like