CNS Lab Manual
1. AIM: 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>
#include<stdlib.h>
void main(){
char *str
= "Hello World";
int l,i;
//finding
string length
l =
strlen(str);
printf("After
xoring each character with '0'\n");
printf("character
^ 0 \t= xoring result -> ASCII value\n ");
for(i=0;
i<l; i++){
printf("%c\t^
%d\t = %d\t %c\n", str[i], 0,
str[i]^0, (char)str[i]^0);
}
}
Explanation:
·
The code starts by declaring a string called
"str" and initializing it with the text "Hello World".
·
The next line declares an integer variable
called l, which will be used to store the length of the string.
·
Next, printf() is used to print out a message
that says after xoring each character with '0', then printing out the ASCII
value for each character in order.
·
The code starts by declaring a string called
"str" and initializing it with the text "Hello World".
·
The next line declares an integer variable
called l, which will be used to store the length of the string.
·
Next, printf() is used to print out a message
that says after xoring each character with '0', then printing out the ASCII
value for each character in order.
·
The code is meant to display the character
'H' in ASCII code.
·
The ^ operator is used to xor two numbers
together, and then the result of that operation will be stored into a variable.
2. AIM: 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>
#include<stdlib.h>
void main(){
char
str[]="Hello World";
int i,len;
len = strlen(str);
for(i=0;i<len;i++){
printf("%c",str[i]&127);
}
printf("\n");
for(int
i=0;i<len;i++){
printf("%c",str[i]^127);
}
printf("\n");
for(int
i=0;i<len;i++){
printf("%c",str[i]|127);
}
printf("\n");
}
Explanation:
·
The for loop iterates through all of the characters
in str until len is reached.
·
For each iteration, a character from str is
printed on screen with its ASCII value (the number that corresponds to that
letter) and then its binary representation (its bit pattern).
·
The first line prints out 'H' using
printf("%c",str[0]&127); which outputs H as 0x48 since 48 = 128 +
64 + 32.
·
The second line prints out 'e' using
printf("%c",str[1]&127); which outputs e as 0x65 since 65 = 96 +
8 + 16.
·
The third line prints out 'l' using
printf("%c",str[2]&127); which outputs l as 0x6C since 6C = 112 +
44+ 12+ 4+ 2+.
·
The code will print the following: Hello
World Hello World
AIM: Write a Java program to perform encryption and decryption using
the following algorithms:
a) Ceaser Cipher
b) Substitution Cipher
c) Hill Cipher
Program:
a) Ceaser Cipher
// import required classes and package, if any
import java.util.Scanner;
// create class CaesarCipherExample for encryption and
decryption
public class CaesarCipherExample
{
// ALPHABET string
denotes alphabet from a-z
public static
final String ALPHABET = "abcdefghijklmnopqrstuvwxyz";
// create
encryptData() method for encrypting user input string with given shift key
public static
String encryptData(String inputStr, int shiftKey)
{
// convert
inputStr into lower case
inputStr =
inputStr.toLowerCase();
// encryptStr
to store encrypted data
String
encryptStr = "";
// use for
loop for traversing each character of the input string
for (int i =
0; i < inputStr.length(); i++)
{
// get
position of each character of inputStr in ALPHABET
int pos =
ALPHABET.indexOf(inputStr.charAt(i));
// get
encrypted char for each char of inputStr
int
encryptPos = (shiftKey + pos) % 26;
char
encryptChar = ALPHABET.charAt(encryptPos);
// add
encrypted char to encrypted string
encryptStr
+= encryptChar;
}
// return
encrypted string
return
encryptStr;
}
// create
decryptData() method for decrypting user input string with given shift key
public static
String decryptData(String inputStr, int shiftKey)
{
// convert
inputStr into lower case
inputStr =
inputStr.toLowerCase();
// decryptStr
to store decrypted data
String
decryptStr = "";
// use for
loop for traversing each character of the input string
for (int i =
0; i < inputStr.length(); i++)
{
// get
position of each character of inputStr in ALPHABET
int pos =
ALPHABET.indexOf(inputStr.charAt(i));
// get
decrypted char for each char of inputStr
int
decryptPos = (pos - shiftKey) % 26;
// if
decryptPos is negative
if
(decryptPos < 0){
decryptPos = ALPHABET.length() + decryptPos;
}
char
decryptChar = ALPHABET.charAt(decryptPos);
// add
decrypted char to decrypted string
decryptStr
+= decryptChar;
}
// return
decrypted string
return
decryptStr;
}
// main() method
start
public static void
main(String[] args)
{
// create an
instance of Scanner class
Scanner sc =
new Scanner(System.in);
// take input
from the user
System.out.println("Enter a string for encryption using Caesar
Cipher: ");
String
inputStr = sc.nextLine();
System.out.println("Enter the value by which each character in the
plaintext message gets shifted: ");
int shiftKey =
Integer.valueOf(sc.nextLine());
System.out.println("Encrypted Data ===>
"+encryptData(inputStr, shiftKey));
System.out.println("Decrypted Data ===>
"+decryptData(encryptData(inputStr, shiftKey), shiftKey));
// close
Scanner class object
sc.close();
}
}
Output:
Enter any String:
Hello World
Enter the Key: 5
Encrypted String is:
mjqqtebtwqi
Decrypted String is:
Hello World
Explanation:
·
The code starts by declaring the
encryptData() method.
·
The first line of code declares a String
variable called inputStr, which will be used to store the user input string
that is being encrypted.
·
The next line of code creates an instance of
the String class and assigns it to the inputStr variable.
·
This is done so that when we call this
function later on in our program, we can use this as a reference to what data
was passed into our function.
·
Next, there are two lines of code that
convert the user's input string into lower case characters before assigning it
to another local variable called encryptStr for storing encrypted data.
·
Then comes a for loop with three statements
inside it: one statement initializes i with 0; one statement gets position of
each character in ALPHABET using indexOf(); and one statement increments i by 1
after every iteration through all 26 letters in alphabet (A-Z).
·
After these three statements have been
executed, there will be 26 iterations through all 26 letters in alphabet (A-Z)
where pos will equal ALPHABET.indexOf(inputStr.charAt(i)).
·
The code will iterate through each character
of the input string and store its position in the ALPHABET array.
·
The for loop will then use the indexOf()
method to find out where each character is located in the alphabet.
·
This is done so that we can encrypt each
character with a different key.
·
For example, if you want to encrypt
"A" with key 0, then it would be stored as "Z".
·
If you wanted to encrypt "B" with
key 1, it would be stored as "Y".
·
And so on and so forth.
·
The code starts by creating an instance of
the Scanner class.
·
The user is then asked to input a string,
which is stored in the variable "inputStr".
·
The code uses a for loop to traverse each
character of the input string and get its position in ALPHABET.
·
It also gets the decrypted char for each char
of inputStr and adds it to decryptStr.
·
The code is an example of how a user input
string can be encrypted and decrypted.
·
The method takes the user input string as its
argument, encrypts it using the given shift key, and returns the encrypted
string.
·
The method also has a decryptData() method
which takes the user input string as its argument, decrypts it with the given
shift key, and returns the decrypted string.
b) Substitution cipher
Program:
import java.io.*;
import java.util.*;
public class SubstitutionCipher{
static Scanner sc = new Scanner(System.in);
static BufferedReader br = new BufferedReader(new
InputStreamReader(System.in)); public
static void main(String[] args) throws IOException {
// TODO code application logic here
String a = "abcdefghijklmnopqrstuvwxyz";
String b ="zyxwvutsrqponmlkjihgfedcba";
System.out.print("Enter any string: "); String
str = br.readLine();
String decrypt = "";
char c;
for(int i=0;i<str.length();i++)
{
c = str.charAt(i); int
j = a.indexOf(c);
decrypt = decrypt+b.charAt(j);
}
System.out.println("The encrypted data is: "
+decrypt);
}
}
c) Hill Cipher
// Java code to implement Hill Cipher
class GFG
{
// Following function generates the
// key matrix for the key string
static void getKeyMatrix(String key, int keyMatrix[][])
{
int k =
0;
for (int
i = 0; i < 3; i++)
{
for
(int j = 0; j < 3; j++)
{
keyMatrix[i][j]
= (key.charAt(k)) % 65;
k++;
}
}
}
// Following function encrypts the message
static void encrypt(int cipherMatrix[][],
int
keyMatrix[][],
int
messageVector[][])
{
int x, i,
j;
for (i =
0; i < 3; i++)
{
for
(j = 0; j < 1; j++)
{
cipherMatrix[i][j]
= 0;
for
(x = 0; x < 3; x++)
{
cipherMatrix[i][j]
+=
keyMatrix[i][x]
* messageVector[x][j];
}
cipherMatrix[i][j]
= cipherMatrix[i][j] % 26;
}
}
}
// Function to implement Hill Cipher
static void HillCipher(String message, String key)
{
// Get
key matrix from the key string
int
[][]keyMatrix = new int[3][3];
getKeyMatrix(key,
keyMatrix);
int
[][]messageVector = new int[3][1];
//
Generate vector for the message
for (int
i = 0; i < 3; i++)
messageVector[i][0]
= (message.charAt(i)) % 65;
int
[][]cipherMatrix = new int[3][1];
//
Following function generates
// the
encrypted vector
encrypt(cipherMatrix,
keyMatrix, messageVector);
String
CipherText="";
//
Generate the encrypted text from
// the
encrypted vector
for (int
i = 0; i < 3; i++)
CipherText
+= (char)(cipherMatrix[i][0] + 65);
//
Finally print the ciphertext
System.out.print("
Ciphertext:" + CipherText);
}
// Driver code
public static void main(String[] args)
{
// Get
the message to be encrypted
String
message = "HEY";
// Get
the key
String
key = "GYBNQKURP";
HillCipher(message,
key);
}
}
Output:
Ciphertext:POH
Comments
Post a Comment