0% found this document useful (0 votes)
23 views

Lab 2

The document explains the Caesar cipher technique for encryption and decryption of messages. It provides the formulas and logic for encrypting messages by shifting each character by a key. Sample C code is given to encrypt and decrypt messages using this technique. The code takes the message and key as input and outputs the encrypted or decrypted message.

Uploaded by

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

Lab 2

The document explains the Caesar cipher technique for encryption and decryption of messages. It provides the formulas and logic for encrypting messages by shifting each character by a key. Sample C code is given to encrypt and decrypt messages using this technique. The code takes the message and key as input and outputs the encrypted or decrypted message.

Uploaded by

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

Write program to create cipher text

Caesar Cipher Program in C


Formula for Encryption and Decryption
Let us derive the encryption formula for Caesar Cipher Program in C. Firstly, for normal cases, if
the key is represented by k and the character by ch, we add both to get the encrypted message.
For extreme cases, we need to start counting from the beginning if the count exceeds z, so we
first try to find the shift. To do so, we subtract a, i.e., the initial character of the series of the
alphabet. Now we take modulus % with 26, i.e., the number of alphabets. Lastly, we add a to get
the final answer.
So, our formula becomes (ch - 'a' + key) % 26 + 'a'.
Similarly, instead of adding the key, we subtract it for decryption. Now, there is the possibility of
getting a negative answer here during subtraction. So, we add 26, and the formula becomes (ch -
'a' - key + 26) % 26 + 'a'.
In the same manner for capital letters and numbers, the formulae are:
Category Encryption Decryption
Small Letters (ch - 'a' + key) % 26 + 'a' (ch - 'a' - key + 26) % 26 + 'a'
Capital Letters (ch - 'A' + key) % 26 + 'A' (ch - 'A' - key + 26) % 26 + 'A'
Digits (ch - '0' + key) % 10 + '0' (ch - '0' - key + 10) % 10 + '0'
Let's see an example of a message consisting of only digits, encryption, and decryption. The
ASCII codes for all the digits are given below:
0 1 2 3 4 5 6 7 8 9
48 49 50 51 52 53 54 55 56 57
Let the message be 7930 and the key be 4.
Code ASCII value (x) x+key x-'0' (x-48) x%10 x+'0' Encoded Character
7 55 59 11 1 49 1
9 57 61 13 3 51 3
3 51 55 7 7 55 7
0 48 52 4 4 52 4
Similarly, decoding can be performed for 1374 to return the original message.
Code ASCII value (x) x-key x-'0' (x-48) x+10 x%10 x+'0' Decoded Character
1 49 45 -3 7 7 49 7
3 51 47 -1 9 9 51 9
7 55 51 3 13 3 55 3
4 52 48 0 10 0 52 0
Thus, using formulae, i.e., Ceaser Cipher's method, we can encode the message safely, send it,
and decode it by only authorized users.
Program for Caesar Cipher in C
Caesar Cipher Encryption (Explain, Syntax & Output)
The Caesar Cipher Program in C can be applied as given below. We will check each character
one by one to see if it is a lowercase character, an uppercase character, or a digit. We have an
above-derived formula for each of them which we can use to encrypt as per the key given by the
user.
Apply the above-derived formulae for each category. If the given character is an alphanumeric
value, only we try encoding it. Else we print an error message.
#include<stdio.h>

#include<ctype.h>

int main() {

char text[500], ch;

int key;

// Taking user input.


printf("Enter a message to encrypt: ");

scanf("%s", text);

printf("Enter the key: ");

scanf("%d", & key);

// Visiting character by 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 + 'a';
}
// Uppercase characters.
if (isupper(ch)) {
ch = (ch - 'A' + key) % 26 + 'A';
}

// Numbers.
if (isdigit(ch)) {
ch = (ch - '0' + key) % 10 + '0';
}
}
// Invalid character.
else {
printf("Invalid Message");
}

// Adding encoded answer.


text[i] = ch;

printf("Encrypted message: %s", text);

return 0;
}
Output:
Enter a message to encrypt: yZq8NS92mdR
Enter the key: 6
Encrypted message: eFw4TY58sjX
We first check if the character is alphanumeric using the isalnum() method, which returns true if
the given character is an alphanumeric value, i.e., either lowercase or uppercase alphabets or
numbers. Then we check if the character is lowercase, uppercase, or a number using islower(),
isupper(), and isdigit(), respectively. Accordingly, conversion is processed. Lastly, the encrypted
character is added to the text at its position.
Caesar Cipher Decryption (Explain, Syntax & Output)
Similarly, we can write Caesar Cipher Program in C for decoding. Instead of adding the key
subtract it and add 26 so that we don't get a negative value as our answer.
#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:
Enter a message to decrypt: eFw4TY58sjX
Enter the key: 6
Decrypted message: yZq8NS92mdR
The example above returns the value we inserted to encode in the above encoding C code in the
previous section.

Advantages of Caesar Cipher


Caesar Cipher Program in C has various advantages due to its simplicity as follows:
• It is a simple algorithm and very easy to implement.
• Only a small amount of computing resources are required.
• Single key is used for the entire message, which makes it less complex.
• It is very much suitable for non-complex syste
Example: C program to encrypt and decrypt the string using RSA algorithm.

RSA is another method for encrypting and decrypting the message. It involves public key and
private key, where the public key is known to all and is used to encrypt the message whereas
private key is only used to decrypt the encrypted message.

It has mainly 3 steps:

1: Creating Keys
Select two large prime numbers x and y
Compute n = x * y
where n is the modulus of private and the public key
Calculate totient function, ø (n) = (x − 1)(y − 1)
Choose an integer e such that e is coprime to ø(n) and 1 < e < ø(n).
e is the public key exponent used for encryption
Now choose d, so that d · e mod ø (n) = 1, i.e., >code>d is the multiplicative inverse of e in mod
ø (n)
2: Encrypting Message

Messages are encrypted using the Public key generated and is known to all.

The public key is the function of both e and n i.e. {e,n}.

If M is the message(plain text), then ciphertext

C = M ^ n( mod n )
3: Decrypting Message

The private key is the function of both d and n i.e {d,n}.

If C is the encrypted ciphertext, then the plain decrypted text M is

M = C ^ d ( mod n )

RSA Algorithm Example

 Choose p = 3 and q = 11
 Compute n = p * q = 3 * 11 = 33
 Compute φ(n) = (p - 1) * (q - 1) = 2 * 10 = 20
 Choose e such that 1 < e < φ(n) and e and φ (n) are coprime. Let e = 7
 Compute a value for d such that (d * e) % φ(n) = 1. One solution is d = 3 [(3 * 7) % 20 =
1]
 Public key is (e, n) => (7, 33)
 Private key is (d, n) => (3, 33)
 The encryption of m = 2 is c = 27 % 33 = 29
 The decryption of c = 29 is m = 293 % 33 = 2
 // Function to calculate (a^b) % c
 int power_mod(int a, int b, int c) {
 int result = 1;

 // Reduce 'a' modulo 'c' to handle large 'a'
 a %= c;

 while (b > 0) {
 // If 'b' is odd, multiply 'result' with 'a' and take modulo 'c'
 if (b % 2 == 1)
 result = (result * a) % c;

 // Divide 'b' by 2
 b /= 2;

 // Update 'a' to (a^2) % c
 a = (a * a) % c;
 }

 return result;
 }

 int main() {
 int a = 7, b = 3, c = 5;
 printf("(%d^%d) %% %d = %d\n", a, b, c, power_mod(a, b, c)); //
Output: (7^3) % 5 = 2
 return 0;
 }

Here is an implementation of RSA in C program.


#include <stdio.h>
#include <stdlib.h>
#include <math.h>

// Function to calculate (a^b) % c


long long int power(long long int a, long long int b, long long int c) {
long long int result = 1;
a = a % c;
while (b > 0) {
if (b & 1)
result = (result * a) % c;
b = b >> 1;
a = (a * a) % c;
}
return result;
}

// Function to calculate gcd (greatest common divisor) of two numbers


long long int gcd(long long int a, long long int b) {
if (b == 0)
return a;
return gcd(b, a % b);
}

// Function to generate a random prime number


long long int generatePrime() {
long long int num, i;
num = rand() % 1000 + 100;
for (i = 2; i <= sqrt(num); i++) {
if (num % i == 0) {
num = generatePrime();
break;
}
}
return num;
}

int main() {
long long int p, q, n, phi, e, d, plain_text, cipher_text, decrypted_text;

// Step 1: Choose two prime numbers


p = generatePrime();
q = generatePrime();

// Step 2: Calculate n and phi


n = p * q;
phi = (p - 1) * (q - 1);

// Step 3: Choose e such that 1 < e < phi and gcd(e, phi) = 1
do {
e = rand() % (phi - 2) + 2;
} while (gcd(e, phi) != 1);

// Step 4: Calculate d such that (d * e) % phi = 1


d = 1;
while ((d * e) % phi != 1) {
d++;
}

printf("Public key (e, n): (%lld, %lld)\n", e, n);


printf("Private key (d, n): (%lld, %lld)\n", d, n);

// Encryption
printf("\nEnter the plain text: ");
scanf("%lld", &plain_text);

cipher_text = power(plain_text, e, n);


printf("Encrypted text: %lld\n", cipher_text);

// Decryption
decrypted_text = power(cipher_text, d, n);
printf("Decrypted text: %lld\n", decrypted_text);

return 0;
}

Write program to validate strong


Password
#include <stdio.h>
#include <string.h>

int main() {
char password[100];
printf("Enter password: ");
fgets(password, sizeof(password), stdin);
password[strcspn(password, "\n")] = '\0';
if (strlen(password) < 8) {
printf("Password is too short.\n");
}
else {
printf("Password is strong.\n");
}
return 0;
}

You might also like