Mca Cns Record
Mca Cns Record
Mca Cns Record
MCA V SEMESTER
CRYPTOGRAPHY AND NETWORK
SECURITY LAB MANUAL
PREPARED BY
Mr W.ANIL
B.Tech, M.Tech(PhD)
Assistant Professor in IT
2
System Spefications
MCA V Semester
Cryptography and Network Security Lab
(Lab Manual)
R19
Course Objectives:
➢ To learn basic understanding of cryptography, how it has evolved, and some key encryption
➢ To understand and implement encryption and decryption using Ceaser Cipher, Substitution
➢ Demonstrate the different algorithms like DES, BlowFish, and Rijndael, encrypt the text
➢ Analyze and implement public key algorithms like RSA, Diffie-Hellman Key Exchange
INDEX
WEEK-1
AIM: Write a C program that contains a string (char pointer) with a value \Hello
World’. The program should XOR each character in this string with 0 and display the
result.
Description: Here, we have two variables, str is a string variable and ptr is a character
pointer, that will point to the string variable str. First of all, we are reading string in
str and then assigning the base address of str to the character pointer ptr by using
ptr=str or it can also be done by using ptr=&str[0].And, finally we are printing the
string character by character until NULL not found. Characters are printing by the
pointer *ptr.
PROGRAM:
#include<stdlib.h>
main() { char
str[]="Hello World";
len=strlen(str);
for(i=0;i<len;i++)
str1[i]=str[i]^0;
printf("%c",str1[i]);
printf("\n");
}
2
Output:
3
AIM: Write a C program that contains a string (char pointer) with a value \Hello
World’. The program should AND or and XOR each character in this string with 127
and display the result.
Description: Here, we have two variables, str is a string variable and ptr is a character
pointer, that will point to the string variable str. First of all, we are reading string in
str and then assigning the base address of str to the character pointer ptr
ptr = &str[0]
by using ptr=str or it can also be done by using.And, finally we are
printing the string character by character until NULL not found. Characters are
printing by the pointer *ptr.
PROGRAM
#include <stdio.h>
#include<stdlib.h>
str[]="Hello World";
str2[11]=str[]; int
for(i=0;i<len;i++)
str1[i]=str[i]&127;
printf("%c",str1[i]); }
4
printf("\n");
for(i=0;i<len;i++
){
str3[i]=str2[i]^127;
printf("%c",str3[i]);
printf("\n");
Output:
5
WEEK-2:
Aim: Encrypt the message “PAY” using hillcipher with the following keymatrix and
show
17 17 5
the decryption to formulate original
plaintext K=21 18 21
2 2 19
Description:
Hill cipher is a polygraphic substitution cipher based on linear algebra. Each letter is
represented by a number modulo 26. Often the simple scheme A = 0, B = 1, …, Z =
25 is used, but this is not an essential feature of the cipher. To encrypt a message,
each block of n letters (considered as an n-component vector) is multiplied by an
invertible n × n matrix, against modulus 26. To decrypt the message, each block is
multiplied by the inverse of the matrix used for encryption.
The matrix used for encryption is the cipher key, and it should be chosen randomly
from the set of invertible n × n matrices (modulo
1 11 12 13 1 26).
2 = 21 22 23 2
mod26
3 31 32 33 3
17 17 5
Given, K =21 18 21
2 2 19
Encryption:
The first three letters “PAY” of the plaintext are
17 17 5 21 15 375 represented by the vector
18 21 0 = 819
mod26
2 2 19 24 486
11
= 13 = LNS
18
Decryption:
P = K⁻ⁱC mod26
4 9 15
K⁻ⁱ = 15 17 6
24 0 17
6
11
C = 13
18
4 9 15 11
P = 15 17 6 13 mod26
24 0 17 18
431 15
= 494 mod26 = 0
570 24
WEEK-3
Aim: Write a C/JAVA program to perform encryption an decryption using
Description:
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.
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.
text.length(); i++) { if
(Character.isUpperCase(text.charAt(i))) {
result.append(ch);
cipher.length(); i++) { if
(Character.isUpperCase(cipher.charAt(i))) { char ch
65);
- 97) % 26 + 97);
result.append(ch);
}
8
main(String[] args) {
String originalText =
System.out.println("Encryption");
System.out.println("Decryption");
}}
Output:
Substitution Cipher
9
Description:
In a Substitution cipher, any character of plain text from the given fixed set of
characters is substituted by some other character from the same set depending on a
key. For example with a shift of 1, A would be replaced by B, B would become C,
and so on.
Special case of Substitution cipher is known as Caesar cipher where the key is taken
as 3.
TEST CASE:
Test Case Functionality Test Data Expected Observed Positive/Negative
TC1 Encryption Plaintext – Ciphertext- Ciphertext- Positive
And WORLD ASVPH ASVPH
Decryption Key – 4 Plaintext- Plaintext-
WORLD WORLD
Program:
import java.util.*;
class Sub{
(String[]args) {
Scanner s=new
char arr[],arr_2[];
i,j=0,counter=0; arr=s1.toCharArray();
arr_2=new char[arr.length];
for(i=0;i<arr.length;i++)
10
{
int value=(int)arr[i]; if(value<65 ||
value=value+26; if(value<65)
value=value+26;
arr_2[j++]=(char)val
ue; counter=1; } }
if(counter!=1){
System.exit(0);
System.out.println("Decrypted text:");
for(i=0;i<arr_2.length;i++){ System.out.print(arr_2[i]);
}
11
Output:
Hill Cipher
Description:
TEST CASE:
Test Functionalit Test Data Expected Observed Positive/Ne
Case y gative
java.io.BufferedReader; import
12
java.io.IOException; import
java.io.InputStreamReader;
static int
invK;
{ while (temp.length()
> s)
temp = temp.substring(s,
temp.length()); calLineMatrix(line);
if(choice ==1){
multiplyLineByKey(line.length());
}else{
multiplyLineByInvKey(line.length());
13
showResult(line.length(
)); } if (temp.length()
== s){
if(choice ==1){
calLineMatrix(temp);
multiplyLineByKey(temp.length(
)); showResult(temp.length());
else{
calLi
neM
atrix(
temp
);
this.
multi
plyLi
neBy
InvK
ey(te
14
mp.le
ngth(
));
show
Resu
lt(te
mp.le
ngth(
));
calLineMatrix(temp);
multiplyLineByKey(temp.length())
; showResult(temp.length()); }
else{ calLineMatrix(temp);
multiplyLineByInvKey(temp.lengt
h()); showResult(temp.length());
int len)
15
{ km = new
int[len][len]; int k =
km[i][j] = ((int)
key.charAt(k)) - 97;
k++; }
{ lm = new int[line.length()];
i++)
- 97;
} } public void
multiplyLineByKey(int len)
{ rm = new int[len];
i++)
16
{
for (int j = 0; j < len; j++) { rm[i] +=
len)
len; i++)
97); }
j1 = 0; j1 < N; j1++)
N; j++)
18
A[0][j1]
* calDeterminant(m, N - 1);
} break; }
return
resultOfDet; }
p, q, m, n, i, j; for
(q = 0; q < f; q++)
{ for (p 0; p < f;
p++)
{
m = 0; n = 0; for
(i = 0; i < f; i++) {
for (j = 0; j < f;
j++) { b[i][j] = 0;
19
if (i != q && j !=
p) { b[m][n] =
num[i][j]; if (n <
(f - 2)) n++;
else
{n=
0;
m++
;}
calDeterminant(b, f - 1);
trans(fac, f);
}
r) {
int[r][r]; int d =
mi = mi(d % 26); mi %=
= 0; j < r; j++) {
b[i][j] = fac[j][i];
} } for (i = 0; i <
r; i++) { for (j 0; j
< r; j++) {
inv[i][j] = b[i][j] %
inv[i][j] += 26;
inv[i][j] *= mi;
inv[i][j] %= 26; } }
invK = inv;
26; r2 = d; t1 = 0; t2 =
!= 0) { q = r1 / r2; r = r1
21
= r2;
r2 = r; t1 = t2;
t2 = t; }
return (t1 +
t2); }
System.out.print(invkey); } public
d calDeterminant(km,
len);
d = d % 26;
if (d == 0)
{
22
return false;
} else if (d % 2 == 0 || d % 13
== 0)
System.out.println("Key is not
else {
return
true;
InputStreamReader(System.in)); System.out.println("Menu:\n1:
System.out.println("Result:"); obj.cofact(obj.km,
} }}}
Output:
Description:
Regulation:R17
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.
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.
Cas
e
result.append(ch);
Regulation:R17
result.append(ch);
return result;
}
public static void main(String[] args) {
shiftCount = 1;
System.out.println("Encryption");
System.out.println("Decryption");
}}
Output:
Week-3 iii) Aim: Write a C/JAVA program for AFFINE CIPHER with equation c =
3x + 12
Description:
The Affine cipher is a type of monoalphabetic substitution cipher, wherein each letter in an
alphabet is mapped to its numeric equivalent, encrypted using a simple mathematical function,
and converted back to a letter. The formula used means that each letter encrypts to one other
letter, and back again, meaning the cipher is essentially a standard substitution cipher with a rule
governing which letter goes to which.
Regulation:R17
The whole process relies on working modulo m (the length of the alphabet used). In the affine
cipher, the letters of an alphabet of size m are first mapped to the integers in the range 0 … m-
1.
The ‘key’ for the Affine cipher consists of 2 numbers, we’ll call them a and b. The following
discussion assumes the use of a 26 character alphabet (m = 26). a should be chosen to be
relatively prime to m (i.e. a should have no factors in common with m).
TEST CASE:
Test Case Functionalit Test Data Expected Observed Positive/Nega
y tive
{
String cipher = ""; for (int i =
{ cipher = cipher
decryptCipher(String cipher)
{ flag = (a * i) % 26; if
(flag == 1)
{
a_inv = i;
main(String[] args)
}}
Output
Regulation:R17
Week-3:
iv) Aim: Write a C/JAVA program to implement Playfair cipher with key ldrp
Description:
The Playfair Cipher is a manual symmetric encryption cipher invented in 1854 by Charles
Wheatstone, however it’s name and popularity came from the endorsement of Lord Playfair.
The Playfair cipher encrypts pairs of letters (digraphs), instead of single letters as is the case
with simpler substitution ciphers such as the Caesar Cipher. Frequency analysis is still possible
on the Playfair cipher, however it would be against 600 possible pairs of letters instead of 26
different possible letters. For this reason the Playfair cipher is much more secure than older
substitution ciphers, and it’s use continued up until WWII.
The playfair cipher starts with creating a key table. The key table is a 5×5 grid of letters that
will act as the key for encrypting your plaintext. Each of the 25 letters must be unique and one
letter of the alphabet (usually Q) is omitted from the table (as there are 25 spots and 26 letters in
the alphabet).
TEST CASE:
Test Functionality Test Data Expected Observed Positive/Negati
Case ve
package com.sanfoundry.setandstring;
PlayfairCipherEncryption
setKey(String k) {
i++)
{ if (k.charAt(i) == K_adjust.charAt(j))
false; }
KeyWord = K_adjust; }
current;
KeyWord.length(); j++)
{ if (current == KeyWord.charAt(j))
Regulation:R17
if (flag)
true; }
System.out.println(Key);
{ int counter = 0;
{ matrix_arr[i][j] = Key.charAt(counter);
counter++; }
System.out.println();
{ if (old_text.charAt(tmp) == 'j')
else
text = text + old_text.charAt(tmp);
len; i = i + 2)
{ if (text.charAt(i + 1) == text.charAt(i))
} } return text; }
size++;
counter = 0;
GetDiminsions(char letter)
Regulation:R17
== 'j') letter =
{ if (matrix_arr[i][j] == letter)
{ key[0] = i; key[1]
= j; break; }
}
return key; } public String encryptMessage(String
Source)
src_arr[i].charAt(1); part1 =
GetDiminsions(one); part2 =
GetDiminsions(two); if (part1[0] ==
== part2[1])
{ if (part1[0] < 4)
0; if (part2[0] < 4)
0; }
= temp;
+ matrix_arr[part2[0]][part2[1]];
} return Code; }
main(String[] args)
keyword = sc.next();
x.setKey(keyword);
x.KeyGen();
even)");
(key_input.length() % 2 == 0)
else
sc.close();
}}
Output:
Regulation:R17
Regulation:R17
Description:
The table consists of the alphabets written out 26 times in different rows, each alphabet shifted
cyclically to the left compared to the previous alphabet, corresponding to the 26 possible Caesar
Ciphers.
At different points in the encryption process, the cipher uses a different alphabet from one of the
rows.
TEST CASE:
Test Functionalit Test Data Expected Observed Positive/Nega
Case y tive
(int i = 0; ; i++)
{
if (x == i) i = 0;
Regulation:R17
key+=(key.charAt(i));
String key)
str.length(); i++)
+= 'A'; cipher_text+=(char)(x);
key.length(); i++)
{
int x = (cipher_text.charAt(i) - key.charAt(i) + 26) %26; x +=
'A';
System.out.println("Ciphertext : "
+ cipher_text + "\n");
+ originalText(cipher_text, key));
}}
Output:
TEST CASE:
Test Functionality Test Data Expected Observed Positive/Negative
Case
TC1 Encryption And Plaintext - Cipher text Cipher text Positive
Decryption HELLO – ULPWZ – ULPWZ
Plaintext - Plaintext -
HELLO HELLO
Program:
import java.lang.*; import java.util.*; public class AutoKey { private static final
String alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"; public static void
main(String[] args)
{
String msg = "HELLO"; String key =
"N";
if (key.matches("[-+]?\\d*\\.?\\d+")) key = "" +
alphabet.charAt(Integer.parseInt(key));
String enc = autoEncryption(msg, key);
System.out.println("Plaintext : " + msg);
System.out.println("Encrypted : " + enc);
System.out.println("Decrypted : " + autoDecryption(enc, key));
} public static String autoEncryption(String msg, String key)
{ int len = msg.length(); String newKey =
key.concat(msg);
Regulation:R17
Description: Hill cipher is a polygraphic substitution cipher based on linear algebra. Each letter
is represented by a number modulo 26. Often the simple scheme A = 0, B = 1, …, Z = 25 is
used, but this is not an essential feature of the cipher. To encrypt a message, each block of n
letters (considered as an n-component vector) is multiplied by an invertible n × n matrix, against
modulus 26. To decrypt the message, each block is multiplied by the inverse of the matrix used
for encryption.
The matrix used for encryption is the cipher key, and it should be chosen randomly from the set
of invertible n × n matrices.
TEST CASE:
Test Functionality Test Data Expected Observed Positive/Negative
Case
TC1 Encryption Plaintext – Ciphertext- Ciphertext Positive
And ACT POH – POH
Decryption Key - Plaintext - Plaintext -
GYBNQKURP ACT ACT
Program: class GFG
i++)
{
for (int j = 0; j < 3; j++) { keyMatrix[i][j] =
0; j < 1; j++) {
cipherMatrix[i][j] = 0;
messageVector[x][j];
}}
getKeyMatrix(key, keyMatrix);
HillCipher(message, key);
}
}
Output:
Encryption
Input : "defend the east wall"
Key = 3
Output : dnhaweedtees alf tl
Decryption
Input : dnhaweedtees alf tl
Key = 3
Output : defend the east wall
TEST CASE:
Test Functionalit Test Data Expected Observed Positive/Negative
Case y
System.out.println("Ciphertext : "+ciphertxt);
plaintxt=""; if(ciphertxt.length()%2==0)
j=ciphertxt.length()/2; else
j=ciphertxt.length()/2+1; k=j; for(i=0;i<k;i++,j++)
{
plaintxt+=ciphertxt.charAt(i); plaintxt+=ciphertxt.charAt(j);
}
System.out.println("Plaintext: "+plaintxt);
}}
Output:
Regulation:R17
Encryption
Input : Geeks on work
Key = HACK
Output : e w_eoo_Gs kknr_
Decryption
Input : e w_eoo_Gs kknr_
Key = HACK
Output : Geeks on work TEST
CASE:
Test Functionalit Test Data Expected Observed Positive/Nega
Case y tive
/*plainText and cipherText would be array containing ASCII values for respective
alphabets */ int plainText[][] = new int[rowCount][columnCount]; int cipherText[][] =
new int[rowCount][columnCount];
/*Encryption Process*/
System.out.print("\n-----Encryption-----\n"); cipherText = encrypt(plainText,
cipherText, message, rowCount, columnCount, key);
/*Decryption Process*/
System.out.print("\n\n\n-----Decryption-----\n");
System.out.println();
}
static int[][] encrypt(int plainText[][], int cipherText[][], String message, int rowCount,
int columnCount, String key){
int i,j;
int k=0;
{ break;
}
}
}
static int[][] decrypt(int plainText[][], int cipherText[][], String message, int rowCount,
int columnCount, String key){
int i,j;
int k=0;
return plainText;
}}
Output:
WEEK-4
The Data Encryption Standard (DES) is a symmetric-key block cipher published by the
National Institute of Standards and Technology (NIST).
Regulation:R17
DES is an implementation of a Feistel Cipher. It uses 16 round Feistel structure. The block size
is 64-bit. Though, key length is 64-bit, DES has an effective key length of 56 bits, since 8 of the
64 bits of the key are not used by the encryption algorithm
Since DES is based on the Feistel Cipher, all that is required to specify DES is −
• Round function
• Key schedule
DES ANALYSIS:
The DES satisfies both the desired properties of block cipher. These two properties make cipher
very strong.
• Avalanche effect − A small change in plaintext results in the very great change in the
ciphertext.
During the last few years, cryptanalysis have found some weaknesses in DES when key selected
are weak keys. These keys shall be avoided.
DES has proved to be a very well designed block cipher. There have been no significant
cryptanalytic attacks on DES other than exhaustive key search.
TEST CASE:
Te Functi Test data Expected Observed Positive/N
st nality egative
C
as
e
Regulation:R17
PROGRAM:
import javax.swing.*; import
java.security.SecureRandom; import
javax.crypto.Cipher; import
javax.crypto.KeyGenerator; import
javax.crypto.SecretKey; import
javax.crypto.spec.SecretKeySpec; import
java.util.Random ; public class DES { byte[]
skey = new byte[1000]; String skeyString;
static byte[] raw;
String inputMessage,encryptedData,decryptedMessage; public
DES()
{
try
{ generateSymmetricKey();
inputMessage=JOptionPane.showInputDialog(null,"Enter message to encrypt");
byte[] ibyte = inputMessage.getBytes(); byte[] ebyte=encrypt(raw, ibyte); String
encryptedData = new String(ebyte);
System.out.println("Encrypted message "+encryptedData);
JOptionPane.showMessageDialog(null,"Encrypted Data "+"\n"+encryptedData);
byte[] dbyte= decrypt(raw,ebyte); String decryptedMessage = new String(dbyte);
System.out.println("Decrypted message "+decryptedMessage);
System.out.println(e);
} } void generateSymmetricKey() { try
{
Random r = new Random(); int num =
r.nextInt(10000); String knum =
String.valueOf(num); byte[] knumb =
knum.getBytes();
skey=getRawKey(knumb); skeyString =
new String(skey);
System.out.println("DES Symmetric key = "+skeyString);
} catch(Exception e)
{
System.out.println(e);
} } private static byte[] getRawKey(byte[] seed) throws Exception
{
KeyGenerator kgen = KeyGenerator.getInstance("DES");
SecureRandom sr = SecureRandom.getInstance("SHA1PRNG");
sr.setSeed(seed); kgen.init(56, sr);
SecretKey skey = kgen.generateKey(); raw =
skey.getEncoded();
return raw;
}
private static byte[] encrypt(byte[] raw, byte[] clear) throws
Exception {
SecretKeySpec skeySpec = new SecretKeySpec(raw, "DES");
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec); byte[]
encrypted = cipher.doFinal(clear); return encrypted; } private
static byte[] decrypt(byte[] raw, byte[] encrypted) throws
Exception
{
SecretKeySpec skeySpec = new SecretKeySpec(raw,
Regulation:R17
"DES");
Cipher cipher = Cipher.getInstance("DES");
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
byte[] decrypted = cipher.doFinal(encrypted); return
decrypted; }
public static void main(String args[]) {
}}
OUTPUT:
AIM:
write a c/java program to implement the blowfish algorithm logic.Generate sub key and s-box
from the given 32 –bit key by blowfish DESCRIPTION:
Blowfish is an encryption technique designed by Bruce Schneier in 1993 as an alternative to
DES Encryption Technique. It is significantly faster than DES and provides a good encryption
rate with no effective cryptanalysis technique found to date. It is one of the first, secure block
cyphers not subject to any patents and hence freely available for anyone to use.
1. blockSize: 64-bits
2. keySize: 32-bits to 448-bits variable size
3. number of subkeys: 18 [P-array]
4. number of rounds: 16
5. number of subsitution boxes: 4 [each having 512 entries of 32-bits each]
Regulation:R17
TEST CASE:
Test Functionality Test Expected Observed Positive/N
Case Data egative
} catch(Exception e) {
System.out.println(e); } }
private static byte[] getRawKey(byte[] seed) throws Exception { KeyGenerator
kgen = KeyGenerator.getInstance("Blowfish"); SecureRandom sr =
SecureRandom.getInstance("SHA1PRNG"); sr.setSeed(seed);
kgen.init(128, sr); // 128, 256 and 448 bits may not be available
SecretKey skey = kgen.generateKey(); raw = skey.getEncoded();
return raw; }
private static byte[] encrypt(byte[] raw, byte[] clear) throws Exception {
SecretKeySpec skeySpec = new SecretKeySpec(raw, "Blowfish");
Cipher cipher = Cipher.getInstance("Blowfish");
cipher.init(Cipher.ENCRYPT_MODE, skeySpec); byte[] encrypted =
cipher.doFinal(clear); return encrypted; }
WEEK-5
DESCRIPTION:
Rijndael (pronounced rain-dahl) is the algorithm that has been selected by the U.S. National
Institute of Standards and Technology (NIST) as the candidate for the Advanced Encryption
Standard (AES). It was selected from a list of five finalists, that were themselves selected from
Regulation:R17
an original list of more than 15 submissions. Rijndael will begin to supplant the Data
Encryption Standard (DES) - and later Triple DES - over the next few years in many
cryptography applications. The algorithm was designed by two Belgian cryptologists, Vincent
Rijmen and Joan Daemen, whose surnames are reflected in the cipher's name.
Rijndael has its origins in Square, an earlier collaboration between the two cryptologists.
The Rijndael algorithm is a new generation symmetric block cipher that supports key sizes of
128, 192 and 256 bits, with data handled in 128-bit blocks - however, in excess of AES design
criteria, the block sizes can mirror those of the keys. Rijndael uses a variable number of rounds,
depending on key/block sizes, as follows:
9 rounds if the key/block size is 128 bits
11 rounds if the key/block size is 192 bits
13 rounds if the key/block size is 256 bits
T Func Inp Expected Output Observed Output Positiv
e tiona ut e/Nega
lity
st tive
C
a
s
e
javax.crypto.sec.*; import
java.io.*; public classAES {
public static String asHex (byte buf[]) {
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
cipher.init(Cipher.DECRYPT_MODE, skeySpec);
2 Write the RC4 logic in java using java cryptography; encrypt the text “Hello World” using
Blowfish. Create your own key using java key tool.
DESCRIPTION:
RC4– this algorithm is used to create pseudo-random stream of bits (a key-stream). As with any
stream cipher, keystreams can be used for encryption.
A stream cipher is a symmetric key cipher where plaintext digits one at a time are XORed with
corresponding digits of pseudorandom stream of bits (keystream) to create encrypted message
i.e. ciphertext.
RC4 is mostly used in protocols such as :
1) Secure Socket Layer (SSL) to establish an encrypted link between a web server and a
browser to ensure all data transmitted remain private and generally used by many websites to
protect their online transaction with their customers.
2) Wired Equivalent Privacy (WEP) security protocol to provide security and privacy to
wireless networks(e.g. Wi-Fi) comparable to as in Wired Network (LAN).
Blowfish is also an encryption technique which is replacement to DES algorithm and that is
very powerful ammunition against hackers and cyber-criminals. It is utilized in a wide array of
products like in performance-constrained environments such as embedded systems, secure E-
mail encryption tools, backup software, password management tools.
Test Functionality Input Expected Output Observed Output Positive/Negative
Case
PROGRAM:
throws Exception{
SecretKey secretkey=keygenerator.generateKey();
//create a cipher based upon
encrypted=cipher.doFinal(inputText.getBytes()); //re-
cipher.init(Cipher.DECRYPT_MODE,secretkey);
decrypted=cipher.doFinal(encrypted);
}}
OUTPUT:
WEEK-6
a = 35; b = 10; g =
gcd(a, b);
System.out.println("GCD(" + a + ", " + b + ") = " + g);
a = 31; b = 2; g =
gcd(a, b);
System.out.println("GCD(" + a + ", " + b + ") = " + g);
}
}
OUTPUT:
Regulation:R17
return gcd; }
}
}
OUTPUT:
RSA algorithm is asymmetric cryptography algorithm. Asymmetric actually means that it works
on two different keys i.e. Public Key and Private Key. As the name describes that the Public
Key is given to everyone and Private key is kept private.
Mechanism:
Select two prime no's. Suppose P = 53 and Q = 59. Now
First part of the Public key : n = P*Q = 3127.
We also need a small exponent say e :
But e Must be an integer.
Not be a factor of n.
1 < e < Φ(n) [Φ(n) is discussed below], Let us now consider it to be equal to 3.
Our Public Key is made of n and e >>
Generating Private Key :
We need to calculate Φ(n) : Such that Φ(n) = (P-1)(Q-1) so, Φ(n) = 3016
Now calculate Private Key, d : d = (k*Φ(n) + 1) / e for some integer k For k = 2, value of d is
2011.
Now we are ready with our – Public Key ( n = 3127 and e = 3) and Private Key(d = 2011) Now
we will encrypt “HI” :
Convert letters to numbers : H = 8 and I = 9
Thus Encrypted Data c = 89e mod n. Thus our Encrypted Data comes out to be 1394 Now
we will decrypt 1394 :
Decrypted Data = cd mod n. Thus our Encrypted Data comes out to be 89 8 =
H and I = 9 i.e. "HI".
TEST CASE:
Test Functionality Test Data Expected Observed Positive/Negative
Case
TC1 Encryption and p=3 q=11 Encryptedmsg=18 Encryptedmsg=18 positive
decryption message=45678 Decryptedmsg=6 Decryptedmsg=6
PROGRAM:
import java.math.*;
class RSA {
public static void main(String args[])
Regulation:R17
{ int p, q, n, z, d = 0, e, i;
OUTPUT:
Aim: Implement the Diffie-Hellman Key Exchange mechanism using HTML and JavaScript.
Description:
This algorithm is used to exchange the secret key between the sender and the receiver.
This algorithm facilitates the exchange of secret key without actually transmitting it.
Algorithm-
Let-
Private key of the sender = Xs
Regulation:R17
TEST CASE:
Test Case Functionality Test Data Expected Observed Positive/Negative
TC1 Finding secret key p=7 Secret key=1 Secret key=1 positive
g=17
a=6
b=4
PROGRAM:
import java.util.*;
class DiffeHellman
{ public static void main(String args[])
{
Regulation:R17
if(S_A==S_B)
{
System.out.println("A and B can communicate with each other!!!");
System.out.println("They share a secret no = "+S_A);
}
else
{
System.out.println("A and B cannot communicate with each other!!!");
}}}
Regulation:R17
OUTPUT:
WEEK-7
AIM: Calculate the message digest of a text using the SHA-1 algorithm in JAVA
DESCRIPTION: SHA-1 or Secure Hash Algorithm 1 is a cryptographic hash function which
takes an input and produces a 160-bit (20-byte) hash value. This hash value is known as a
message digest. This message digest is usually then rendered as a hexadecimal number which is
40 digits long. It is a U.S. Federal Information Processing Standard and was designed by the
United States National Security Agency.
SHA-1 is now considered insecure since 2005. Message Digest Class provides following
cryptographic hash function to find hash value of a text as follows:
MD2
MD5
SHA-1
SHA-224
SHA-256
SHA-384
SHA-512
SHA1
Test Functionality Testdata Expected Observed Positive/
case (Plain Negative
Text)
Regulation:R17
a)
try
MessageDigest md = MessageDigest.getInstance("SHA1");
output = md.digest();
System.out.println();
System.out.println();
Regulation:R17
output = md.digest();
System.out.println();
System.out.println("");
} catch (Exception e) {
char hexDigit[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
OUTPUT:
Regulation:R17
AIM: Calculate the message digest of a text using the SHA-1 algorithm in JAVA.
DESCRIPTION: Message Digest is used to ensure the integrity of a message transmitted over
an insecure channel (where the content of the message can be changed). The message is passed
through a Cryptographic hash function. This function creates a compressed image of the
message called Digest.
Lets assume, Alice sent a message and digest pair to Bob. To check the integrity of the message
Bob runs the cryptographic hash function on the received message and gets a new digest. Now,
Bob will compare the new digest and the digest sent by Alice. If, both are same then Bob is sure
that the original message is not changed.
MD5
Test Functionality Testdata Expected Observed Positive/
case (Plain Negative
Text)
TC1 Encryption ““ D41D8CD98F00B204E98 D41D8CD98F00B204E980 Positive
00998ECF8427E 0998ECF8427E
TC2 Encryption abc 900150983CD24FB0D69 900150983CD24FB0D696 Positive
63F7D28E17F72 3F7D28E17F72
TC3 Encryption abcdefghi C3FCD3D76192E4007DF C3FCD3D76192E4007DF Positive
jklmnopqr B496CCA67E13B B496CCA67E13B
stuvwxyz
PROGRAM:
try
MessageDigest md = MessageDigest.getInstance("MD5");
System.out.println("MD5(\""+input+"\") = "
System.out.println();
output = md.digest();
System.out.println();
} catch (Exception e)
char hexDigit[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
} return buf.toString(); } }
OUTPUT: