Lab 2
Lab 2
#include<ctype.h>
int main() {
int key;
scanf("%s", text);
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");
}
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() {
int key;
scanf("%s", text);
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;
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.
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.
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.
C = M ^ n( mod n )
3: Decrypting Message
M = C ^ d ( mod n )
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;
}
int main() {
long long int p, q, n, phi, e, d, plain_text, cipher_text, decrypted_text;
// 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);
// Encryption
printf("\nEnter the plain text: ");
scanf("%lld", &plain_text);
// Decryption
decrypted_text = power(cipher_text, d, n);
printf("Decrypted text: %lld\n", decrypted_text);
return 0;
}
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;
}