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

Vigenere Cipher Using Java - Crypto Lab

This document describes implementing the Vigenere cipher encryption and decryption using a Java server/client program. It includes the encryption and decryption algorithms, an example, and the Java code for the server and client classes. The server accepts an encrypted message and key from the client, decrypts the message, and returns the plaintext. The client gets user input, encrypts the message, generates and sends the key, and receives the decrypted text from the server.

Uploaded by

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

Vigenere Cipher Using Java - Crypto Lab

This document describes implementing the Vigenere cipher encryption and decryption using a Java server/client program. It includes the encryption and decryption algorithms, an example, and the Java code for the server and client classes. The server accepts an encrypted message and key from the client, decrypts the message, and returns the plaintext. The client gets user input, encrypts the message, generates and sends the key, and receives the decrypted text from the server.

Uploaded by

Naveen Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

1807036 15IT10 - CRYPTOGRAPHY LABORATORY K.

NAVEEN KUMAR

EX.NO – 1b VIGNERE CIPHER ENCRYPTION AND DECRYPTION


USING
07/08/2021 SERVER/CLIENT PROGRAMMING

I Aim:
To implement the Vignere cipher encryption and decryption using server/client programming using
java.

II Vignere Cipher algorithm:


Encryption:
The encryption formula is given as follows:
C[i] = (P[i] + K[i]) mod 26;
Where C is the Cipher text
P is the Plain text
K is the Key
Decryption:
The decryption formula is given as follows:
P[i] = (C[i] - K[i] + 26) mod 26;
Where C is the Cipher text
P is the Plain text
K is the Key
III Example:

1. ENCRYPTION 2. DECRYPTION
Plain text : NAVEEN Cipher text : NBVFEO
Key : AB Key : AB
Thus, Thus,
NAVEEN NBVFEO
ABABAB + ( mod 26) ABABAB - ( mod 26)
_________ _________
NBVFEO NAVEEN
_________ _________
Therefore, Therefore,
Cipher Text : NBVFEO Plain Text : NAVEEN

1|Page
15IT10 - CRYPTOGRAPHY LABORATORY

USING ONLINE TOOL:

IV Java Code:
Server class ( MyServer.java )

import java.io.IOException;
import java.io.PrintStream;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Scanner;

public class VignereServer {

/* DECRYPTION
e.g

Text : NBVFEO
Generated key: ABABAB - (mod 26)
___________________
Cipher : NAVEEN

*/

2|Page
15IT10 - CRYPTOGRAPHY LABORATORY

public static String decrypt(String cipher_text, String key)


{
String orig_text="";

for (int i = 0 ; i < cipher_text.length() &&


i <
key.length() ; i++)
{
int x = (cipher_text.charAt(i) - key.charAt(i) + 26) %26;
x += 'A';
orig_text+=(char)(x);
}
return orig_text;
}

public static void main(String args[]) throws IOException


{
String message;
String decy;
String key;
ServerSocket s1 = new ServerSocket(3456);
Socket ss = s1.accept();

// Receive from Client


Scanner ser = new Scanner(ss.getInputStream());
message = ser.nextLine();
key = ser.nextLine();
System.out.println("The message received is:");
System.out.println("Encrypted message:"+message+"\nkey:"+key);

// Decrypt the message & send reply


decy = decrypt(message,key);
System.out.println("Decrypted message:"+decy);

PrintStream p = new PrintStream(ss.getOutputStream());


p.println(decy);
}
}

3|Page
15IT10 - CRYPTOGRAPHY LABORATORY

Client class ( MyClient.java )


import java.io.IOException;
import java.io.PrintStream;
import java.net.Socket;
import java.util.Scanner;

public class VignereClient {

/* KEY GENERATION
e.g
Text: NAVEEN
Key: AB
Generated key: ABABAB
*/
static String generateKey(String str, String key)
{
int x = str.length();
for (int i = 0; ; i++)
{
// For repeating key in cyclic
if (x == i)
i = 0;
// When key and plain text has same length - End
condition
if (key.length() == str.length())
break;

key+=(key.charAt(i));

return key;
}

/* ENCRYPTION
e.g

Text : NAVEEN
Generated key: ABABAB + (mod 26)
___________________
Cipher : NBVFEO

*/

public static String encrypt(String str, String key)


{
String cipher_text="";

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


{
int x = (str.charAt(i) + key.charAt(i)) %26;
x += 'A'; // convert to upper case
cipher_text+=(char)(x);
}

return cipher_text;
}

4|Page
15IT10 - CRYPTOGRAPHY LABORATORY

public static void main(String[] args)throws IOException


{

{
String messconf;
String key;
Scanner sc = new Scanner(System.in);
Socket cli = new Socket("127.0.0.1",3456);
Scanner sc1 = new Scanner(cli.getInputStream());

// Get input from user


System.out.println("Enter the encrypted message to be
sent:");
String message;
message = sc.nextLine();
System.out.println("Enter the key:");
key = sc.nextLine();

// Generate key, Encrypt text and send


String encrypted;
String generated_key = generateKey(message.toUpperCase(),
key.toUpperCase());
encrypted = encrypt(message.toUpperCase(),generated_key);
System.out.println("message :"+ encrypted);
System.out.println("key :"+ key.toUpperCase());

PrintStream p = new PrintStream(cli.getOutputStream());


p.println(encrypted);
p.println(generated_key);

messconf = sc1.nextLine();
System.out.println("The received message is:");
System.out.println(messconf);

}
}
}

5|Page
15IT10 - CRYPTOGRAPHY LABORATORY

V Output:

V Result:
Thus, the server / client program for Vignere cipher is successfully executed and the output is
verified.

6|Page

You might also like