CNS Final (1)
CNS Final (1)
Program:
#include <stdio.h>
int main() {
char str[100];
char result[100];
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
int i;
for (i = 0; str[i] != '\0'; i++) {
result[i] = str[i] ^ 0;
}
result[strcspn(result, "\n")] = '\0';
printf("Original string: %s", str);
printf("XOR result: %s\n", result);
return 0;
}
Output:
Enter a string: hello world
Original string: hello world
XOR result: hello world
scanner.close();
}
}
Output:
Enter a key (up to 16 characters): 1234567890123456
Enter plaintext (8 characters): mysecret
Ciphertext: 5C 4B 40 51 56 44 52 4C
Decrypted: mysecret
6. Write a C/JAVA program to implement the Rijndael algorithm logic.
Program:
import javax.crypto.Cipher;
import javax.crypto.spec.SecretKeySpec;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
try {
System.out.print("Enter a key (16 characters): ");
String keyInput = scanner.nextLine();
byte[] key = keyInput.getBytes();
System.out.print("Enter plaintext (16 characters): ");
String plaintextInput = scanner.nextLine();
byte[] plaintext = plaintextInput.getBytes();
if (key.length != 16 || plaintext.length != 16) {
System.out.println("Key and plaintext must be exactly 16 characters
long.");
return;
}
SecretKeySpec secretKey = new SecretKeySpec(key, "AES");
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
byte[] ciphertext = cipher.doFinal(plaintext);
System.out.print("Ciphertext: ");
for (byte b : ciphertext) {
System.out.printf("%02X ", b);
}
System.out.println();
cipher.init(Cipher.DECRYPT_MODE, secretKey);
byte[] decrypted = cipher.doFinal(ciphertext);
System.out.println("Decrypted: " + new String(decrypted));
} catch (Exception e) {
System.out.println("An error occurred: " + e.getMessage());
e.printStackTrace();
} finally {
scanner.close();
}
}
}
Output:
Enter a key (16 characters): 1234567890123456
Enter plaintext (16 characters): hellomysecretand
Ciphertext: 3C 7B 56 26 BA 5B EF 1B AD E6 12 08 80 E0 67 74 05 01 87 A0 CD
E5 A9 87 2C BA B0 91 AB 73 E5 53
Decrypted: hellomysecretand
7. Write the RC4 logic in Java Using Java cryptography; encrypt the text “Hello
world” using Blowfish. Create your own key using Java key tool.
Program:
a)RC4.java
import java.util.Arrays;
public class RC4 {
private byte[] S = new byte[256];
private int x = 0;
private int y = 0;
}
}
b)GenerateBlowFish.java
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import java.util.Base64;
Output:
Encrypted BlowFish : Z7WvoLAywxkhX18DfLZ+TQ==
Decrypted BlowFish : Hello World
8. Write a Java program to implement RSA algorithm.
Program:
import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.Scanner;
public class Main {
private BigInteger modulus;
private BigInteger privateKey;
private BigInteger publicKey;
public Main(int bitLength) {
SecureRandom random = new SecureRandom();
BigInteger p = BigInteger.probablePrime(bitLength / 2, random);
BigInteger q = BigInteger.probablePrime(bitLength / 2, random);
modulus = p.multiply(q);
BigInteger phi =
p.subtract(BigInteger.ONE).multiply(q.subtract(BigInteger.ONE));
publicKey = BigInteger.valueOf(65537);
privateKey = publicKey.modInverse(phi);
}
public BigInteger encrypt(String message) {
return new BigInteger(message.getBytes()).modPow(publicKey, modulus);
}
public String decrypt(BigInteger encryptedMessage) {
byte[] decryptedBytes = encryptedMessage.modPow(privateKey,
modulus).toByteArray();
return new String(decryptedBytes);
}
public static void main(String[] args) {
Main rsa = new Main(1024);
Scanner scanner = new Scanner(System.in);
System.out.print("Enter a message to encrypt: ");
String message = scanner.nextLine();
System.out.println("Original Message: " + message);
BigInteger encryptedMessage = rsa.encrypt(message);
System.out.println("Encrypted Message: " + encryptedMessage);
String decryptedMessage = rsa.decrypt(encryptedMessage);
System.out.println("Decrypted Message: " + decryptedMessage);
scanner.close();
}
}
Output:
Enter a message to encrypt: hello raja
Original Message: hello raja
Encrypted Message:
435413196426638748791838417368305697776238140765020878193968238
394121851229023505378467894921634507901645720805285002024989290
185635439183201300490934701021190376081518213278494835854225393
549215949910002937448351175913226799892972663263508532184518576
64034643582537917644866276913986537337140618483951996597
Decrypted Message: hello raja
9. Implement the Diffie-Hellman Key Exchange mechanism using HTML and
JavaScript.
Program:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Diffie-Hellman Key Exchange</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
input {
margin: 5px;
}
</style>
</head>
<body>
<h1>Diffie-Hellman Key Exchange</h1>
<div>
<label for="base">Base (g): </label>
<input type="number" id="base" value="5"><br>
<label for="prime">Prime (p): </label>
<input type="number" id="prime" value="23"><br>
<label for="privateKeyA">Private Key (Alice): </label>
<input type="number" id="privateKeyA" value="6"><br>
<label for="privateKeyB">Private Key (Bob): </label>
<input type="number" id="privateKeyB" value="15"><br>
<button onclick="performKeyExchange()">Perform Key Exchange</button>
</div>
<h2>Results</h2>
<p id="result"></p>
<script>
function performKeyExchange() {
const g = parseInt(document.getElementById('base').value);
const p = parseInt(document.getElementById('prime').value);
const privateKeyA =
parseInt(document.getElementById('privateKeyA').value);
const privateKeyB =
parseInt(document.getElementById('privateKeyB').value);
const publicKeyA = modExp(g, privateKeyA, p);
const publicKeyB = modExp(g, privateKeyB, p);
const sharedSecretA = modExp(publicKeyB, privateKeyA, p);
const sharedSecretB = modExp(publicKeyA, privateKeyB, p);
document.getElementById('result').innerHTML = `
<strong>Alice's Public Key:</strong> ${publicKeyA}<br>
<strong>Bob's Public Key:</strong> ${publicKeyB}<br>
<strong>Alice's Shared Secret:</strong> ${sharedSecretA}<br>
<strong>Bob's Shared Secret:</strong> ${sharedSecretB}<br>
<strong>Keys Match:</strong> ${sharedSecretA === sharedSecretB}
`;
}
function modExp(base, exp, mod) {
let result = 1;
base = base % mod;
while (exp > 0) {
if (exp % 2 === 1) { // If exp is odd
result = (result * base) % mod;
}
exp = Math.floor(exp / 2);
base = (base * base) % mod;
}
return result;
}
</script>
</body>
</html>
10. Calculate the message digest of a text using the SHA-1 algorithm in JAVA.
Program:
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter text to hash using SHA-1: ");
String inputText = scanner.nextLine();
try {
MessageDigest digest = MessageDigest.getInstance("SHA-1");
byte[] hashBytes = digest.digest(inputText.getBytes());
StringBuilder hexString = new StringBuilder();
for (byte b : hashBytes) {
String hex = Integer.toHexString(0xff & b);
if (hex.length() == 1) {
hexString.append('0');
}
hexString.append(hex);
}
System.out.println("SHA-1 Hash: " + hexString.toString());
} catch (NoSuchAlgorithmException e) {
System.err.println("SHA-1 algorithm not found.");
} finally {
scanner.close();
}
}
}
Output:
Enter text to hash using SHA-1: hello world
SHA-1 Hash: 2aae6c35c94fcfb415dbe95f408b9ce91ee846ed
11. Calculate the message digest of a text using the MD5 algorithm in JAVA.
Program:
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter text to hash using MD5: ");
String inputText = scanner.nextLine();
try {
MessageDigest digest = MessageDigest.getInstance("MD5");
byte[] hashBytes = digest.digest(inputText.getBytes());
StringBuilder hexString = new StringBuilder();
for (byte b : hashBytes) {
String hex = Integer.toHexString(0xff & b);
if (hex.length() == 1) {
hexString.append('0');
}
hexString.append(hex);
}
System.out.println("MD5 Hash: " + hexString.toString());
} catch (NoSuchAlgorithmException e) {
System.err.println("MD5 algorithm not found.");
} finally {
scanner.close();
}
}
}
Output:
Enter text to hash using MD5: hello world
MD5 Hash: 5eb63bbbe01eeed093cb22bb8f5acdc3