IS Jignasha

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 17

IS(3170720)

PRACTICAL – 1
AIM: Implement Caesar cipher encryption-decryption.
Code:
#include<stdio.h>
#include<ctype.h>
int main() {
char text[500], ch;
int key;
printf("Enter a message to encrypt: ");
scanf("%s", text);
printf("Enter the key: ");
scanf("%d", & key);
for (int i = 0; text[i] != '\0'; ++i) {
ch = text[i];
if (isalnum(ch)) {
if (islower(ch)) {
ch = (ch - 'a' + key) % 26 + 'a';
}
if (isupper(ch)) {
ch = (ch - 'A' + key) % 26 + 'A';
}
if (isdigit(ch)) {
ch = (ch - '0' + key) % 10 + '0';
}
}
else {
printf("Invalid Message");
}
text[i] = ch;
}
printf("Encrypted message: %s", text);
return 0;
}
Output:

VIEAT/B.E/SEM-VII/220943107040
1
IS(3170720)

PRACTICAL – 2
AIM: Implement Monoalphabetic cipher encryption-decryption.
Code:
#include<stdio.h>
char monocipher_encr(char);
char alpha[27][3] = { { 'a', 'f' }, { 'b', 'a' }, { 'c', 'g' }, { 'd', 'u' }, {
'e', 'n' }, { 'f', 'i' }, { 'g', 'j' }, { 'h', 'k' }, { 'i', 'l' }, {
'j', 'm' }, { 'k', 'o' }, { 'l', 'p' }, { 'm', 'q' }, { 'n', 'r' }, {
'o', 's' }, { 'p', 't' }, { 'q', 'v' }, { 'r', 'w' }, { 's', 'x' }, {
't', 'y' }, { 'u', 'z' }, { 'v', 'b' }, { 'w', 'c' }, { 'x', 'd' }, {
'y', 'e' }, { 'z', 'h' } };
char str[20];
int main() {
char str[20], str2[20];
int i;
printf("\n Enter String..");
gets(str);
for (i = 0; str[i]; i++) {
str2[i] = monocipher_encr(str[i]);
}
str2[i] = '\0';
printf("\n Before Decryption..%s", str);
printf("\n After Decryption..%s\n", str2);
}
char monocipher_encr(char a) {
int i;
for (i = 0; i < 27; i++) {
if (a == alpha[i][0])
break;
}
return alpha[i][1];
}
Output:

VIEAT/B.E/SEM-VII/220943107040
2
IS(3170720)

