0% found this document useful (0 votes)
32 views11 pages

Crytography Lab (1) (1)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views11 pages

Crytography Lab (1) (1)

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

CRYPTOGRAPHY LAB

SEMESTER IV
[email protected]

PRACTICAL NO.2:-

A] Encryption in OTP Cipher:-

//Code

#include<stdio.h>

#include<string.h>

#include<ctype.h>

main()

int i,j,len1,len2,numstr[100],numkey[100],numcipher[100];

char str[100],key[100],cipher[100];

printf("Enter a string text to encrypt\n");

gets(str);

for(i=0,j=0;i<strlen(str);i++)

1
if(str[i]!=' ')

str[j]=toupper(str[i]);

j++;

str[j]='\0';

for(i=0;i<strlen(str);i++)

numstr[i]=str[i]-'A';

printf("Enter key string of random text\n");

gets(key);

for(i=0,j=0;i<strlen(key);i++)

if(key[i]!=' ')

key[j]=toupper(key[i]);

j++;

2
}

key[j]='\0';

for(i=0;i<strlen(key);i++)

numkey[i]=key[i]-'A';

for(i=0;i<strlen(str);i++)

numcipher[i]=numstr[i]+numkey[i];

for(i=0;i<strlen(str);i++)

if(numcipher[i]>25)

numcipher[i]=numcipher[i]-26;

} }

printf("One Time Pad Cipher text is\n");

for(i=0;i<strlen(str);i++)

3
{

printf("%c",(numcipher[i]+'A'));

printf("\n");

Output:-

B] Decryption of OTP Cipher:-

//Code:-

#include<stdio.h>

#include<string.h>

4
#include<ctype.h>

main()

//All the text which ever entered is converted to upper and without spaces

int i,j,len1,len2,numstr[100],numkey[100],numcipher[100];

char str[100],key[100],cipher[100];

printf("Enter an Encrypted string text to Decrypt\n");

gets(str);

for(i=0,j=0;i<strlen(str);i++)

if(str[i]!=' ')

str[j]=toupper(str[i]);

j++;

str[j]='\0';

//obtaining numerical plain text ex A-0,B-1,C-2

for(i=0;i<strlen(str);i++)

numstr[i]=str[i]-'A';

5
}

printf("Enter key string of random text\n");

gets(key);

for(i=0,j=0;i<strlen(key);i++)

if(key[i]!=' ')

key[j]=toupper(key[i]);

j++;

key[j]='\0';

//obtaining numerical one time pad(OTP) or key

for(i=0;i<strlen(key);i++)

numkey[i]=key[i]-'A';

for(i=0;i<strlen(str);i++)

numcipher[i]=numstr[i]-numkey[i];//changed from + to - for decryption

6
if(numcipher[i]<0)

numcipher[i]=numcipher[i]+26;//If cipher is negative we have to add 26

numcipher[i]=numcipher[i]%26;//To loop within 1 to 26 for alphabets from A-Z

printf("Decrypted One Time Pad Cipher text is\n");

for(i=0;i<strlen(str);i++)

printf("%c",(numcipher[i]+'A'));

printf("\n");

7
Output:-

C] Encrypting a word document:-

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

void encrypt(char* plaintext, char* key, char* ciphertext) {

int i, len = strlen(plaintext);

for (i = 0; i < len; i++) {

ciphertext[i] = ((plaintext[i] - 'A') ^ (key[i] - 'A')) + 'A';

ciphertext[i] = '\0';

8
}

int main() {

char key[100];

printf("Enter the key: ");

scanf("%s", key);

FILE *plaintext_file, *ciphertext_file;

plaintext_file = fopen("C:\\Users\\acer\\Desktop\\DAA_SAKET\\Plaintext.txt", "r");

ciphertext_file = fopen("C:\\Users\\acer\\Desktop\\DAA_SAKET\\Ciphertext.txt", "w");

if (plaintext_file == NULL || ciphertext_file == NULL) {

printf("Error opening file.");

return 1;

char plaintext[1000], ciphertext[1000];

int n = 0;

while (fgets(plaintext, sizeof(plaintext), plaintext_file) != NULL) {

9
encrypt(plaintext, key, ciphertext);

fprintf(ciphertext_file, "%s", ciphertext);

n += strlen(plaintext);

printf("Encrypted %d characters.\n", n);

fclose(plaintext_file);

fclose(ciphertext_file);

return 0;

10
11

You might also like