CNS Lab
CNS Lab
OF
COMPUTER SCIENCE AND ENGINEERING
LAB MANUAL
Program:
#include<string.h>
#include<stdio.h>
void main()
{
char str[]="Hello World";
char str1[11];
int i,len;
len=strlen(str);
for(i=0;i<len;i++)
{
str1[i]=str[i]^0;
printf("%c",str1[i]);
}
printf("\n");
}
Output:
Hello World
1
2. Write a C program that contains a string (char pointer) with a value ‘Hello World’. The program should AND and
XOR each character in this string with 127 and display the result.
Program:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char str[] = "Hello World";
char str1[12];
char str2[12];
char str3[12];
strcpy(str2, str);
int i, len;
len = strlen(str);
return 0;
}
Output:
Hello World
7␦_(
2
3. Write a Java program to perform encryption and decryption using the following algorithms:
(a)Caeser Cipher
(b)Substitution Cipher
(c)Hill Cipher
(a)Caeser Cipher
Program:
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;
(b)Substitution Cipher
Program:
import java.io.*;
import java.util.*;
Output:
Enter any string: aceho
The encrypted data is: zxvsl
(c)Hill Cipher
import java.io.*;
import java.util.*;
inverse();
System.out.print("\n");
}
System.out.println("");
System.out.println("\nInverse Matrix is : ");
for (int i = 0; i < 3; i++) {
for (int j = 0; j < 3; j++) {
System.out.print(b[i][j] + " ");
}
System.out.print("\n");
}
}
}
Output:
Inverse Matrix is :
-24.0 18.0 5.0
20.0 -15.0 -4.0
-5.0 4.0 1.0
7
4. Write a Java program to implement the DES algorithm logic.
Program:
import java.util.*;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.security.spec.KeySpec;
import javax.crypto.Cipher;
import javax.crypto.SecretKey;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.DESedeKeySpec;
import java.util.Base64;
Output:
Enter the string: Welcome
9
5. Write a C/JAVA program to implement the BlowFish algorithm logic.
Program:
import java.io.*;
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.CipherOutputStream;
import javax.crypto.KeyGenerator;
import java.util.Base64;
if (iv != null) {
System.out.println("Initialization Vector of the Cipher: " + encoder.encodeToString(iv));
}
// Read data from input file and write encrypted data to output file
FileInputStream fin = new FileInputStream("inputFile.txt"); // Input file
FileOutputStream fout = new FileOutputStream("outputFile.txt"); // Output file
CipherOutputStream cout = new CipherOutputStream(fout, cipherOut);
int input = 0;
while ((input = fin.read()) != -1) {
cout.write(input); // Encrypt and write data to output file
}
// Optionally, print out the contents of input and output files to verify
System.out.println("\nContents of inputFile.txt:");
printFileContent("inputFile.txt");
10
// Helper method to print the content of a file
private static void printFileContent(String filename) throws IOException {
BufferedReader reader = new BufferedReader(new FileReader(filename));
String line;
while ((line = reader.readLine()) != null) {
System.out.println(line); // Print each line of the file
}
reader.close();
}
}
Output:
Contents of inputFile.txt:
Hello World
11
6. Write a C/JAVA program to implement the Rijndael algorithm logic.
Program:
import java.security.*;
import javax.crypto.*;
import javax.crypto.spec.*;
import java.io.*;
import java.util.Base64; // Added for Base64 encoding/decoding
import java.util.Scanner; // Importing Scanner for user input
// Encryption
cipher.init(Cipher.ENCRYPT_MODE, skeySpec);
byte[] encrypted = cipher.doFinal(message.getBytes()); // Encrypt the message
Output:
13
7. Using Java Cryptography, encrypt the text “Hello world” using BlowFish.
Create your own key using Java keytool.
Program:
import javax.crypto.Cipher;
import javax.crypto.KeyGenerator;
import javax.crypto.SecretKey;
import javax.swing.JOptionPane;
// Create a key
SecretKey secretkey = keygenerator.generateKey();
// Encrypt message
byte[] encrypted = cipher.doFinal(inputText.getBytes());
// Decrypt message
byte[] decrypted = cipher.doFinal(encrypted);
System.exit(0);
}
}
Output:
Program:
import java.math.BigInteger;
import java.util.Random;
import java.util.Scanner;
// Encrypt a message
System.out.print("Enter a message to encrypt (in numeric form): ");
BigInteger message = sc.nextBigInteger(); // Input message as a numeric value
BigInteger encryptedMessage = encrypt(message, e, n);
System.out.println("Encrypted message: " + encryptedMessage);
do {
15
y = x.nextInt(fiofn.intValue() - 1) + 2; // Generate random y between 2 and φ(n)-1
e = new BigInteger(Integer.toString(y));
} while (fiofn.gcd(e).compareTo(BigInteger.ONE) != 0); // Ensure gcd(e, φ(n)) = 1
return e;
}
Output:
16
9. Implement the Diffie-Hellman Key Exchange mechanism using HTML and
JavaScript. Consider the end user as one of the parties (Alice) and the JavaScript application as other party (bob).
Program:
import java.math.BigInteger;
import java.security.*;
import javax.crypto.*;
import javax.crypto.interfaces.DHPublicKey; // Import for DHPublicKey
import javax.crypto.spec.*;
Output:
18
10. Calculate the message digest of a text using the SHA-1 algorithm in JAVA.
Program:
import java.security.*;
input = "abc";
md.update(input.getBytes());
output = md.digest();
System.out.println();
System.out.println("SHA1(\"" + input + "\") = " + bytesToHex(output));
input = "abcdefghijklmnopqrstuvwxyz";
md.update(input.getBytes());
output = md.digest();
System.out.println();
System.out.println("SHA1(\"" + input + "\") = " + bytesToHex(output));
System.out.println("");
} catch (Exception e) {
System.out.println("Exception: " + e);
}
}
return buf.toString();
19
}
}
Output:
SHA1("") = DA39A3EE5E6B4B0D3255BFEF95601890AFD80709
SHA1("abc") = A9993E364706816ABA3E25717850C26C9CD0D89D
SHA1("abcdefghijklmnopqrstuvwxyz") = 32D10C7B8CF96570CA04CE37F2A19D84240D3A89
20