PRACTICAL – 3
AIM: Implement Playfair cipher encryption-decryption.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SIZE 30
void toLowerCase(char plain[], int ps)
{
int i;
for (i = 0; i < ps; i++) {
if (plain[i] > 64 && plain[i] < 91)
plain[i] += 32;
}
}
int removeSpaces(char* plain, int ps)
{
int i, count = 0;
for (i = 0; i < ps; i++)
if (plain[i] != ' ')
plain[count++] = plain[i];
plain[count] = '\0';
return count;
}
void generateKeyTable(char key[], int ks, char keyT[5][5])
{
int i, j, k, flag = 0, *dicty;
dicty = (int*)calloc(26, sizeof(int));
for (i = 0; i < ks; i++) {
if (key[i] != 'j')
dicty[key[i] - 97] = 2;
}
dicty['j' - 97] = 1;
i = 0;
j = 0;
for (k = 0; k < ks; k++) {
if (dicty[key[k] - 97] == 2) {
dicty[key[k] - 97] -= 1;
keyT[i][j] = key[k];
j++;
if (j == 5) {
i++;
j = 0;
}

VIEAT/B.E/SEM-VII/220943107040
3
IS(3170720)
}
}
for (k = 0; k < 26; k++) {
if (dicty[k] == 0) {
keyT[i][j] = (char)(k + 97);
j++;
if (j == 5) {
i++;
j = 0;
}
}
}
}
void search(char keyT[5][5], char a, char b, int arr[])
{
int i, j;
if (a == 'j')
a = 'i';
else if (b == 'j')
b = 'i';
for (i = 0; i < 5; i++) {
for (j = 0; j < 5; j++) {
if (keyT[i][j] == a) {
arr[0] = i;
arr[1] = j;
}
else if (keyT[i][j] == b) {
arr[2] = i;
arr[3] = j;
} } }}
int mod5(int a) { return (a % 5); }
int prepare(char str[], int ptrs)
{
if (ptrs % 2 != 0) {
str[ptrs++] = 'z';
str[ptrs] = '\0';
}
return ptrs;
}
void encrypt(char str[], char keyT[5][5], int ps)
{
int i, a[4];

for (i = 0; i < ps; i += 2) {


search(keyT, str[i], str[i + 1], a);
VIEAT/B.E/SEM-VII/220943107040
4
IS(3170720)
if (a[0] == a[2]) {
str[i] = keyT[a[0]][mod5(a[1] + 1)];
str[i + 1] = keyT[a[0]][mod5(a[3] + 1)];
}
else if (a[1] == a[3]) {
str[i] = keyT[mod5(a[0] + 1)][a[1]];
str[i + 1] = keyT[mod5(a[2] + 1)][a[1]];
}
else {
str[i] = keyT[a[0]][a[3]];
str[i + 1] = keyT[a[2]][a[1]];
} }}
void encryptByPlayfairCipher(char str[], char key[])
{
char ps, ks, keyT[5][5];
ks = strlen(key);
ks = removeSpaces(key, ks);
toLowerCase(key, ks);
ps = strlen(str);
toLowerCase(str, ps);
ps = removeSpaces(str, ps);
ps = prepare(str, ps);
generateKeyTable(key, ks, keyT);
encrypt(str, keyT, ps);
}
int main()
{
char str[SIZE], key[SIZE];
strcpy(key, "Information");
printf("Key text: %s\n", key);
strcpy(str, "Security");
printf("Plain text: %s\n", str);
encryptByPlayfairCipher(str, key);
printf("Cipher text: %s\n", str);
return 0;
}
Output:

VIEAT/B.E/SEM-VII/220943107040
5
IS(3170720)

PRACTICAL – 4
AIM: Implement Polyalphabetic cipher encryption-decryption.
Code:
#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
char msg[30],key[30],k[20],ct[20],pt[20];
int lenm,lenk,i,j;
clrscr();
printf("Enter Message : ");
gets(msg);
printf("Enter Key : ");
gets(key);
lenm=strlen(msg);
lenk=strlen(key);
for(i=0;i<lenm;i++,j++)
{
if(j==lenk)
{
j=0;
}
k[i]=key[j];
}
for(i=0;i<lenm;i++)
{
ct[i]=((msg[i]+k[i])%26)+'A';
}
ct[i]='\0';
for(i=0;i<lenm;i++)
{
pt[i]=(((ct[i]-k[i])+26)%26)+'A';
}
pt[i]='\0';
printf("\nEncrypted Message : %s", ct);
printf("\nDecrypted Message : %s", pt);
getch();}
Output:

VIEAT/B.E/SEM-VII/220943107040
6
IS(3170720)

PRACTICAL – 5
AIM: Implement Hill cipher encryption-decryption.
Code:
#include<stdio.h>
#include<string.h>
int main() {
unsigned int a[3][3] = { { 6, 24, 1 }, { 13, 16, 10 }, { 20, 17, 15 } };
unsigned int b[3][3] = { { 8, 5, 10 }, { 21, 8, 21 }, { 21, 12, 8 } };
int i, j;
unsigned int c[20], d[20];
char msg[20];
int determinant = 0, t = 0;
printf("Enter plain text\n ");
scanf("%s", msg);
for (i = 0; i < 3; i++) {
c[i] = msg[i] - 65;
printf("%d ", c[i]);
} for (i = 0; i < 3; i++) {
t = 0;
for (j = 0; j < 3; j++) {
t = t + (a[i][j] * c[j]);
}
d[i] = t % 26; }
printf("\nEncrypted Cipher Text :");
for (i = 0; i < 3; i++)
printf(" %c", d[i] + 65);
for (i = 0; i < 3; i++) {
t = 0;
for (j = 0; j < 3; j++) {
t = t + (b[i][j] * d[j]);
}
c[i] = t % 26; }
printf("\nDecrypted Cipher Text :");
for (i = 0; i < 3; i++)
printf(" %c", c[i] + 65);
return 0;
}
Output:

