1807036 15IT10 - CRYPTOGRAPHY LABORATORY K.
NAVEEN KUMAR
[Link] – 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 ( [Link] )
import [Link];
import [Link];
import [Link];
import [Link];
import [Link];
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 <
[Link]() ; i++)
{
int x = (cipher_text.charAt(i) - [Link](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 = [Link]();
// Receive from Client
Scanner ser = new Scanner([Link]());
message = [Link]();
key = [Link]();
[Link]("The message received is:");
[Link]("Encrypted message:"+message+"\nkey:"+key);
// Decrypt the message & send reply
decy = decrypt(message,key);
[Link]("Decrypted message:"+decy);
PrintStream p = new PrintStream([Link]());
[Link](decy);
}
}
3|Page
15IT10 - CRYPTOGRAPHY LABORATORY
Client class ( [Link] )
import [Link];
import [Link];
import [Link];
import [Link];
public class VignereClient {
/* KEY GENERATION
e.g
Text: NAVEEN
Key: AB
Generated key: ABABAB
*/
static String generateKey(String str, String key)
{
int x = [Link]();
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 ([Link]() == [Link]())
break;
key+=([Link](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 < [Link](); i++)
{
int x = ([Link](i) + [Link](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([Link]);
Socket cli = new Socket("[Link]",3456);
Scanner sc1 = new Scanner([Link]());
// Get input from user
[Link]("Enter the encrypted message to be
sent:");
String message;
message = [Link]();
[Link]("Enter the key:");
key = [Link]();
// Generate key, Encrypt text and send
String encrypted;
String generated_key = generateKey([Link](),
[Link]());
encrypted = encrypt([Link](),generated_key);
[Link]("message :"+ encrypted);
[Link]("key :"+ [Link]());
PrintStream p = new PrintStream([Link]());
[Link](encrypted);
[Link](generated_key);
messconf = [Link]();
[Link]("The received message is:");
[Link](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