0% found this document useful (0 votes)
8 views13 pages

Cns Record152

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

Cns Record152

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

Roll No: Exp.

No : Date : 03-
160121733152 9 10-24
Experiment 9
Implement the Diffie-Hellman Key Exchange mechanism using HTML and JavaScript.

Aim: To implement the diffie-hellman key exchange algorithm

Description:

The Diffie-Hellman key exchange is a cryptographic protocol allowing two parties to


securely generate a shared secret key over an insecure channel. Each party generates a public
and private key, shares the public key, and uses both keys to compute a common secret. The
security relies on the difficulty of solving the Discrete Logarithm Problem, making it hard for
eavesdroppers to deduce the secret.

Source code:

<!DOCTYPE html>

<html>

<head>

<title>Diffie-Hellman Key Exchange</title>

</head>

<body>

<script>

function power(a, b, p)

if (b == 1)

return a;

else

return((Math.pow(a, b)) % p);

1
Roll No: Exp. No : Date : 03-
160121733152 9 10-24
}

var P, G, x, a, y, b, ka, kb;

P = parseInt(prompt("Enter a prime number P:"));

document.write("The value of P: " + P + "<br>");

G = parseInt(prompt("Enter a primitive root

G:")); document.write("The value of G: " + G +

"<br>");

a = parseInt(prompt("Enter the private key for Alice (a):"));

document.write("The private key (a) for Alice: " + a + "<br>");

x = power(G, a, P);

b = parseInt(prompt("Enter the private key for Bob (b):"));

document.write("The private key (b) for Bob: " + b + "<br>");

y = power(G, b, P);

ka = power(y, a, P);

kb = power(x, b, P);

document.write("Secret key for Alice is: " + ka + "<br>");

document.write("Secret key for Bob is: " + kb + "<br>");

</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.*;

public class SHA_1

public static String encryptThisString(String input)

4
Roll No: Exp. No : Date : 03-
160121733152 10 10-24
try

MessageDigest md = MessageDigest.getInstance("SHA-1");

byte[] messageDigest = md.digest(input.getBytes());

BigInteger no = new BigInteger(1, messageDigest);

String hashtext = no.toString(16);

while (hashtext.length() < 32)

hashtext = "0" + hashtext;

return hashtext;

catch (NoSuchAlgorithmException e)

throw new RuntimeException(e);

}}

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

String s1;

s1="cryptography";

System.out.println("\n" + s1 + " : " + encryptThisString(s1));

}}

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.

Aim: To implement MD5 algorithm.

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;

public class MD5 {

public static String getMd5(String input)

try {

MessageDigest md = MessageDigest.getInstance("MD5");

byte[] messageDigest = md.digest(input.getBytes());

BigInteger no = new BigInteger(1, messageDigest);

String hashtext = no.toString(16);

while (hashtext.length() < 32) {

7
Roll No: Exp. No : Date : 24-
160121733152 11 10-24
hashtext = "0" + hashtext;

return hashtext;

catch (NoSuchAlgorithmException e)

{ throw new RuntimeException(e);

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

String s1;

s1="cryptography";

System.out.println(" Hash code for "+s1 + ":" + getMd5(s1));

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

Aim: To implement the columnar 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.*;

public class ColumnarTranspositionCipher

{ static final String key = "HACK";

static Map<Character, Integer> keyMap = new HashMap<>();

static void setPermutationOrder() {

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

{ keyMap.put(key.charAt(i), i);

static String encryptMessage(String msg) {

int row, col;

StringBuilder cipher = new StringBuilder();

col = key.length();

9
Roll No: Exp. No : Date : 24-
160121733152 12 10-24
row = (int) Math.ceil((double) msg.length() / col);

char[][] matrix = new char[row][col];

for (int i = 0, k = 0; i < row; i++)

{ for (int j = 0; j < col; ) {

if (k < msg.length()) {

char ch = msg.charAt(k);

if (Character.isLetter(ch) || ch == ' ')

{ matrix[i][j] = ch;

j++;

} k+

+;

} else {

matrix[i][j] = '_'; j+

+;

for (Map.Entry<Character, Integer> entry : keyMap.entrySet())

{ int columnIndex = entry.getValue();

for (int i = 0; i < row; i++) {

if (Character.isLetter(matrix[i][columnIndex]) || matrix[i][columnIndex] == ' ' ||


matrix[i][columnIndex] == '_') {

cipher.append(matrix[i][columnIndex]);

}}}

return cipher.toString();

static String decryptMessage(String cipher) {

10
Roll No: Exp. No : Date : 24-
160121733152 12 10-24
int col = key.length();

int row = (int) Math.ceil((double) cipher.length() / col);

char[][] cipherMat = new char[row][col];

int k = 0;

for (int j = 0; j < col; j++) {

for (int i = 0; i < row; i++)

cipherMat[i][j] = cipher.charAt(k);

k++;

int index = 0;

for (Map.Entry<Character, Integer> entry : keyMap.entrySet()) {

entry.setValue(index++);

char[][] decCipher = new char[row][col];

for (int l = 0; l < key.length(); l++) {

int columnIndex = keyMap.get(key.charAt(l));

for (int i = 0; i < row; i++) {

decCipher[i][l] = cipherMat[i][columnIndex];

StringBuilder msg = new StringBuilder();

for (int i = 0; i < row; i++) {

for (int j = 0; j < col; j++)

{ 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)

{ String msg = "cryptography";

setPermutationOrder();

String cipher = encryptMessage(msg);

System.out.println("Encrypted Message: " + cipher);

System.out.println("Decrypted Message: " + decryptMessage(cipher));

}}

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

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