VIEAT/B.E/SEM-VII/220943107040
7
IS(3170720)

PRACTICAL – 6
AIM: To implement Simple DES or AES.
Code:
#include <stdio.h>
#include <stdint.h>
uint8_t S_BOX[16] = {
0x3, 0xF, 0xE, 0x0, 0x2, 0xD, 0x8, 0x4,
0xB, 0x1, 0x7, 0xA, 0xC, 0x6, 0x9, 0x5
};

uint8_t simple_DES(uint8_t input, uint8_t key) {

uint8_t temp = input ^ key;

return S_BOX[temp & 0x0F];


}

int main() {
uint8_t plaintext = 0x9; // Example input
uint8_t key = 0xF; // Example key

uint8_t ciphertext = simple_DES(plaintext, key);


printf("Ciphertext: %x\n", ciphertext);

return 0;
}
Output:

VIEAT/B.E/SEM-VII/220943107040
8
IS(3170720)

PRACTICAL – 7
AIM: Implement Diffi-Hellmen Key Exchange Method.
Code:
#include<stdio.h>
long int power(int a,int b,int mod)
{
long long int t;
if(b==1)
return a;
t=power(a,b/2,mod);
if(b%2==0)
return (t*t)%mod;
else
return (((t*t)%mod)*a)%mod;
}
long long int calculateKey(int a,int x,int n)
{
return power(a,x,n);
}
int main()
{
int n,g,x,a,y,b;
// both the persons will be agreed upon the common n and g
printf("Enter the value of n and g : ");
scanf("%d%d",&n,&g);
// first person will choose the x
printf("Enter the value of x for the first person : ");
scanf("%d",&x); a=power(g,x,n);
// second person will choose the y
printf("Enter the value of y for the second person : ");
scanf("%d",&y); b=power(g,y,n);
printf("key for the first person is : %lld\n",power(b,x,n));
printf("key for the second person is : %lld\n",power(a,y,n));
return 0;
}
Output:

VIEAT/B.E/SEM-VII/220943107040
9
IS(3170720)

PRACTICAL – 8
AIM: Implement RSA encryption-decryption algorithm.

Code :
#include <stdio.h>
#include <math.h>
int gcd(int a, int h) {
int temp;
while (1) {
temp = a % h;
if (temp == 0)
return h;
a = h;
h = temp;
}}
int main() {
double p = 3;
double q = 7;
double n = p * q;
double e = 2;
double phi = (p - 1) * (q - 1);

while (e < phi) {


if (gcd(e, phi) == 1)
break;
else
e++; }
int k = 2;
double d = (1 + (k * phi)) / e;
double msg = 12;
printf("Message data = %lf\n", msg);
double c = pow(msg, e);
c = fmod(c, n);
printf("Encrypted data = %lf\n", c);
double m = pow(c, d);
m = fmod(m, n);
printf("Original Message Sent = %lf\n", m);
return 0;}
Output:

VIEAT/B.E/SEM-VII/220943107040
10
IS(3170720)

PRACTICAL – 9
AIM: Write a program to generate SHA-1 hash.

Code :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <openssl/sha.h>
void computeSHA1(unsigned char *input, unsigned char *output) {
SHA_CTX ctx;
SHA1_Init(&ctx);
SHA1_Update(&ctx, input, strlen((char *)input));
SHA1_Final(output, &ctx);
}
void printHash(unsigned char *hash) {
for (int i = 0; i < SHA_DIGEST_LENGTH; i++) {
printf("%02x", hash[i]);
}
printf("\n");
}
int main() {
unsigned char input1[] = "Indian Government";
unsigned char input2[] = "hello world";
unsigned char hash[SHA_DIGEST_LENGTH];
printf("HashCode Generated by SHA-1 for:\n");
computeSHA1(input1, hash);
printf("%s : ", input1);
printHash(hash);
computeSHA1(input2, hash);
printf("%s : ", input2);
printHash(hash);
return 0;
}
Output:

VIEAT/B.E/SEM-VII/220943107040
11
IS(3170720)

