0% found this document useful (0 votes)
1K views6 pages

Practical 5 6 NIS PDF

The document describes the Vernam cipher encryption algorithm. It involves assigning a number to each character in the plaintext and key based on alphabetical order. The numbers are then added together, and if the sum exceeds 26, 26 is subtracted. This results in a ciphertext number for each character. The program implements the Vernam cipher by getting character values, adding the plaintext and key arrays, and getting the ciphertext character from the final array.

Uploaded by

Bhavesh Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1K views6 pages

Practical 5 6 NIS PDF

The document describes the Vernam cipher encryption algorithm. It involves assigning a number to each character in the plaintext and key based on alphabetical order. The numbers are then added together, and if the sum exceeds 26, 26 is subtracted. This results in a ciphertext number for each character. The program implements the Vernam cipher by getting character values, adding the plaintext and key arrays, and getting the ciphertext character from the final array.

Uploaded by

Bhavesh Patil
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 6

Practical 5

Aim :- Write a program to implement Caesar Cipher


 The Caesar Cipher technique is one of the earliest and simplest method of encryption
technique. It’s simply a type of substitution cipher, i.e., each letter of a given text is replaced
by a letter some fixed number of positions down the alphabet. For example with a shift of 1,
A would be replaced by B, B would become C, and so on. The method is apparently named
after Julius Caesar, who apparently used it to communicate with his officials.
 Thus to cipher a given text we need an integer value, known as shift which indicates the
number of position each letter of the text has been moved down.
 The encryption can be represented using modular arithmetic by first transforming the letters
into numbers, according to the scheme, A = 0, B = 1,…, Z = 25. Encryption of a letter by a
shift n can be described mathematically as.

(Encryption Phase with shift n)

(Decryption Phase with shift n)


Encryption Algorithm:
Input:
1. A String of lower case letters, called Text.
2. An Integer between 0-25 denoting the required shift.
Procedure:
 Traverse the given text one character at a time .
 For each character, transform the given character as per the rule, depending on whether we’re
encrypting or decrypting the text.
 Return the new string generated.
Program that receives a Text (string) and Shift value( integer) and returns the encrypted text.
Examples :
Text : ABCDEFGHIJKLMNOPQRSTUVWXYZ
Shift: 23
Cipher: XYZABCDEFGHIJKLMNOPQRSTUVW

Text : ATTACKATONCE
Shift: 4
Cipher: EXXEGOEXSRGI

PROGRAM :-
//A Java Program to illustrate Caesar Cipher Technique
import java.io.*;
import java.util.Scanner;
class CaesarCipher
{
// Encrypts text using a shift od s
public static StringBuffer encrypt(String text, int s)
{
StringBuffer result= new StringBuffer();

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


if (Character.isUpperCase(text.charAt(i))){
char ch = (char)(((int)text.charAt(i) + s - 65) % 26 + 65);
result.append(ch);
}
else{
char ch = (char)(((int)text.charAt(i) + s - 97) % 26 + 97);
result.append(ch);
}
}
return result;
}
// Driver code
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
System.out.println("Enter text which you want to cipher");
String text = sc.next();
System.out.println("Enter shift number");
int s = sc.nextInt();

System.out.println("Text : " + text);


System.out.println("Shift : " + s);
System.out.println("Cipher: " + encrypt(text, s));
}
}

Output :-
Practical No 6
Aim :- Write a program to implement Vernam Cipher
 Vernam Cipher is a method of encrypting alphabetic text. It is simply a type of substitution
cipher. In this mechanism we assign a number to each character of the Plain-Text, like (a = 0,
b = 1, c = 2, … z = 25).
 Method to take key:
In Vernam cipher algorithm, we take a key to encrypt the plain text which length should be
equal to the length of the plain text.

Encryption Algorithm:

1. Assign a number to each character of the plain-text and the key according to alphabetical order.
2. Add both the number (Corresponding plain-text character number and Key character number).
3. Subtract the number from 26 if the added number is greater than 26, if it isn’t then leave it.
Example:
Plain-Text: RAMSWARUPK
Key: RANCHOBABA
Now according to our encryption algorithm we assign a number to each character of our plain-text
and key.
PT: R A M S W A R U P K
NO: 17 0 12 18 22 0 17 20 15 10

KEY: R A N C H O B A B A
NO: 17 0 13 2 7 14 1 0 1 0

Now add the number of Plain-Text and Key and after doing the addition and subtraction operation (if
required), we will get the corresponding Cipher-Text character number.
CT-NO: 34 0 25 20 29 14 18 20 16 10
In this case, there are two numbers which are greater than the 26 so we have to subtract 26 from
them and after applying the subtraction operation the new Cipher text character numbers are as
follow:
CT-NO: 8 0 25 20 3 14 18 20 16 10
New Cipher-Text is after getting the corresponding character from the number.
CIPHER-TEXT: I A Z U D O S U Q K

Note :- For the Decryption apply the just reverse process of encryption.
PROGRAM :-
//VernamCipher.java - cipher based on random pad
import java.io.*;

class VernamCipher{
public static int getCharValue(char x){
int y=(int)'a';
return ((int)x-y);
}

public static char getNumberValue(int x){


int z=x+(int)'a';
return ((char)z);
}

public static void main(String args[])throws Exception{


BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter your plain text");
String accept=br.readLine();
System.out.println("\nEnter your one time pad text");
String pad=br.readLine();
int aval[]=new int[accept.length()];
int pval[]=new int[pad.length()];
int initval[]=new int[pad.length()];

if(pad.length()!=accept.length()){
System.out.println("Invalid one time pad. Application terminates.");
return;
}
for(int i=0;i<accept.length();i++){
int k=getCharValue(accept.charAt(i));
aval[i]=k;
}
for(int i=0;i<pad.length();i++){
int k=getCharValue(pad.charAt(i));
pval[i]=k;
}
for(int i=0;i<pad.length();i++){
initval[i]=aval[i]+pval[i];
if(initval[i]>25)
initval[i]-=26;
}
System.out.println("\nCipher text is : ");
String cipher="";
for(int i=0;i<pad.length();i++){
cipher+=getNumberValue(initval[i]);
}
System.out.print(cipher+"\n");
}
}
Output :-

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy