Cns Record152
Cns Record152
No : Date : 03-
160121733152 9 10-24
Experiment 9
Implement the Diffie-Hellman Key Exchange mechanism using HTML and JavaScript.
Description:
Source code:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script>
function power(a, b, p)
if (b == 1)
return a;
else
1
Roll No: Exp. No : Date : 03-
160121733152 9 10-24
}
"<br>");
x = power(G, a, P);
y = power(G, b, P);
ka = power(y, a, P);
kb = power(x, b, P);
</script>
</body>
</html>
Output :
Conclusion :
Diffie-Hellman enables secure key exchange over public channels by allowing two parties to
establish a shared secret without directly transmitting it. Its security foundation on the
Discrete Logarithm Problem makes it highly resistant to interception. Widely used in secure
2
Roll No: Exp. No : Date : 03-
160121733152 9 10-24
communications, it provides a basis for modern encryption protocols like SSL/TLS.
3
Roll No: Exp. No : Date : 03-
160121733152 10 10-24
Experiment 10
Calculate the message digest of a text using the SHA-1 algorithm in JAVA.
Aim: To calculate the message digest of a given text using the SHA-1 algorithm
Description:
The SHA-1 (Secure Hash Algorithm 1) is a cryptographic hash function that produces a 160-
bit hash value (or message digest) for an input message. Hash functions are used to ensure
data integrity, as the hash will change with any alteration in the input text.
Source code:
package sha_1;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;
4
Roll No: Exp. No : Date : 03-
160121733152 10 10-24
try
MessageDigest md = MessageDigest.getInstance("SHA-1");
return hashtext;
catch (NoSuchAlgorithmException e)
}}
String s1;
s1="cryptography";
}}
Output:
Conclusion :
SHA-1 is a widely used cryptographic hash function that generates a unique 160-bit hash
value for a given input, ensuring data integrity by detecting even minor changes in input data.
Although it has been largely phased out for security-sensitive applications due to
vulnerability to collision attacks, SHA-1 remains a foundational hash function in
5
Roll No: Exp. No : Date : 03-
160121733152 10 10-24
cryptography histo
6
Roll No: Exp. No : Date : 24-
160121733152 11 10-24
Experiment 11
Calculate the message digest of a text using the MD5 algorithm in JAVA.
Description:
MD5 (Message Digest Algorithm 5) is a cryptographic hash function that produces a 128-bit
hash value represented as a 32-character hexadecimal number. It is commonly used for
checksums and data integrity verification, as even a small change in the input yields a
completely different hash.
Source code :
import java.util.Date;
import java.math.BigInteger;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Scanner;
try {
MessageDigest md = MessageDigest.getInstance("MD5");
7
Roll No: Exp. No : Date : 24-
160121733152 11 10-24
hashtext = "0" + hashtext;
return hashtext;
catch (NoSuchAlgorithmException e)
String s1;
s1="cryptography";
Output :
Conclusion:
While MD5 was once popular for data integrity verification and digital signatures, it is now
considered insecure for cryptographic use due to vulnerability to collisions. The algorithm’s
susceptibility to attacks limits its effectiveness in security-critical applications, though it is
still occasionally used in non-secure contexts where speed is prioritized over security.
8
Roll No: Exp. No : Date : 24-
160121733152 12 10-24
Experiment 12
Implement Simple Columner Transposition technique and Advanced Columner Transposition
technique
Description:
The columnar transposition is a classical encryption method that rearranges the characters of
a plaintext by writing them into a grid of columns and then reading them out in a different,
predefined order based on a keyword.
o Choose a Keyword
o Write the Message in Rows
o Reorder Columns by Keyword
o Read Vertically – cipher text
The plaintext is written out in rows within a grid of fixed column width and then read
vertically according to a defined key order. The order of columns is rearranged based on a
keyword, creating the cipher text by reading the columns sequentially according to the key.
This technique provides a level of security by scrambling the message structure, though it is
susceptible to frequency analysis and thus best suited for simpler encryption needs.
Source code:
import java.util.*;
{ keyMap.put(key.charAt(i), i);
col = key.length();
9
Roll No: Exp. No : Date : 24-
160121733152 12 10-24
row = (int) Math.ceil((double) msg.length() / col);
if (k < msg.length()) {
char ch = msg.charAt(k);
{ matrix[i][j] = ch;
j++;
} k+
+;
} else {
matrix[i][j] = '_'; j+
+;
cipher.append(matrix[i][columnIndex]);
}}}
return cipher.toString();
10
Roll No: Exp. No : Date : 24-
160121733152 12 10-24
int col = key.length();
int k = 0;
cipherMat[i][j] = cipher.charAt(k);
k++;
int index = 0;
entry.setValue(index++);
decCipher[i][l] = cipherMat[i][columnIndex];
{ if (decCipher[i][j] !=
'_') {
msg.append(decCipher[i][j]);
} }}
11
Roll No: Exp. No : Date : 24-
160121733152 12 10-24
return msg.toString();
12
Roll No: Exp. No : Date : 24-
160121733152 12 10-24
public static void main(String[] args)
setPermutationOrder();
}}
Output:
Conclusion:
The columnar transposition cipher is an effective method for rearranging plaintext to create a
more secure cipher text through simple reordering. While it offers basic encryption and is
easy to implement, its vulnerability to frequency analysis limits its use to low-security
applications. This technique is often combined with other ciphers to enhance overall
encryption strength.
13