Cns Lab Manual
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.
Implementation Code:
#include <stdio.h>
#include <string.h>
int main() {
char str[] = "Hello World";
char str1[11]; // Make sure to allocate enough space for the null terminator
int i, len;
len = strlen(str);
printf("\n");
return 0;
}
Output:
Hello World
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int main()
{
char text[500], ch;
int key;
// Encrypting message
for (int i = 0; text[i] != '\0'; ++i) {
ch = text[i];
if (isalpha(ch)) {
if (islower(ch)) {
ch = 'a' + (ch - 'a' + key) % 26;
} else if (isupper(ch)) {
ch = 'A' + (ch - 'A' + key) % 26;
}
} else if (isdigit(ch)) {
ch = '0' + (ch - '0' + key) % 10;
}
// else: leave non-alphanumeric characters unchanged
text[i] = ch;
}
return 0;
}
Output:
Enter a message to encrypt: yZq8NS92mdR
Enter the key: 6
Encrypted message: eFw4TY58sjX
4. Aim: Write a C program to implementation of Caesar Cipher Decryption technique.
#include<stdio.h>
#include<ctype.h>
int main() {
char text[500], ch;
int key;
// Taking user input.
printf("Enter a message to decrypt: ");
scanf("%s", text);
printf("Enter the key: ");
scanf("%d", & key);
// Visiting each character.
for (int i = 0; text[i] != '\0'; ++i) {
ch = text[i];
// Check for valid characters.
if (isalnum(ch)) {
//Lowercase characters.
if (islower(ch)) {
ch = (ch - 'a' - key + 26) % 26 + 'a';
}
// Uppercase characters.
if (isupper(ch)) {
ch = (ch - 'A' - key + 26) % 26 + 'A';
}
// Numbers.
if (isdigit(ch)) {
ch = (ch - '0' - key + 10) % 10 + '0';
}
}
// Invalid characters.
else {
printf("Invalid Message");
}
// Adding decoded character back.
text[i] = ch;
}
printf("Decrypted message: %s", text);
return 0;
}
Output:
Output:
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.security.Key;
import javax.crypto.Cipher;
import javax.crypto.CipherInputStream;
import javax.crypto.KeyGenerator;
public class AESEncryptionDecryption {
Output:
Encryption and Decryption of plain text file performed successfully.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
int main() {
printf("\nEnter first prime number: ");
scanf("%ld", &p);
flag = prime(p);
if (flag == 0) {
printf("\nWRONG INPUT\n");
exit(1);
}
encrypt();
decrypt();
return 0;
}
void ce() {
int k;
k = 0;
for (i = 2; i < t; i++) {
if (t % i == 0)
continue;
flag = prime(i);
if (flag == 1 && i != p && i != q) {
e[k] = i;
flag = cd(e[k]);
if (flag > 0) {
d[k] = flag;
k++;
}
if (k == 99)
break;
}
}
}
void decrypt() {
long int pt, ct, key = d[0], k;
i = 0;
while (en[i] != -1) {
ct = temp[i];
k = 1;
for (j = 0; j < key; j++) {
k = k * ct;
k = k % n;
}
pt = k + 96;
m[i] = pt;
i++;
}
m[i] = -1;
printf("\nTHE DECRYPTED MESSAGE IS\n");
for (i = 0; m[i] != -1; i++)
printf("%c", m[i]);
}
Output:
Enter first prime number: 7
Enter another prime number: 3
Enter message to be encrypted: swetha
Possible values of e and d are:
5 5
11 11
THE ENCRYPTED MESSAGE IS
jkqtha
THE DECRYPTED MESSAGE IS
sbetha
• Write a C program to implement the Diffie-Hellman Key Exchange mechanism.
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
int main() {
long long int prime_number = 23; // Example prime number
long long int primitive_root = 5; // Example primitive root modulo 23
long long int private_key_Alice = 6; // Alice's private key
long long int private_key_Bob = 15; // Bob's private key
if (shared_secret != -1) {
printf("Shared secret: %lld\n", shared_secret);
} else {
printf("Error: Shared secret calculation failed.\n");
}
return 0;
}
import java.security.*;
} catch (NoSuchAlgorithmException e) {
System.err.println("MD5 algorithm not found");
e.printStackTrace();
}
}
}
output:
Message digest object info:
Algorithm = MD5
Provider = SUN version 11
ToString = MD5 Message Digest from SUN, <initialized>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>
#define SIZE 20
struct DataItem {
int data;
int key;
};
struct DataItem* hashArray[SIZE];
struct DataItem* dummyItem;
struct DataItem* item;
int hashCode(int key) {
return key % SIZE;
}
struct DataItem *search(int key)
{
int hashIndex = hashCode(key);
while(hashArray[hashIndex] != NULL)
{
if(hashArray[hashIndex]->key == key)
return hashArray[hashIndex];
++hashIndex;
hashIndex %= SIZE;
}
return NULL;
}
void insert(int key,int data)
{
struct DataItem *item = (struct DataItem*) malloc(sizeof(struct DataItem)); item->data = data;
item->key = key;
int hashIndex = hashCode(key);
while(hashArray[hashIndex] != NULL && hashArray[hashIndex]->key != -1)
{
++hashIndex;
hashIndex %= SIZE;
}
hashArray[hashIndex] = item;
}
struct DataItem* delete(struct DataItem* item)
{
int key = item->key;
int hashIndex = hashCode(key);
while(hashArray[hashIndex] != NULL)
{
if(hashArray[hashIndex]->key == key)
{
struct DataItem* temp = hashArray[hashIndex];
hashArray[hashIndex] = dummyItem;
return temp;
}
++hashIndex;
hashIndex %= SIZE;
}
return NULL;
}
void display() {
int i = 0;
for(i = 0; i<SIZE; i++)
{
if(hashArray[i] != NULL)
printf(" (%d,%d)",hashArray[i]->key,hashArray[i]->data);
else
printf(" ~~ ");
}
printf("\n");
}
int main() {
dummyItem = (struct DataItem*) malloc(sizeof(struct DataItem)); dummyItem->data = -1;
dummyItem->key = -1;
insert(1, 20);
insert(2, 70);
insert(42, 80);
insert(4, 25);
insert(12, 44);
insert(14, 32);
insert(17, 11);
insert(13, 78);
insert(37, 97);
display();
item = search(37);
if(item != NULL)
{
printf("Element found: %d\n", item->data);
}
else
{
printf("Element not found\n");
}
delete(item);
item = search(37);
if(item != NULL)
{
printf("Element found: %d\n", item->data);
}
else
{
printf("Element not found\n");
}
}
Output: