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

EXPERMENT

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 views15 pages

EXPERMENT

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/ 15

DEPARTMENT OF COMPUTER SCIENCE

CRYPTOGRAPHY AND NETWOK SECURITY 3rd BTECH[3-2]


EXPERMENT:-1

1.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 displays
the result.
PROGRAM:
#include <stdio.h>
#include <string.h>

int main()
{
// Declare a char pointer with the value "Hello World"
char *str = "Hello World";
// Get the length of the string
int len = strlen(str);
// Loop through each character in the string
for (int i = 0; i < len; i++)
{
// XOR each character with 0 and print the result
printf("%c", str[i] ^ 0);
}
// Print a newline
printf("\n");
// Return 0 to indicate success
return 0;
}

OUTPUT: Hello world

Signature of faculty:
DEPARTMENT OF COMPUTER SCIENCE

CRYPTOGRAPHY AND NETWOK SECURITY 3rd BTECH[3-2]

EXPERMENT:-2

2. 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.
PROGRAM:
#include <stdio.h>
#include <string.h>
int main()
{
// Declare a char pointer with the value "Hello World"
char *str = "Hello World";
// Get the length of the string
int len = strlen(str);
// Loop through each character in the string
for (int i = 0; i < len; i++)
{
// AND each character with 127 and print the result
printf("%c", str[i] & 127);
}
// Print a newline
printf("\n");
// Return 0 to indicate success
return 0;
}
OUTPUT: Hello world

Signature of faculty:
DEPARTMENT OF COMPUTER SCIENCE

CRYPTOGRAPHY AND NETWOK SECURITY 3rd BTECH[3-2]

EXPERMENT: 3
Write a Java program to perform encryption and decryption using the following
algorithms:
a. Ceaser Cipher
PROGRAM:
import java.util.Scanner;

public class CaesarCipher {

// A constant string that contains all the letters of the alphabet


public static final String ALPHABET = "abcdefghijklmnopqrstuvwxyz";

// A method that encrypts a message by shifting each letter by the given key
public static String encrypt(String message, int key) {
// Convert the message to lower case
message = message.toLowerCase();
// A string to store the encrypted message
String encrypted = "";
// Loop through each character of the message
for (int i = 0; i < message.length(); i++) {
// Get the current character
char c = message.charAt(i);
// Check if the character is a letter
if (c >= 'a' && c <= 'z') {
// Find the index of the character in the alphabet
int index = ALPHABET.indexOf(c);
// Compute the new index by adding the key and modulo 26
int newIndex = (index + key) % 26;

Signature of faculty:
DEPARTMENT OF COMPUTER SCIENCE

CRYPTOGRAPHY AND NETWOK SECURITY 3rd BTECH[3-2]

// Get the new character from the alphabet


char newChar = ALPHABET.charAt(newIndex);
// Append the new character to the encrypted message
encrypted += newChar;
} else {
// If the character is not a letter, just append it as it is
encrypted += c;
}
}
// Return the encrypted message
return encrypted;
}

// A method that decrypts a message by shifting each letter back by the given key
public static String decrypt(String message, int key) {
// Convert the message to lower case
message = message.toLowerCase();
// A string to store the decrypted message
String decrypted = "";
// Loop through each character of the message
for (int i = 0; i < message.length(); i++) {
// Get the current character
char c = message.charAt(i);
// Check if the character is a letter
if (c >= 'a' && c <= 'z') {
// Find the index of the character in the alphabet
int index = ALPHABET.indexOf(c);
// Compute the new index by subtracting the key and modulo 26

Signature of faculty:
DEPARTMENT OF COMPUTER SCIENCE

CRYPTOGRAPHY AND NETWOK SECURITY 3rd BTECH[3-2]

int newIndex = (index - key + 26) % 26;


// Get the new character from the alphabet
char newChar = ALPHABET.charAt(newIndex);
// Append the new character to the decrypted message
decrypted += newChar;
} else {
// If the character is not a letter, just append it as it is
decrypted += c;
}
}
// Return the decrypted message
return decrypted;
}

// A main method that tests the encryption and decryption methods


public static void main(String[] args) {
// Create a scanner object to read user input
Scanner sc = new Scanner(System.in);
// Prompt the user to enter a message
System.out.println("Enter a message: ");
// Read the message from the user
String message = sc.nextLine();
// Prompt the user to enter a key
System.out.println("Enter a key (0-25): ");
// Read the key from the user
int key = sc.nextInt();
// Validate the key
if (key < 0 || key > 25) {

Signature of faculty:
DEPARTMENT OF COMPUTER SCIENCE

CRYPTOGRAPHY AND NETWOK SECURITY 3rd BTECH[3-2]

// If the key is invalid, print an error message and exit


System.out.println("Invalid key. It must be between 0 and 25.");
System.exit(1);
}
// Encrypt the message using the key
String encrypted = encrypt(message, key);
// Print the encrypted message
System.out.println("Encrypted message: " + encrypted);
// Decrypt the message using the key
String decrypted = decrypt(encrypted, key);
// Print the decrypted message
System.out.println("Decrypted message: " + decrypted);
}
}

