The document outlines the implementation of various encryption algorithms including Caesar Cipher, Playfair Cipher, DES, and RSA using C language. Each section provides an aim, description, algorithm steps, and code snippets for the respective cipher techniques. The document serves as a comprehensive guide for understanding and coding these encryption methods.
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 ratings0% found this document useful (0 votes)
18 views8 pages
Is Lab-1 PDF
The document outlines the implementation of various encryption algorithms including Caesar Cipher, Playfair Cipher, DES, and RSA using C language. Each section provides an aim, description, algorithm steps, and code snippets for the respective cipher techniques. The document serves as a comprehensive guide for understanding and coding these encryption methods.
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/ 8
1A.
IMPLEMENTATION OF CAESAR CIPHER
AIM: To implement the simple substitution technique named Caesar cipher using C language. DESCRIPTION: To encrypt a message with a Caesar cipher, each letter in the message is changed using a simple rule: shift by three. Each letter is replaced by the letter three letters ahead in the alphabet. A becomes D, B becomes E, and so on. For the last letters, we can think of the alphabet as a circle and "wrap around". W becomes Z, X becomes A, Y becomes B, and Z becomes C. To change a message back, each letter is replaced by the one three before it. ALGORITHM: STEP-1: Read the plain text from the user. STEP-2: Read the key value from the user. STEP-3: If the key is positive then encrypt the text by adding the key with each character in the plain text. STEP-4: Else subtract the key from the plain text. STEP-5: Display the cipher text obtained above. CODE #include <stdio.h> #include <string.h> #include <ctype.h> int main() { char plain[100], cipher[100]; int key, i, length; printf("Enter the plain text: "); scanf("%s", plain); printf("Enter the key value: "); scanf("%d", &key); length = strlen(plain); printf("\nEncrypted Text: "); for (i = 0; i < length; i++) { if (isalpha(plain[i])) { if (isupper(plain[i])) cipher[i] = (plain[i] - 'A' + key) % 26 + 'A'; else cipher[i] = (plain[i] - 'a' + key) % 26 + 'a'; } else { cipher[i] = plain[i]; } printf("%c", cipher[i]); } printf("\nDecrypted Text: "); for (i = 0; i < length; i++) { if (isalpha(cipher[i])) { if (isupper(cipher[i])) plain[i] = (cipher[i] - 'A' - key + 26) % 26 + 'A'; else plain[i] = (cipher[i] - 'a' - key + 26) % 26 + 'a'; } else { plain[i] = cipher[i]; } printf("%c", plain[i]); } } 1B. IMPLEMENTATION OF PLAYFAIR CIPHER AIM To write a C program to implement the Playfair Substitution technique. DESCRIPTION: The Playfair cipher starts with creating a key table. The key table is a 5×5 grid of letters that will act as the key for encrypting your plaintext. Each of the 25 letters must be unique and one letter of the alphabet is omitted from the table (as there are 25 spots and 26 letters in the alphabet). To encrypt a message, one would break the message into digrams (groups of 2 letters) such that, for example, "HelloWorld" becomes "HE LL OW OR LD", and map them out on the key table. The two letters of the diagram are considered as the opposite corners of a rectangle in the key table. Note the relative position of the corners of this rectangle. Then apply the following 4 rules, in order, to each pair of letters in the plaintext: 1. If both letters are the same (or only one letter is left), add an "X" after the first letter 2. If the letters appear on the same row of your table, replace them with the letters to their immediate right respectively 3. If the letters appear on the same column of your table, replace them with the letters immediately below respectively 4. If the letters are not on the same row or column, replace them with the letters on the same row respectively but at the other pair of corners of the rectangle defined by the original pair. ALGORITHM: STEP-1: Read the plain text from the user. STEP-2: Read the keyword from the user. STEP-3: Arrange the keyword without duplicates in a 5*5 matrix in the row order and fill the remaining cells with missed out letters in alphabetical order. Note that ‘i’ and ‘j’ takes the same cell. STEP-4: Group the plain text in pairs and match the corresponding corner letters by forming a rectangular grid. STEP-5: Display the obtained cipher text. CODE #include <stdio.h> #include <string.h> #include <ctype.h> #define SIZE 5 void constructKeyMatrix(char key[SIZE][SIZE], char *keystr) { int i, j, k = 0, m = 0; char alphabet[26] = "ABCDEFGHIKLMNOPQRSTUVWXYZ"; int used[26] = {0}; for (i = 0; keystr[i] != '\0'; i++) { if (keystr[i] == 'J') keystr[i] = 'I'; if (!used[keystr[i] - 'A']) { used[keystr[i] - 'A'] = 1; key[k / SIZE][k % SIZE] = keystr[i]; k++; } } for (i = 0; i < 26; i++) { if (!used[i]) { key[k / SIZE][k % SIZE] = alphabet[i]; k++; } } } void search(char key[SIZE][SIZE], char ch, int *row, int *col) { for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { if (key[i][j] == ch) { *row = i; *col = j; return; } } } } void encryptPair(char key[SIZE][SIZE], char ch1, char ch2) { int row1, col1, row2, col2; search(key, ch1, &row1, &col1); search(key, ch2, &row2, &col2); if (row1 == row2) { printf("%c%c", key[row1][(col1 + 1) % SIZE], key[row2][(col2 + 1) % SIZE]); } else if (col1 == col2) { printf("%c%c", key[(row1 + 1) % SIZE][col1], key[(row2 + 1) % SIZE][col2]); } else { printf("%c%c", key[row1][col2], key[row2][col1]); } } void encryptText(char key[SIZE][SIZE], char *plaintext) { char preparedText[100]; int len = 0, i; for (i = 0; plaintext[i] != '\0'; i++) { if (plaintext[i] == 'J') plaintext[i] = 'I'; if (isalpha(plaintext[i])) { preparedText[len++] = toupper(plaintext[i]); if (len > 1 && preparedText[len - 1] == preparedText[len - 2]) { preparedText[len - 1] = 'X'; preparedText[len++] = toupper(plaintext[i]); } } } if (len % 2 != 0) preparedText[len++] = 'X'; preparedText[len] = '\0'; printf("Cipher Text: "); for (i = 0; i < len; i += 2) { encryptPair(key, preparedText[i], preparedText[i + 1]); } printf("\n"); } int main() { char keystr[50], plaintext[100], key[SIZE][SIZE]; printf("Enter the key: "); gets(keystr); printf("Enter the plaintext: "); gets(plaintext); constructKeyMatrix(key, keystr); printf("\nKey Matrix:\n"); for (int i = 0; i < SIZE; i++) { for (int j = 0; j < SIZE; j++) { printf("%c ", key[i][j]); } printf("\n"); } encryptText(key, plaintext); return 0; }
2A. IMPLEMENTATION OF DES
AIM: To write a C program to implement Data Encryption Standard (DES) using C Language. DESCRIPTION: DES is a symmetric encryption system that uses 64-bit blocks, 8 bits of which are used for parity checks. The key therefore has a "useful" length of 56 bits, which means that only 56 bits are actually used in the algorithm. The algorithm involves carrying out combinations, substitutions and permutations between the text to be encrypted and the key, while making sure the operations can be performed in both directions. The key is ciphered on 64 bits and made of 16 blocks of 4 bits, generally denoted k1 to k16. Given that "only" 56 bits are actually used for encrypting, there can be 256 different keys. The main parts of the algorithm are as follows: ➢ Fractioning of the text into 64-bit blocks ➢ Initial permutation of blocks ➢ Breakdown of the blocks into two parts: left and right, named L and R ➢ Permutation and substitution steps repeated 16 times ➢ Re-joining of the left and right parts then inverse initial permutation ALGORITHM: STEP-1: Read the 64-bit plain text. STEP-2: Split it into two 32-bit blocks and store it in two different arrays. STEP-3: Perform XOR operation between these two arrays. STEP-4: The output obtained is stored as the second 32-bit sequence and the original second 32-bit sequence forms the first part. STEP-5: Thus the encrypted 64-bit cipher text is obtained in this way. Repeat the same process for the remaining plain text characters. CODE import javax.crypto.Cipher; import javax.crypto.KeyGenerator; import javax.crypto.SecretKey; import javax.crypto.spec.SecretKeySpec; import javax.swing.*; import java.security.SecureRandom; import java.util.Random; public class DES { private byte[] rawKey; private String symmetricKey; public DES() { try { generateSymmetricKey(); String inputMessage = JOptionPane.showInputDialog("Enter message to encrypt"); byte[] encryptedBytes = encrypt(rawKey, inputMessage.getBytes()); String encryptedMessage = new String(encryptedBytes); JOptionPane.showMessageDialog(null, "Encrypted Data:\n" + encryptedMessage); System.out.println("Encrypted message: " + encryptedMessage); byte[] decryptedBytes = decrypt(rawKey, encryptedBytes); String decryptedMessage = new String(decryptedBytes); JOptionPane.showMessageDialog(null, "Decrypted Data:\n" + decryptedMessage); System.out.println("Decrypted message: " + decryptedMessage); } catch (Exception e) { e.printStackTrace(); } } private void generateSymmetricKey() { try { Random random = new Random(); int num = random.nextInt(10000); byte[] seed = String.valueOf(num).getBytes();
new DES(); } } 2B. IMPLEMENTATION OF RSA AIM: To write a C program to implement the RSA encryption algorithm. DESCRIPTION: RSA is an algorithm used by modern computers to encrypt and decrypt messages. It is an asymmetric cryptographic algorithm. Asymmetric means that there are two different keys. This is also called public key cryptography, because one of them can be given to everyone. A basic principle behind RSA is the observation that it is practical to find three very large positive integers e, d and n such that with modular exponentiation for all integer m: (m^e)^d = m (mod n) The public key is represented by the integers n and e; and, the private key, by the integer d. m represents the message. RSA involves a public key and a private key. The public key can be known by everyone and is used for encrypting messages. The intention is that messages encrypted with the public key can only be decrypted in a reasonable amount of time using the private key. ALGORITHM: STEP-1: Select two co-prime numbers as p and q. STEP-2: Compute n as the product of p and q. STEP-3: Compute (p-1)*(q-1) and store it in z. STEP-4: Select a random prime number e that is less than that of z. STEP-5: Compute the private key, d as e * mod^-1(z). STEP-6: The cipher text is computed as message * mod n. STEP-7: Decryption is done as cipherdmod n. CODE #include <stdio.h> #include <stdlib.h> #include <math.h> #include <string.h> long int p, q, n, t, e, d; char message[100]; long int encrypted[100], decrypted[100]; int is_prime(long int); void generate_keys(); void encrypt(); void decrypt(); int main() { printf("Enter first prime number (p): "); scanf("%ld", &p); if (!is_prime(p)) { printf("Invalid input. %ld is not a prime number.\n", p); return 1; } printf("Enter another prime number (q): "); scanf("%ld", &q); if (!is_prime(q) || p == q) { printf("Invalid input. %ld is not a valid choice.\n", q); return 1; } printf("Enter message to encrypt: "); scanf("%s", message); n = p * q; t = (p - 1) * (q - 1); generate_keys(); printf("Public Key (e, n): (%ld, %ld)\n", e, n); printf("Private Key (d, n): (%ld, %ld)\n", d, n); encrypt(); printf("Encrypted message: "); for (int i = 0; encrypted[i] != -1; i++) printf("%c", (char)encrypted[i]); printf("\n"); decrypt(); printf("Decrypted message: "); for (int i = 0; decrypted[i] != -1; i++) printf("%c", (char)decrypted[i]); printf("\n"); return 0; } int is_prime(long int num) { if (num <= 1) return 0; for (long int i = 2; i <= sqrt(num); i++) { if (num % i == 0) return 0; } return 1; } void generate_keys() { for (e = 2; e < t; e++) { if (__gcd(e, t) == 1) break; } long int k = 1; while (1) { if ((1 + k * t) % e == 0) { d = (1 + k * t) / e; break; } k++; } } void encrypt() { int i = 0; while (message[i] != '\0') { long int pt = message[i]; long int ct = 1; for (int j = 0; j < e; j++) { ct = (ct * pt) % n; } encrypted[i] = ct; i++; } encrypted[i] = -1; } void decrypt() { int i = 0; while (encrypted[i] != -1) { long int ct = encrypted[i]; long int pt = 1; for (int j = 0; j < d; j++) { pt = (pt * ct) % n; } decrypted[i] = pt; i++; } decrypted[i] = -1; }