PRACTICAL – 10
AIM: Implement a digital signature algorithm.

Code:
#include<stdio.h>
#include<conio.h>
#include<math.h>
long int ext_eucledian(long int m,long int b)
{
int a1=1,a2=0,a3=m,b1=0,b2=1,b3=b,q,t1,t2,t3;
while(1)
{
if(b3==0)
{
return 0;
}
if(b3==1)
{
if(b2<0)
b2+=m;
return b2;
}
q=a3/b3;
t1=a1-(q*b1);
t2=a2-(q*b2);
t3=a3-(q*b3);
a1=b1;
a2=b2;
a3=b3;
b1=t1;
b2=t2;
b3=t3;
}}
long int power(long int a, long int j, long int c)
{
int f,i;

VIEAT/B.E/SEM-VII/220943107040
12
IS(3170720)
f=1;
for(i=1;i<=j;i++)
{
f=(f*a)%c;
}
f=f%c;
return f;}
void main()
{
long int p,q,g,x,hm,k,y,r,s,s1,w,u1,u2,v,v1,v2,v3;
clrscr();
printf("enter the value of p:");
scanf("%ld",&p);
printf("enter the value of q:");
scanf("%ld",&q);
printf("enter the value of g:");
scanf("%ld",&g);
printf("enter the value of x:");
scanf("%ld",&x);
printf("enter the value of hm:");
scanf("%ld",&hm);
printf("enter the value of k:");
scanf("%ld",&k);
y=power(g,x,p);
printf("\nvalue of y:%ld",y);
r=power(g,k,p);
r=r%q;
printf("\nvalue of r:%ld",r);
s=ext_eucledian(q,k);
s1=(hm+(x*r));
s=(s*s1)%q;
printf("\nvalue of s:%ld",s);
w=ext_eucledian(q,s);
printf("\nsignature (r,s):%ld %ld",r,s);
printf("\nvalue of w:%ld",w);
u1=(hm*w)%q;

VIEAT/B.E/SEM-VII/220943107040
13
IS(3170720)
printf("\nvalue of u1:%ld",u1);
u2=(r*w)%q;
printf("\nvalue of u2:%ld",u2);
v=power(g,u1,p);
v1=power(y,u2,p);
v2=(v*v1)%p;
v3=v2%q;
printf("\nvalue of v:%ld",v3);
getch();
}

Output:

VIEAT/B.E/SEM-VII/220943107040
14
IS(3170720)

PRACTICAL – 11
Aim: Perform various encryption-decryption techniques with cryptool.

Cryptool:
CrypTool is an educational software tool designed to teach and demonstrate cryptographic algorithms
and techniques. It provides a hands-on approach to learning about cryptography, allowing users to
experiment with different cryptographic methods and understand how they work.
Encryption and Decryption of Caesar Ciphe
To start with the process you have to move to the Encrypt/Decrypt tab of the program. There, you will
find Symmetric (Classic) tab - Choose Caesar Cipher. For further information, you can get guided by
the image below.

In encryption, we are replacing the plaintext letter with the 3rd letter of the alphabet that is if “A” is
our plaintext character, then the Ciphertext will be “D”.

So, if I give “Monarchy” as plaintext in Caesar Cipher, it will show me the encryption, as shown in the
below image.

VIEAT/B.E/SEM-VII/220943107040
15
IS(3170720)

Encryption and Decryption of Playfair

Again, we have to move to Encrypt/Decrypt - Symmetric - Playfair Cipher and perform the encryption
part. We are putting the same plaintext – MONARCHY.

So, when we press the encrypt button, we will get the Ciphertext – “ONARMDYB”.

VIEAT/B.E/SEM-VII/220943107040
16
IS(3170720)

Encryption and Decryption of Hill Cipher

Again, we have to move to Encrypt/Decrypt - Symmetric - Hill Cipher and perform the encryption
part. We are putting the plaintext as – DRGREERROCKS and assuming that the program gives us the
Ciphertext as – FZIFTOTBXGPO.

So, when we press the encrypt button, we will get the Ciphertext – “FZIFTOTBXGPO”.

VIEAT/B.E/SEM-VII/220943107040
17