OUTPUT:
Enter a message:
Hello World
Enter a key (0-25):
3
Encrypted message: khoor zruog
Decrypted message: hello world

Signature of faculty:
DEPARTMENT OF COMPUTER SCIENCE

CRYPTOGRAPHY AND NETWOK SECURITY 3rd BTECH[3-2]

B. Substitution Cipher
PROGRAM:
import java.util.HashMap;
import java.util.Scanner;

public class SubstitutionCipher {

// A constant string that contains all the letters of the alphabet


public static final String ALPHABET = "abcdefghijklmnopqrstuvwxyz";

// A method that creates a mapping of letters based on a given key


public static HashMap<Character, Character> createMapping(String key) {
// A hash map to store the mapping of letters
HashMap<Character, Character> map = new HashMap<>();
// A string to store the remaining letters that are not in the key
String remaining = "";
// Loop through each letter of the alphabet
for (char c : ALPHABET.toCharArray()) {
// If the letter is not in the key, add it to the remaining string
if (key.indexOf(c) == -1) {
remaining += c;
}
}
// Loop through each letter of the key
for (int i = 0; i < key.length(); i++) {
// Map the letter of the key to the corresponding letter of the alphabet
map.put(key.charAt(i), ALPHABET.charAt(i));
}

Signature of faculty:
DEPARTMENT OF COMPUTER SCIENCE

CRYPTOGRAPHY AND NETWOK SECURITY 3rd BTECH[3-2]

// Loop through the remaining letters


for (int i = 0; i < remaining.length(); i++) {
// Map the remaining letter to the remaining letter of the alphabet
map.put(remaining.charAt(i), ALPHABET.charAt(i + key.length()));
}
// Return the mapping
return map;
}

// A method that encrypts a message using a mapping of letters


public static String encrypt(String message, HashMap<Character, Character> map) {
// Convert the message to lower case
message = message.toLowerCase();
// A string to store the encrypted message
String encrypted = "";
// Loop through each character of the message
for (char c : message.toCharArray()) {
// If the character is a letter, replace it with the mapped letter
if (map.containsKey(c)) {
encrypted += map.get(c);
} else {
// If the character is not a letter, just append it as it is
encrypted += c;
}
}
// Return the encrypted message
return encrypted;
}

Signature of faculty:
DEPARTMENT OF COMPUTER SCIENCE

CRYPTOGRAPHY AND NETWOK SECURITY 3rd BTECH[3-2]

// A method that decrypts a message using a mapping of letters


public static String decrypt(String message, HashMap<Character, Character> map) {
// Convert the message to lower case
message = message.toLowerCase();
// A string to store the decrypted message
String decrypted = "";
// Loop through each character of the message
for (char c : message.toCharArray()) {
// If the character is a letter, replace it with the inverse mapped letter
if (map.containsValue(c)) {
// Loop through the keys of the map
for (char key : map.keySet()) {
// If the value of the key is equal to the character, append the key to the decrypted message
if (map.get(key) == c) {
decrypted += key;
break;
}
}
} else {
// If the character is not a letter, just append it as it is
decrypted += c;
}
}
// Return the decrypted message
return decrypted;
}

// A main method that tests the encryption and decryption methods

Signature of faculty:
DEPARTMENT OF COMPUTER SCIENCE

CRYPTOGRAPHY AND NETWOK SECURITY 3rd BTECH[3-2]

public static void main(String[] args) {


// Create a scanner object to read user input
Scanner sc = new Scanner(System.in);
// Prompt the user to enter a key
System.out.println("Enter a key (a word without repeated letters): ");
// Read the key from the user
String key = sc.nextLine();
// Validate the key
if (key.length() > 26 || !key.matches("[a-zA-Z]+")) {
// If the key is invalid, print an error message and exit
System.out.println("Invalid key. It must be a word without repeated letters and less than 26
letters.");
System.exit(1);
}
// Create a mapping of letters using the key
HashMap<Character, Character> map = createMapping(key);
// Print the mapping
System.out.println("The mapping of letters is: ");
for (char c : map.keySet()) {
System.out.println(c + " -> " + map.get(c));
}
// Prompt the user to enter a message
System.out.println("Enter a message: ");
// Read the message from the user
String message = sc.nextLine();
// Encrypt the message using the mapping
String encrypted = encrypt(message, map);
// Print the encrypted message
System.out.println("Encrypted message: " + encrypted);
Signature of faculty:
DEPARTMENT OF COMPUTER SCIENCE

CRYPTOGRAPHY AND NETWOK SECURITY 3rd BTECH[3-2]

// Decrypt the message using the mapping


String decrypted = decrypt(encrypted, map);
// Print the decrypted message
System.out.println("Decrypted message: " + decrypted);
}
}

OUTPUT:
Enter a key (a word without repeated letters):
java
The mapping of letters is:
j -> a
a -> b
v -> c
b -> d
c -> e
d -> f
e -> g
f -> h
g -> i
h -> j
i -> k
k -> l
l -> m
m -> n
n -> o
o -> p
p -> q

Signature of faculty:
DEPARTMENT OF COMPUTER SCIENCE

CRYPTOGRAPHY AND NETWOK SECURITY 3rd BTECH[3-2]

q -> r
r -> s
s -> t
t -> u
u -> v
w -> w
x -> x
y -> y
z -> z
Enter a message:
Hello World
Encrypted message: jgssb wosjc
Decrypted message: hello world

Signature of faculty:
DEPARTMENT OF COMPUTER SCIENCE

CRYPTOGRAPHY AND NETWOK SECURITY 3rd BTECH[3-2]

C. Hill Cipher
PROGRAM:
import java.util.Scanner;

public class HillCipherExample {


// Method to accept the key matrix
private static int[][] getKeyMatrix() {
Scanner sc = new Scanner(System.in);
System.out.println("Enter key matrix (4 characters):");
String key = sc.nextLine();
double sq = Math.sqrt(key.length());

int[][] matrix = new int[2][2];


int index = 0;
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
matrix[i][j] = key.charAt(index) - 'A';
index++;
}
}
return matrix;
}

// Main method
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Enter plain text (4 characters):");
String plainText = sc.nextLine().toUpperCase();

Signature of faculty:
DEPARTMENT OF COMPUTER SCIENCE

CRYPTOGRAPHY AND NETWOK SECURITY 3rd BTECH[3-2]

int[][] keyMatrix = getKeyMatrix();

// Encryption
int[] plainVector = new int[2];
for (int i = 0; i < 2; i++) {
plainVector[i] = plainText.charAt(i) - 'A';
}

int[] encryptedVector = new int[2];


for (int i = 0; i < 2; i++) {
for (int j = 0; j < 2; j++) {
encryptedVector[i] += keyMatrix[i][j] * plainVector[j];
}
encryptedVector[i] %= 26;
}

StringBuilder encryptedText = new StringBuilder();


for (int i : encryptedVector) {
encryptedText.append((char) ('A' + i));
}

System.out.println("Encrypted ciphertext: " + encryptedText.toString());


}
}

OUTPUT:

Signature of faculty:
DEPARTMENT OF COMPUTER SCIENCE

CRYPTOGRAPHY AND NETWOK SECURITY 3rd BTECH[3-2]

Signature of faculty:

You might also like