EXPERMENT
EXPERMENT
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;
}
Signature of faculty:
DEPARTMENT OF COMPUTER SCIENCE
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
EXPERMENT: 3
Write a Java program to perform encryption and decryption using the following
algorithms:
a. Ceaser Cipher
PROGRAM:
import java.util.Scanner;
// 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
// 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
Signature of faculty:
DEPARTMENT OF COMPUTER SCIENCE
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
B. Substitution Cipher
PROGRAM:
import java.util.HashMap;
import java.util.Scanner;
Signature of faculty:
DEPARTMENT OF COMPUTER SCIENCE
Signature of faculty:
DEPARTMENT OF COMPUTER SCIENCE
Signature of faculty:
DEPARTMENT OF COMPUTER SCIENCE
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
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
C. Hill Cipher
PROGRAM:
import java.util.Scanner;
// 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
// Encryption
int[] plainVector = new int[2];
for (int i = 0; i < 2; i++) {
plainVector[i] = plainText.charAt(i) - 'A';
}
OUTPUT:
Signature of faculty:
DEPARTMENT OF COMPUTER SCIENCE
Signature of faculty: