In this chapter we will deal with application programs related to cryptography. In love, war and business, we need to send the messages secretly. The art and science of keeping the messages secure is called cryptography. A message to be dispatched is also called as plaintext or cleartext. Encryption is the process of converting the plaintext into a scrambled, unreadable message. This message is called ciphertext. The process of converting the scrambled message back into plaintext is called decryption.

The various methods used for encryption/decryption of messages are as follows:

  • Reverse cipher . In reverse cipher, the text is simply reversed in order to encrypt it. Unless the text is palindrome, the encrypted text is Greek to anyone. For example, the word “computer” is encrypted as “retupmoc” in reverse cipher. During decryption, ciphertext is reversed to retrieve plaintext.

  • Caesar cipher . Caesar cipher was invented by Julius Caesar hence the name. If key = 2, then letters A, B, C,…, X, Y, Z in plaintext are replaced by the letters C, D, E,…, Z, A, B, respectively, to obtain the ciphertext. During decryption, the letters C, D, E,…, Z, A, B in ciphertext are replaced by the letters A, B, C,…, X, Y, Z, respectively, to retrieve the plaintext. In program, letters are first conveted to their ASCII code and then key is added (during encryption) and subtracted (during decryption) from the ASCII codes.

  • Transposition cipher . In transposition cipher, firstly, plaintext is written in a 2-dimensional array, then dimensions of this 2-dimensional array are interchanged (ie, rows become columns and columns become rows), and then text is read from this modified 2-dimensional array which is nothing but ciphertext. See Figure 10-1.

    Figure 10-1.
    figure 1

    Cryptography using Transposition cipher. Here, key is 7, hence number of columns are 7.

  • Multiplicative cipher . Multiplicative cipher is analogous to Caesar cipher. However, instead of addition/subtraction, multiplication/division is performed. During encryption, ASCII codes of letters in plaintext are multiplied by key to obtain ciphertext. During decryption, ASCII codes of letters in ciphertext are multiplied by an inverse function of the key to retrieve the plaintext.

  • Affine cipher . Affine cipher is combination of Caesar cipher and Multiplicative cipher.

  • Simple Substitution cipher . In Simple Substitution cipher, every letter in alphabet is randomly replaced by another letter to obtain the key. Thus key in this cipher is nothing but the string of 26 letters (alphabets) in random order. Using this key, encryption and decryption is performed.

  • Vigenère cipher . Vigenère cipher is nothing but the Caesar cipher with multiple keys. As it uses multiple keys, it is called as “polyalphabetic substitution cipher.” Let the word CAT be the Vigenere cipher text key (see Figure 10-2). Here, letter C means key is 2, letter A means key is 0, and letter T means key is 19. During encryption and decryption these keys are used in cyclical order. During encryption, first letter in plaintext is encrypted using the key = 2, second letter in plaintext is encrypted using the key = 0, third letter in plaintext is encrypted using the key = 19, fourth letter in plaintext is encrypted using the key = 2, and so on. During decryption also these keys are used in the same order.

    Figure 10-2.
    figure 2

    Letters A to Z are serially numbered as 0 to 25 as shown here

  • One-Time Pad cipher . This cipher is impossible to crack but also inconvenient to use. It is nothing but a Vigenère cipher with the following additional features: (a) key is exactly as long as the plaintext message, (b) key is made up of randomly selected characters, and (c) the key once used is thrown away and never used again. Like many consumable items, this key also believes in the policy of “use and throw.”

  • RSA cipher . RSA cipher is named after its inventors Ron Rivest, Adi Shamir, and Leonard Adleman. This cipher uses two types of keys, namely, Public key and Private key. Public key is used for encryption of plaintext. Private key is used for decryption of ciphertext. This cipher derives its strenght from the fact that if two large prime numbers are multiplied then the resulting number is difficult to factorize.

10-1. Use the Reverse Cipher Method

Problem

You want to implement a cryptographic system using the Reverse cipher method.

Merits:

  • Easy to implement

  • Quick execution of program

  • Needs less memory

Demerits:

  • Not very difficult to decipher

  • Cannot be used in high-level applications

Solution

Write a C program that implements a cryptographic system using the Reverse cipher method, with the following specifications:

  • Program defines the functions: (a) menu() to display menu for users on the screen, (b) encryptMsg() to encrypt the plaintext, and (c) decryptMsg() to decrypt the ciphertext.

  • Function encryptMsg() simply reverses the plaintext in order to encrypt it. Function decryptMsg() simply reverses the encrypted text in order to restore the plaintext.

  • Program also defines the char type arrays msgOriginal, msgEncrypt, and msgDecrypt to store the messages. Array should accommodate 100 characters .

The Code

Code of C program written with these specifications is given below. Type the following C program in a text editor and save it in the folder C:\Code with the filename crypt1.c:

/* This program implements cryptographic system using the Reverse cipher method. */                                                                    /* BL */ #include <stdio.h>                                                 /* L1 */ #include <string.h>                                                /* L2 */                                                                    /* BL */ char msgOriginal[100];                                             /* L3 */ char msgEncrypt[100];                                              /* L4 */ char msgDecrypt[100];                                              /* L5 */ int intChoice, length;                                             /* L6 */                                                                    /* BL */ void menu()                                                        /* L7 */ {                                                                  /* L8 */   printf("\nEnter 1 to Encrypt a Message.");                       /* L9 */   printf("\nEnter 2 to Decrypt an Encrypted Message.");            /* L10 */   printf("\nEnter 3 to Stop the Execution of Program.");           /* L11 */   printf("\nNow Enter Your Choice (1, 2 or 3) and Strike Enter Key: ");  /* L12 */   scanf("%d", &intChoice);                                         /* L13 */ }                                                                  /* L14 */                                                                    /* BL  */ void encryptMsg()                                                  /* L15 */ {                                                                  /* L16 */   int i, j;                                                        /* L17 */   fflush(stdin);                                                   /* L18 */   printf("Enter the Message to be Encrypted (upto 100 characters): \n");  /* L19 */   gets(msgOriginal);                                               /* L20 */   length = strlen(msgOriginal);                                    /* L21 */   j = length - 1;                                                  /* L22 */   for (i = 0; i < length; i++) {                                   /* L23 */     msgEncrypt[j] = msgOriginal[i] ;                               /* L24 */     j--;                                                           /* L25 */   }                                                                /* L26 */   msgEncrypt[length] = '\0';                                       /* L27 */   printf("\nEncrypted Message: %s", msgEncrypt);                   /* L28 */ }                                                                  /* L29 */                                                                    /* BL  */ void decryptMsg()                                                  /* L30 */ {                                                                  /* L31 */   int i, j;                                                        /* L32 */   fflush(stdin);                                                   /* L33 */   printf("Enter the Message to be Decrypted (upto 100 characters): \n"); /* L34 */   gets(msgEncrypt);                                                /* L35 */   length = strlen(msgEncrypt);                                     /* L36 */   j = length - 1;                                                  /* L37 */   for (i = 0; i < length; i++) {                                   /* L38 */     msgDecrypt[j] = msgEncrypt[i] ;                                /* L39 */     j--;                                                           /* L40 */   }                                                                /* L41 */   msgDecrypt[length] = '\0';                                       /* L42 */   printf("\nDecrypted Message: %s", msgDecrypt);                   /* L43 */ }                                                                  /* L44 */                                                                    /* BL  */ void main()                                                        /* L45 */ {                                                                  /* L46 */   do {                                                             /* L47 */     menu();                                                        /* L48 */     switch (intChoice) {                                           /* L49 */       case 1:                                                      /* L50 */                encryptMsg();                                       /* L51 */                break;                                              /* L52 */       case 2:                                                      /* L53 */                decryptMsg();                                       /* L54 */                break;                                              /* L55 */       default:                                                     /* L56 */                printf("\nThank you.\n");                           /* L57 */                exit(0);                                            /* L58 */     }                                                              /* L59 */   } while (1);                                                     /* L60 */ }                                                                  /* L61 */

Compile and execute this program. A run of this program is given below :

Enter 1 to Encrypt a Message. Enter 2 to Decrypt an Encrypted Message. Enter 3 to Stop the Execution of Program. Now Enter Your Choice (1, 2, or 3) and Strike Enter Key: 1    Enter the Message to be Encrypted (upto 100 characters): C and Cryptography is very powerful combination.    Encrypted Message: .noitanibmoc lufrewop yrev si yhpargotpyrC dna C Enter 1 to Encrypt a Message. Enter 2 to Decrypt an Encrypted Message. Enter 3 to Stop the Execution of Program. Now Enter Your Choice (1, 2, or 3) and Strike Enter Key: 2    Enter the Message to be Decrypted (upto 100 characters): .noitanibmoc lufrewop yrev si yhpargotpyrC dna C    Decrypted Message: C and Cryptography is very powerful combination. Enter 1 to Encrypt a Message. Enter 2 to Decrypt an Encrypted Message . Enter 3 to Stop the Execution of Program. Now Enter Your Choice (1, 2, or 3) and Strike Enter Key: 3    Thank you.

How It Works

Reverse cipher is a simple method to cipher the plaintext. In this method, the plaintext to be encrypted is simply reversed. For example, if plaintext is “computer” then encrypted text according to Reverse cipher is “retupmoc.” This method can be implemented using recursion. However, in this program, recursion is avoided to keep the logic simple.

In the LOCs 3-5, three char type arrays are declared namely, msgOriginal, msgEncrypt, and msgDecrypt. LOCs 7-14 consist of definition of function menu(). This function displays menu for users on the screen so that various options in this program can be used conveniently. LOCs 9-12 ask the user to enter an appropriate choice and also describe the various options available to user. LOC 13 reads the choice entered by user and stores this choice in the int variable intChoice.

LOCs 15-29 consist of definition of the function encryptMsg(). This function simply reverses the plaintext and stores the encrypted text in the array msgEncrypt. LOC 19 asks the user to enter the plaintext. The plaintext entered by user is stored in the variable msgOriginal. LOCs 23-26 consist of a for loop which reverses the plaintext stored in the msgOriginal and processed text is stored in the variable msgEncrypt.

LOCs 30-44 consist of definition of the function decryptMsg(). This function again reverses the ciphertext stored in the array msgEncrypt and stores the processed text in the array msgDecrypt.

LOCs 45-61 consist of definition of the function main(). LOCs 47-61 consist of a do-while loop. This seems to be an infinite loop, however, function exit() in the LOC 58 effectively stops the execution of this loop. LOC 48 calls the function menu() which displays the menu for users on the screen. The choice entered by user is stored in the int variable intChoice. LOCs 49-59 consist of switch statement. Value stored in intChoice is passsed to this statement. If value of intChoice is 1 then function encryptMsg() is called. If value of intChoice is 2 then function decryptMsg() is called. If value of intChoice is else then function exit() is called which terminates the execution of do-while loop and also terminates the execution of this program .

10-2. Use the Caesar Cipher Method

Problem

You want to implement a cryptographic system using the Caesar cipher method .

Merits:

  • Based on simple logic

  • Historically important

  • Can be modified to make the deciphering of ciphertext difficult

  • Economical to implement

Demerits :

  • Ciphertext can be deciphered using brute-force techniques

  • Cannot be used in high-level applications without modifications

  • Transportation of key securely is difficult

Solution

Write a C program that implements a cryptographic system using the Caesar cipher method, with the following specifications:

  • Program defines the functions: (a) menu() to display menu for users on the screen, (b) encryptMsg() to encrypt the plaintext, and (c) decryptMsg() to decrypt the ciphertext.

  • Assume the suitable value for KEY. Function encryptMsg() encrypts the plaintext simply by adding the KEY to ASCII values of letters. Function decryptMsg() decrypts the ciphertext simply by subtracting the KEY from the ASCII values of letters.

  • Program also defines the char type arrays msgOriginal, msgEncrypt, and msgDecrypt to store the messages. Array should accommodate 100 characters.

The Code

Code of C program written with these specifications is given below. Type the following C program in a text editor and save it in the folder C:\Code with the filename crypt2.c:

/* This program implements a cryptographic system using Caesar cipher method. */                                                                    /* BL */ #include <stdio.h>                                                 /* L1 */ #include <string.h>                                                /* L2 */                                                                    /* BL */ #define  KEY  5                                                    /* L3 */                                                                    /* BL */ char msgOriginal[100];                                             /* L4 */ char msgEncrypt[100];                                              /* L5 */ char msgDecrypt[100];                                              /* L6 */ int intChoice, length;                                             /* L7 */                                                                    /* BL */ void menu()                                                        /* L8 */ {                                                                  /* L9 */   printf("\nEnter 1 to Encrypt a Message.");                       /* L10 */   printf("\nEnter 2 to Decrypt an Encrypted Message.");            /* L11 */   printf("\nEnter 3 to Stop the Execution of Program.");           /* L12 */   printf("\nNow Enter Your Choice (1, 2 or 3) and Strike Enter Key: ");  /* L13 */   scanf("%d", &intChoice);                                         /* L14 */ }                                                                  /* L15 */                                                                    /* BL  */ void encryptMsg()                                                  /* L16 */ {                                                                  /* L17 */   int i, ch;                                                       /* L18 */   fflush(stdin);                                                   /* L19 */   printf("Enter the Message to Encrypt, Do Not Include Spaces and \n");  /* L20 */   printf("Punctuation Symbols (upto 100 alphabets): \n");          /* L21 */   gets(msgOriginal);                                               /* L22 */   length = strlen(msgOriginal);                                    /* L23 */     for(i = 0; i < length; i++) {                                  /* L24 */       ch = msgOriginal[i];                                         /* L25 */       if(ch >= 'a' && ch <= 'z') {                                 /* L26 */         ch = ch + KEY;                                             /* L27 */         if(ch > 'z')                                               /* L28 */           ch = ch - 'z' + 'a' - 1;                                 /* L29 */          msgEncrypt[i] = ch;                                       /* L30 */       }                                                            /* L31 */       else if(ch >= 'A' && ch <= 'Z'){                             /* L32 */         ch = ch + KEY;                                             /* L33 */         if(ch > 'Z')                                               /* L34 */           ch = ch - 'Z' + 'A' - 1;                                 /* L35 */         msgEncrypt[i] = ch;                                        /* L36 */       }                                                            /* L37 */     }                                                              /* L38 */   msgEncrypt[length] = '\0';                                       /* L39 */   printf("\nEncrypted Message: %s", msgEncrypt);                   /* L40 */ }                                                                  /* L41 */                                                                    /* BL  */ void decryptMsg()                                                  /* L42 */ {                                                                  /* L43 */   int i, ch;                                                       /* L44 */   fflush(stdin);                                                   /* L45 */   printf("Enter the Message to Decrypt (upto 100 alphabets):\n");  /* L46 */   gets(msgEncrypt);                                                /* L47 */   length = strlen(msgEncrypt);                                     /* L48 */   for(i = 0; i < length; i++) {                                    /* L49 */     ch = msgEncrypt[i];                                            /* L50 */     if(ch >= 'a' && ch <= 'z') {                                   /* L51 */       ch = ch - KEY;                                               /* L52 */       if(ch < 'a')                                                 /* L53 */         ch = ch + 'z' - 'a' + 1;                                   /* L54 */       msgDecrypt[i] = ch;                                          /* L55 */     }                                                              /* L56 */     else if(ch >= 'A' && ch <= 'Z'){                               /* L57 */       ch = ch - KEY;                                               /* L58 */       if(ch < 'A')                                                 /* L59 */         ch = ch + 'Z' - 'A' + 1;                                   /* L60 */       msgDecrypt[i] = ch;                                          /* L61 */     }                                                              /* L62 */   }                                                                /* L63 */   msgDecrypt[length] = '\0';                                       /* L64 */   printf("\nDecrypted Message: %s", msgDecrypt);                   /* L65 */ }                                                                  /* L66 */                                                                    /* BL  */ void main()                                                        /* L67 */ {                                                                  /* L68 */   do {                                                             /* L69 */     menu();                                                        /* L70 */     switch (intChoice) {                                           /* L71 */       case 1:                                                      /* L72 */                encryptMsg();                                       /* L73 */                break;                                              /* L74 */       case 2:                                                      /* L75 */                decryptMsg();                                       /* L76 */                break;                                              /* L77 */       default:                                                     /* L78 */                printf("\nThank you.\n");                           /* L79 */                exit(0);                                            /* L80 */     }                                                              /* L81 */   } while (1);                                                     /* L82 */ }                                                                  /* L83 */

Compile and execute this program. A run of this program is given below:

Enter 1 to Encrypt a Message. Enter 2 to Decrypt an Encrypted Message. Enter 3 to Stop the Execution of Program. Now Enter Your Choice (1, 2, or 3) and Strike Enter Key: 1    Enter the Message to Encrypt, Do Not Include Spaces and Punctuation Symbols (upto 100 alphabets): CandCryptographyIsVeryPowerfulCombination    Encrypted Message: HfsiHwduytlwfumdNxAjwdUtbjwkzqHtrgnfynts Enter 1 to Encrypt a Message. Enter 2 to Decrypt an Encrypted Message. Enter 3 to Stop the Execution of Program. Now Enter Your Choice (1, 2, or 3) and Strike Enter Key: 2    Enter the Message to Decrypt (upto 100 alphabets): HfsiHwduytlwfumdNxAjwdUtbjwkzqHtrgnfynts    Decrypted Message: CandCryptographyIsVeryPowerfulCombination Enter 1 to Encrypt a Message. Enter 2 to Decrypt an Encrypted Message. Enter 3 to Stop the Execution of Program. Now Enter Your Choice (1, 2, or 3) and Strike Enter Key: 3    Thank you.

How It Works

Caesar cipher was invented by Julius Caesar . In this cipher, during encryption, key is added to ASCII values of letters in plaintext in order to obtain the ciphertext. Also, during decryption, key is subtracted from the ASCII values of letters in ciphertext in order to retrieve the plaintext.

For example, if value of key is 3 then during encryption, A would be ciphered to D, B would be ciphered to E, …, W would be ciphered to Z, X would be ciphered to A, Y would be ciphered to B, and Z would be ciphered to C. Ditto for lowrecase letters. During decryption, the procedure is reverted.

In LOC 3, the value of KEY is set to be 5. In the LOCs 4-6, three char type arrays are declared namely, msgOriginal, msgEncrypt, and msgDecrypt. LOCs 8-15 consist of definition of function menu(). This function displays menu for users on the screen so that various options in this program can be used conveniently. LOCs 10-13 ask the user to enter an appropriate choice and also describe the various options available to user. LOC 14 reads the choice entered by user and stores this choice in the int variable intChoice.

LOCs 16-41 consist of definition of the function encryptMsg(). This function converts the plaintext into ciphertext. LOCs 20-21 ask the user to enter plaintext. LOC 22 reads this plaintext and stores it in the variable msgOriginal. LOCs 24-38 consist of a for loop. In this for loop the plaintext is converted into ciphertext using the Caesar cipher method. Ciphertext is stored in the variable msgEncrypt. LOC 40 displays the ciphertext on the screen .

LOCs 42-66 consist of definition of the function decryptMsg(). This function retrieves the plaintext from the ciphertext. LOC 46 asks the user to enter the ciphertext. The ciphertext entered by user is read and stored in the variable msgEncrypt in LOC 47. LOCs 49-63 consist of a for loop. In this for loop, the ciphertext is decrypted into a plaintext and is stored in the variable msgDecrypt. LOC 65 displays this decrypted text on the screen.

LOCs 67-83 consist of definition of the function main(). LOCs 69-82 consist of a do-while loop. This seems to be an infinite loop, however, function exit() in the LOC 80 effectively stops the execution of this loop. LOC 70 calls the function menu() which displays the menu for users on the screen. The choice entered by user is stored in the int variable intChoice. LOCs 71-81 consist of a switch statement. Value stored in intChoice is passsed to this statement . If value of intChoice is 1 then function encryptMsg() is called. If value of intChoice is 2 then function decryptMsg() is called. If value of intChoice is else then function exit() is called which terminates the execution of do-while loop and also terminates the execution of this program .

10-3. Use the Transposition Cipher Method

Problem

You want to implement a cryptographic system using the Transposition cipher method.

Merits:

  • More secure compared to Caesar cipher

  • Can be modified to make the deciphering of ciphertext difficult

  • Different versions of transposition cipher are available and we have a choice to select a suitable method that serves the problem best

Demerits:

  • Logic is not simple and hence program is somewhat difficult to debug

  • Level of security offered is moderate

  • Not very effective for small messages

  • Transportation of keys securely is difficult

Solution

Write a C program that implements a cryptographic system using the Transposition cipher method, with the following specifications:

  • Program defines the functions: (a) menu() to display menu for users on the screen, (b) encryptMsg() to encrypt the plaintext, and (c) decryptMsg() to decrypt the ciphertext.

  • Program also defines the char type arrays msgOriginal, msgEncrypt, and msgDecrypt to store the messages. Array should accommodate 100 characters.

The Code

Code of C program written with these specifications is given below. Type the following C program in a text editor and save it in the folder C:\Code with the filename crypt3.c:

/* This program implements a cryptographic system using the Transposition cipher method. */                                                                    /* BL */ #include <stdio.h>                                                 /* L1 */ #include <string.h>                                                /* L2 */                                                                    /* BL */ #define  KEY  7                                                    /* L3 */                                                                    /* BL */ char msgToEncrypt[110];                                            /* L4 */ char msgToDecrypt[110];                                            /* L5 */ char msgEncrypt[20][KEY];                                          /* L6 */ char msgDecrypt[20][KEY];                                          /* L7 */ int intChoice, length;                                             /* L8 */                                                                    /* BL */ void menu()                                                        /* L9 */ {                                                                  /* L10 */   printf("\nEnter 1 to Encrypt a Message.");                       /* L11 */   printf("\nEnter 2 to Decrypt an Encrypted Message.");            /* L12 */   printf("\nEnter 3 to Stop the Execution of Program.");           /* L13 */   printf("\nNow Enter Your Choice (1, 2 or 3) and Strike Enter Key: ");  /* L14 */   scanf("%d", &intChoice);                                         /* L15 */ }                                                                  /* L16 */                                                                    /* BL  */ void encryptMsg()                                                  /* L17 */ {                                                                  /* L18 */   int row, col, rows, cilr, length, k = 0;                         /* L19 */   printf("Enter the Message (20 to 110 letters) to be Encrypted: \n");  /* L20 */   fflush(stdin);                                                   /* L21 */   gets(msgToEncrypt);                                              /* L22 */   length = strlen(msgToEncrypt);                                   /* L23 */   rows = (length/KEY) + 1 ;                                        /* L24 */   cilr = length % KEY; /* cilr - characters in last row */         /* L25 */   for(row = 0; row < rows; row++) {                                /* L26 */    for(col = 0; col < KEY; col++) {                                /* L27 */      msgEncrypt[row][col] = msgToEncrypt[k++];                     /* L28 */      if (k == length) break;                                       /* L29 */    }                                                               /* L30 */   }                                                                /* L31 */  printf("\nEncrypted Message: \n");                                /* L32 */   for(col = 0; col < KEY; col++) {                                 /* L33 */    for(row = 0; row < rows; row++) {                               /* L34 */      if ((col >= cilr) && (row == (rows-1)))                       /* L35 */        continue;                                                   /* L36 */      printf("%c", msgEncrypt[row][col]);                           /* L37 */    }                                                               /* L38 */   }                                                                /* L39 */ }                                                                  /* L41 */                                                                    /* BL  */ void decryptMsg()                                                  /* L42 */ {                                                                  /* L43 */   int row, col, rows, cilr, length, k = 0;                         /* L44 */   printf("Enter the Message (20 to 110 letters) to be Decrypted: \n");  /* L45 */   fflush(stdin);                                                   /* L46 */   gets(msgToDecrypt);                                              /* L47 */   length = strlen(msgToDecrypt);                                   /* L48 */   rows = (length/KEY) + 1 ;                                        /* L49 */   cilr = length % KEY;    /* cilr - characters in last row */      /* L50 */   for(col = 0; col < KEY; col++) {                                 /* L51 */    for(row = 0; row < rows; row++) {                               /* L52 */      if ((col >= cilr) && (row == (rows-1)))                       /* L53 */        continue;                                                   /* L54 */      msgDecrypt[row][col] = msgToDecrypt[k++];                     /* L55 */      if (k == length) break;                                       /* L56 */    }                                                               /* L57 */   }                                                                /* L58 */   printf("\nDecrypted Message: \n");                               /* L59 */   for(row = 0; row < rows; row++) {                                /* L60 */    for(col = 0; col < KEY; col++) {                                /* L61 */      printf("%c", msgDecrypt[row][col]);                           /* L62 */    }                                                               /* L63 */   }                                                                /* L64 */ }                                                                  /* L65 */                                                                    /* BL  */ void main()                                                        /* L66 */ {                                                                  /* L67 */   do {                                                             /* L68 */     menu();                                                        /* L69 */     switch (intChoice) {                                           /* L70 */       case 1:                                                      /* L71 */                encryptMsg();                                       /* L72 */                break;                                              /* L73 */       case 2:                                                      /* L74 */                decryptMsg();                                       /* L75 */                break;                                              /* L76 */       default:                                                     /* L77 */                printf("\nThank you.\n");                           /* L78 */                exit(0);                                            /* L79 */     }                                                              /* L80 */   } while (1);                                                     /* L81 */ }                                                                  /* L82 */

Compile and execute this program. A run of this program is given below:

Enter 1 to Encrypt a Message. Enter 2 to Decrypt an Encrypted Message. Enter 3 to Stop the Execution of Program. Now Enter Your Choice (1, 2, or 3) and Strike Enter Key: 1    Enter the Message (20 to 110 letters) to be Encrypted: C and Cryptography is very powerful combination.    Encrypted Message: Cra o a ypvwctapheeointyrrmodo yfbn gi ui.Crspln Enter 1 to Encrypt a Message. Enter 2 to Decrypt an Encrypted Message. Enter 3 to Stop the Execution of Program. Now Enter Your Choice (1, 2, or 3) and Strike Enter Key: 2    Enter the Message (20 to 110 letters) to be Decrypted: Cra o a ypvwctapheeointyrrmodo yfbn gi ui.Crspln    Decrypted Message: C and Cryptography is very powerful combination. Enter 1 to Encrypt a Message. Enter 2 to Decrypt an Encrypted Message. Enter 3 to Stop the Execution of Program. Now Enter Your Choice (1, 2, or 3) and Strike Enter Key: 3    Thank you.

How It Works

Firstly, let us discuss the working of Transposition cipher. Transposition cipher is illustrated in figure 10-1. Draw the table as shown in figure. Number of columns in this table should be equal to key. Here, key is 7, hence number of columns is also 7. Now write the plaintext in this table as follows: Write the first 7 (because key is 7) characters in the first row (i.e., row 0), then write the next 7 characters in the second row (i.e., row 1), and so on. The last cell in the last row is unoccupied hence it is shown shaded. Now plaintext is encrypted as follows: Firstly, write the characters in the first column (i.e., col 0), then write the characters in the second column (i.e., col 1), and so on. In this manner you get the encrypted text. Decryption is simply reverse of the encryption.

Now let us discuss the working of this program. In LOC 3, the value of KEY is set to be 7. LOCs 4-8 consist of variable declaration. In LOCs 4-5 two 1-dimensional char type arrays are declared, namely, msgToEncrypt and msgToDecrypt. In LOCs 6-7 two 2-dimensional char type arrays are declared, namely, msgEncrypt and msgDecrypt.

LOCs 9-16 consist of definition of function menu(). This function displays menu for users on the screen so that various options in this program can be used conveniently. LOCs 11-14 ask the user to enter an appropriate choice and also describe the various options available to user. LOC 15 reads the choice entered by user and stores this choice in the int variable intChoice.

LOCs 17-41 consist of definition of the function encryptMsg(). This function converts the plaintext into ciphertext. LOC 20 asks the user to enter plaintext. LOC 22 reads this plaintext and stores it in the variable msgToEncrypt. LOC 24 computes the number of row required for the storage of message. LOC 25 computes the cilr, i.e., characters in last row. Here, cilr is 6 and hence one cell in the last row is empty and it is shown shaded. LOCs 26-31 consist of nested for loops. In this nested for loops, message is encrypted and stored in the variable msgEncrypt. LOCs 32-39 display the encrypted message on the screen.

LOCs 42-65 consist of definition of the function decryptMsg(). This function retrieves the plaintext from the ciphertext. LOC 45 asks the user to enter the ciphertext. The ciphertext entered by user is read and stored in the variable msgToDecrypt in LOC 47. LOC 49 calculates number of rows required. LOC 50 calculates cilr, i.e., characters in the last row. LOCs 51-58 consist of nested for loops. In these nested loops, plaintext is retrieved from the ciphertext. LOCs 59-64 display the decrypted message on the screen.

LOCs 66-82 consist of definition of the function main(). LOCs 68-81 consist of a do-while loop. This seems to be an infinite loop, however, function exit() in the LOC 79 effectively stops the execution of this loop. LOC 69 calls the function menu() which displays the menu for users on the screen. The choice entered by user is stored in the int variable intChoice. LOCs 70-80 consist of a switch statement. Value stored in intChoice is passsed to this statement. If value of intChoice is 1 then function encryptMsg() is called. If value of intChoice is 2 then function decryptMsg() is called. If value of intChoice is else then function exit() is called which terminates the execution of do-while loop and also terminates the execution of this program .

10-4. Use the Multiplicative Cipher Method

Problem

You want to implement a cryptographic system using the Multiplicative cipher method.

Merits:

  • Level of security offered is good

  • System requirements are not very high

Demerits:

  • Logic is not simple and hence program is somewhat difficult to implement and debug

  • Letter A in plaintext always encrypts to A

  • Ciphertext can be deciphered using brute force techniques with a very high speed computer

  • Transportation of keys securely is difficult

Solution

Write a C program that implements a cryptographic system using the Multiplicative cipher method, with the following specifications:

  • Program defines the functions: (a) menu() to display menu for users on the screen, (b) encryptMsg() to encrypt the plaintext, and (c) decryptMsg() to decrypt the ciphertext.

  • Program also defines the char type arrays msgOriginal, msgEncrypt, and msgDecrypt to store the messages. Array should accommodate 100 characters.

The Code

Code of C program written with these specifications is given below. Type the following C program in a text editor and save it in the folder C:\Code with the filename crypt4.c:

/* This program implements a cryptographic system using the Multiplicative cipher method. */                                                                    /* BL */ #include <stdio.h>                                                 /* L1 */ #include <string.h>                                                /* L2 */                                                                    /* BL */ char msgOriginal[100];                                             /* L3 */ char msgEncrypt[100];                                              /* L4 */ char msgDecrypt[100];                                              /* L5 */ int length, intChoice, a = 3;    /* a is the KEY */                /* L6 */                                                                    /* BL */ void menu()                                                        /* L7 */ {                                                                  /* L8 */   printf("\nEnter 1 to Encrypt a Message.");                       /* L9 */   printf("\nEnter 2 to Decrypt an Encrypted Message.");            /* L10 */   printf("\nEnter 3 to Stop the Execution of Program.");           /* L11 */   printf("\nNow Enter Your Choice (1, 2 or 3) and Strike Enter Key: "); /* L12 */   scanf("%d", &intChoice);                                         /* L13 */ }                                                                  /* L14 */                                                                    /* BL  */ void encryptMsg()                                                  /* L15 */ {                                                                  /* L16 */   int i ;                                                          /* L17 */   printf("Enter the Message to Encrypt in FULL CAPS, Do Not Include \n"); /* L18 */   printf("Spaces and Punctuation Symbols (upto 100 characters): \n");  /* L19 */   fflush(stdin);                                                   /* L20 */   gets(msgOriginal);                                               /* L21 */   length = strlen(msgOriginal);                                    /* L22 */   for (i = 0; i < length; i++)                                     /* L23 */     msgEncrypt[i] = (((a * msgOriginal[i]) % 26) + 65);            /* L24 */   msgEncrypt[length] = '\0';                                       /* L25 */   printf("\nEncrypted Message: %s", msgEncrypt);                   /* L26 */ }                                                                  /* L27 */                                                                    /* BL  */ void decryptMsg()                                                  /* L28 */ {                                                                  /* L29 */   int i;                                                           /* L30 */   int aInv = 0;                                                    /* L31 */   int flag = 0;                                                    /* L32 */   printf("Enter the Message to Decrypt (upto 100 characters): \n"); /* L33 */   fflush(stdin);                                                   /* L34 */   gets(msgEncrypt);                                                /* L35 */   length = strlen(msgEncrypt);                                     /* L36 */    for (i = 0; i < 26; i++) {                                      /* L37 */      flag = (a * i) % 26;                                          /* L38 */             if (flag == 1)                                         /* L39 */                 aInv = i;                                          /* L40 */    }                                                               /* L41 */    for (i = 0; i < length; i++)                                    /* L42 */       msgDecrypt[i] = (((aInv * msgEncrypt[i]) % 26) + 65);        /* L43 */    msgDecrypt[length] = '\0';                                      /* L44 */    printf("\nDecrypted Message: %s", msgDecrypt);                  /* L45 */ }                                                                  /* L46 */                                                                    /* BL  */ void main()                                                        /* L47 */ {                                                                  /* L48 */   do {                                                             /* L49 */     menu();                                                        /* L50 */     switch (intChoice) {                                           /* L51 */       case 1:                                                      /* L52 */                encryptMsg();                                       /* L53 */                break;                                              /* L54 */       case 2:                                                      /* L55 */                decryptMsg();                                       /* L56 */                break;                                              /* L57 */       default:                                                     /* L58 */                printf("\nThank you.\n");                           /* L59 */                exit(0);                                            /* L60 */     }                                                              /* L61 */   } while (1);                                                     /* L62 */ }                                                                  /* L63 */

Compile and execute this program. A run of this program is given below:

Enter 1 to Encrypt a Message. Enter 2 to Decrypt an Encrypted Message. Enter 3 to Stop the Execution of Program. Now Enter Your Choice (1, 2, or 3) and Strike Enter Key: 1    Enter the Message to Encryptin FULL CAPS, Do Not Include Spaces and Punctation Symbols (upto 100 characters): CANDCRYPTOGRAPHYISVERYPOWERFULCOMBINATION    Encrypted Message: TNAWTMHGSDFMNGIHLPYZMHGDBZMCVUTDXQLANSLDA Enter 1 to Encrypt a Message. Enter 2 to Decrypt an Encrypted Message. Enter 3 to Stop the Execution of Program. Now Enter Your Choice (1, 2, or 3) and Strike Enter Key: 2    Enter the Message to Decrypt (upto 100 characters): TNAWTMHGSDFMNGIHLPYZMHGDBZMCVUTDXQLANSLDA    Decrypted Message: CANDCRYPTOGRAPHYISVERYPOWERFULCOMBINATION Enter 1 to Encrypt a Message. Enter 2 to Decrypt an Encrypted Message. Enter 3 to Stop the Execution of Program. Now Enter Your Choice (1, 2, or 3) and Strike Enter Key: 3    Thank you.

How It Works

Firstly, let us discuss the working of Multiplicative cipher. In Caesar cipher, key is added to serial number of letters in plaintext. In Multiplicative cipher, key is multiplied to serial number of letters in plaintext in order to encrypt it. During decryption, serial numbers of letters in ciphertext are multiplied by an inverse function of the key to retrieve the plaintext. To keep the things simple, let us consider only capital alphabets. Hence, the symbol set consists of only 26 characters. The serial number of capital alphabets are as follows: A → 0, B → 1, C → 2, …, I → 8, …, K → 10, …, U → 20, …, X → 23, Y → 24, Z → 25. Suppose the key is 3. With this key, letter A (whose serial number is 0) would be encrypted as follows:

A would be encrypted to → (serial no. of A x key) = 0 x 3 = 0  = A

With the same key, letter B (whose serial number is 1) would be encrypted as follows:

B would be encrypted to → (serial no. of B x key) = 1 x 3 = 3 = D

With the same key, letter C (whose serial number is 2) would be encyrpted as follows:

C would be encrypted to → (serial no. of C x key) = 2 x 3 = 6 = G

With the same key, letter K (whose serial number is 10) would be encrypted as follows:

K would be encrypted to → (serial no. of D x key) = 10 x 3 = 30 = 30 - 26 = 4 = E

With the same key, letter U (whose serial number is 20) would be encrypted as follows:

U would be encrypted to → (serial no. of U x key) = 20 x 3 = 60 = 60 - 26 - 26 = 8 = I

If the product (serial no. × key) exceeds 25 we subtract 26 from it repeatedly till the result is less than 26. In program this is done using the modulus operator %. Notice the LOC 24 given below:

msgEncrypt[i] = (((a * msgOriginal[i]) % 26) + 65);          /* L24 */

In this LOC a is key and its value is set to 3 in LOC 6. The msgOriginal[i] is nothing but ASCII value of (i+1)th letter in the plaintext. Also, 65 are added to result in order to convert serial number of letter to its ASCII value as ASCII value of letter A is 65.

LOC 43 is mainly responsible for decryption of ciphertext and it is reproduced below for your quick reference:

msgDecrypt[i] = (((aInv * msgEncrypt[i]) % 26) + 65);        /* L43 */

Here, aInv is modular inverse of (a % 26) and msgEncrypt[i] is nothing but the ASCII value of (i+1)th letter in the ciphertext. Also, 26 is the size of symbol set. Only capital alphabets are used to form the plaintext hence symbol set consists of only 26 characters. Modular inverse is computed using the following formula. Let i be the modular inverse of (a % m) then following relation holds good :

(a * i) % m = 1

LOCs 37-41 consist of a for loop which computes the aInv, the modular inverse of (a % 26) where a is the key and 26 is the size of symbol set.

In this program, the key is 7. Generally, the key is kept very large in order to make its hacking with brute-force technique very difficult. Keys with 7 or 8 digits are not uncommon in Multiplicative cipher. But unlike in Caesar cipher, you just cannot take any intger as a key in Multiplicative cipher. For example, if you choose the key = 8. Then both B and O encrypt to the same letter I. Also, both C and P encrypt to the same letter Q. Certainly, this key is useless. Ideally, every letter in alphabet A to Z must encrypt to a unique letter in alphabet A to Z, otherwise the cipher would not work. The useful key in Multiplicative cipher is selected using the following formula:

Note

Key and size of symbol set must be relatively prime to each other. Numbers m and n are relatively prime to each other when their gcd ("greatest common divisor" also called “greatest common factor”) is 1.

There is Euclid’s algorithm to find the gcd of two positive numbers m and n, given below:

Step 1: Divide m by n and let r be the remainder. Step 2: If r is 0, n is the answer; if r is not 0, go to step 3. Step 3: Set m = n and n = r. Go back to step 1.

In this program key is 3 and size of symbol set is 26. As these numbers are small, it is immediately clear that their gcd is 1 and these numbers are realtivley prime. However, if numbers are large, then you are required to use Euclid’s algorithm to verify the usefulness of key.

Now let us discuss the working of this program. LOCs 3-6 consist of variable declaration. In LOCs 3-5 three char type arrays are declared, namely, msgOriginal, msgEncrypt, and msgDecrypt. In LOC 6, the value of variable a is set to 3.

LOCs 7-14 consist of definition of function menu(). This function displays menu for users on the screen so that various options in this program can be used conveniently. LOCs 9-12 ask the user to enter an appropriate choice and also describe the various options available to user. LOC 13 reads the choice entered by user and stores this choice in the int variable intChoice.

LOCs 15-27 consist of definition of the function encryptMsg(). This function converts the plaintext into ciphertext. LOCs 18-19 ask the user to enter plaintext. LOC 21 reads this plaintext and stores it in the variable msgOriginal. LOCs 23-24 consist of a for loop. In this for loop the plaintext is converted into a ciphertext according to multiplicative cipher logic. LOC 26 displays the encrypted message on the screen.

LOCs 28-46 consist of definition of the function decryptMsg(). This function retrieves the plaintext from the ciphertext. LOC 33 asks the user to enter the ciphertext. The ciphertext entered by user is read and stored in the variable msgEncrypt in LOC 35. LOCs 37-43 consist of two for loops in consecution. In these for loops the plaintext is retrieved from the ciphertext. In LOC 45, the decrypted message is displayed on the screen.

LOCs 47-63 consist of definition of the function main(). LOCs 49-62 consist of a do-while loop. This seems to be an infinite loop, however, function exit() in the LOC 60 effectively stops the execution of this loop. LOC 50 calls the function menu() which displays the menu for users on the screen. The choice entered by user is stored in the int variable intChoice. LOCs 51-61 consist of a switch statement. Value stored in intChoice is passsed to this statement. If value of intChoice is 1 then function encryptMsg() is called. If value of intChoice is 2 then function decryptMsg() is called. If value of intChoice is else then function exit() is called which terminates the execution of do-while loop and also terminates the execution of this program .

10-5. Use the Affine Cipher Method

Problem

You want to implement a cryptographic system using the Affine cipher method.

Merits:

  • Level of security offered is better than multiplicative cipher

  • System requirements are not very high

  • Utilizes the benefits of Caesar cipher and multiplicative cipher

Demerits:

  • Logic is not simple and hence program is somewhat difficult to implement and debug

  • Ciphertext can be deciphered using brute force technique with a very high speed computer

  • Transportation of keys securely is difficult

Solution

Write a C program that implements a cryptographic system using the Affine cipher method, with the following specifications:

  • Program defines the functions: (a) menu() to display menu for users on the screen, (b) encryptMsg() to encrypt the plaintext, and (c) decryptMsg() to decrypt the ciphertext.

  • Program also defines the char type arrays msgOriginal, msgEncrypt, and msgDecrypt to store the messages. Array should accommodate 100 characters.

The Code

Code of C program written with these specifications is given below. Type the following C program in a text editor and save it in the folder C:\Code with the filename crypt5.c:

/* This program implements a cryptographic system using the Affine cipher method. */                                                                    /* BL */ #include <stdio.h>                                                 /* L1 */ #include <string.h>                                                /* L2 */                                                                    /* BL */ char msgOriginal[100];                                             /* L3 */ char msgEncrypt[100];                                              /* L4 */ char msgDecrypt[100];                                              /* L5 */ int intChoice, length, a = 3, b = 5;                               /* L6 */                                                                    /* BL */ void menu()                                                        /* L7 */ {                                                                  /* L8 */   printf("\nEnter 1 to Encrypt a Message.");                       /* L9 */   printf("\nEnter 2 to Decrypt an Encrypted Message.");            /* L10 */   printf("\nEnter 3 to Stop the Execution of Program.");           /* L11 */   printf("\nNow Enter Your Choice (1, 2 or 3) and Strike Enter Key: "); /* L12 */   scanf("%d", &intChoice);                                         /* L13 */ }                                                                  /* L14 */                                                                    /* BL  */ void encryptMsg()                                                  /* L15 */ {                                                                  /* L16 */   int i;                                                           /* L17 */   printf("Enter the Message to Encrypt in FULL CAPS, Do Not Include \n"); /* L18 */   printf("Spaces and Punctuation Symbols (upto 100 characters): \n"); /* L19 */   fflush(stdin);                                                   /* L20 */   gets(msgOriginal);                                               /* L21 */   length = strlen(msgOriginal);                                    /* L22 */   for (i = 0; i < length; i++)                                     /* L23 */     msgEncrypt[i] = ((((a * msgOriginal[i]) + b) % 26) + 65);      /* L24 */   msgEncrypt[length] = '\0';                                       /* L25 */   printf("\nEncrypted Message: %s", msgEncrypt);                   /* L26 */ }                                                                  /* L27 */                                                                    /* BL */ void decryptMsg()                                                  /* L28 */ {                                                                  /* L29 */  int i;                                                            /* L30 */   int aInv = 0;                                                    /* L31 */   int flag = 0;                                                    /* L32 */   printf("Enter the Message to Decrypt (upto 100 chars): \n");     /* L33 */   fflush(stdin);                                                   /* L34 */   gets(msgEncrypt);                                                /* L35 */   length = strlen(msgEncrypt);                                     /* L36 */   for (i = 0; i < 26; i++) {                                       /* L37 */     flag = (a * i) % 26;                                           /* L38 */     if (flag == 1)                                                 /* L39 */         aInv = i;                                                  /* L40 */    }                                                               /* L41 */    for (i = 0; i < length; i++)                                    /* L42 */       msgDecrypt[i] = (((aInv * ((msgEncrypt[i] - b)) % 26)) + 65);   /* L43 */    msgDecrypt[length] = '\0';                                      /* L44 */    printf("\nDecrypted Message: %s", msgDecrypt);                  /* L45 */ }                                                                  /* L46 */                                                                    /* BL  */ void main()                                                        /* L47 */ {                                                                  /* L48 */   do {                                                             /* L49 */     menu();                                                        /* L50 */     switch (intChoice) {                                           /* L51 */       case 1:                                                      /* L52 */                encryptMsg();                                       /* L53 */                break;                                              /* L54 */       case 2:                                                      /* L55 */                decryptMsg();                                       /* L56 */                break;                                              /* L57 */       default:                                                     /* L58 */                printf("\nThank you.\n");                           /* L59 */                exit(0);                                            /* L60 */     }                                                              /* L61 */   } while (1);                                                     /* L62 */ }                                                                  /* L63 */

Compile and execute this program. A run of this program is given below:

Enter 1 to Encrypt a Message. Enter 2 to Decrypt an Encrypted Message. Enter 3 to Stop the Execution of Program. Now Enter Your Choice (1, 2, or 3) and Strike Enter Key: 1    Enter the Message to Encrypt in FULL CAPS, Do Not Include Spaces and Punctuation Symbols (upto 100 characters): CANDCRYPTOGRAPHYISVERYPOWERFULCOMBINATION    Encrypted Message: YSFBYRMLXIKRSLNMQUDERMLIGERHAZYICVQFSXQIF Enter 1 to Encrypt a Message. Enter 2 to Decrypt an Encrypted Message. Enter 3 to Stop the Execution of Program. Now Enter Your Choice (1, 2, or 3) and Strike Enter Key: 2    Enter the Message to Decrypt (upto 100 characters): YSFBYRMLXIKRSLNMQUDERMLIGERHAZYICVQFSXQIF    Decrypted Message: CANDCRYPTOGRAPHYISVERYPOWERFULCOMBINATION Enter 1 to Encrypt a Message. Enter 2 to Decrypt an Encrypted Message. Enter 3 to Stop the Execution of Program. Now Enter Your Choice (1, 2, or 3) and Strike Enter Key: 3    Thank you.

How It Works

Firstly, let us discuss the working of Affine cipher. A notable drawback of Multiplicative cipher is that letter A in plaintext always encrypts to A. This is a sort of loop hole. In order to deal with this drawback, Multiplicative cipher is modified to Affine cipher. Affice cipher is combination of Multiplicative cipher and Caesar cipher. Consequently, there are two keys in Affine cipher, namely, key_A and key_B; and in this program, these keys are represented by the variables a and b, respectively. Also, their values are set to 3 (for a) and 5 (for b) in LOC 6. First key, key_A is used for Multiplicative component of Affine cipher and second key, key_B is used for Caesar component of Affine cipher. The restrictions on the selection of key_A in Affine cipher are same as that of in Multiplicative cipher. Like in Caesar cipher, there are almost no restrictions on the selection key_B.

LOC 24 mainly encrypts the plaintext into ciphertext and it is reproduced below for your quick reference:

msgEncrypt[i] = ((((a * msgOriginal[i]) + b) % 26) + 65);      /* L24 */

Here, msgOriginal[i] represents the ASCII value of (i+1)th character in plaintext. Also, a and b are first and second keys, respectively. Key a is meant for Multiplicative component and key b is meant for Ceasar component. 26 is the size of the symbol set as only capital alphabets A..Z are used for forming the plaintext. Finally, integer 65 is ASCII value of letter A.

LOC 43 mainly decrypts the ciphertext to retrieve the plaintext and it is reproduced below for your quick reference:

msgDecrypt[i] = (((aInv * ((msgEncrypt[i] - b)) % 26)) + 65);     /* L43 */

Here, aInv is modular inverse of (a % 26), b is second key meant for Caesar component, msgEncrypt[i] is ASCII value of (i+1)th character in ciphertext, 26 is size of symbol set, and 65 is ASCII value of A.

Now let us discuss the working of this program. LOCs 3-6 consist of variable declaration. In LOCs 3-5 three char type arrays are declared, namely, msgOriginal, msgEncrypt, and msgDecrypt. In LOC 6, the value of variable a is set to 3 and value of variable b is set to 5.

LOCs 7-14 consist of definition of function menu(). This function displays menu for users on the screen so that various options in this program can be used conveniently. LOCs 9-12 ask the user to enter an appropriate choice and also describe the various options available to user. LOC 13 reads the choice entered by user and stores this choice in the int variable intChoice.

LOCs 15-27 consist of definition of the function encryptMsg(). This function converts the plaintext into ciphertext. LOCs 18-19 ask the user to enter plaintext. LOC 21 reads this plaintext and stores it in the variable msgOriginal. LOCs 23-24 consist of a for loop. In this for loop the plaintext is converted into a ciphertext according to affine cipher logic. LOC 26 displays the encrypted message on the screen.

LOCs 28-46 consist of definition of the function decryptMsg(). This function retrieves the plaintext from the ciphertext. LOC 33 asks the user to enter the ciphertext. The ciphertext entered by user is read and stored in the variable msgEncrypt in LOC 35. LOCs 37-43 consist of two for loops in consecution. In these for loops the plaintext is retrieved from the ciphertext. In LOC 45, the decrypted message is displayed on the screen.

LOCs 47-63 consist of definition of the function main(). LOCs 49-62 consist of a do-while loop. This seems to be an infinite loop, however, function exit() in the LOC 60 effectively stops the execution of this loop. LOC 50 calls the function menu() which displays the menu for users on the screen. The choice entered by user is stored in the int variable intChoice. LOCs 51-61 consist of a switch statement. Value stored in intChoice is passsed to this statement. If value of intChoice is 1 then function encryptMsg() is called. If value of intChoice is 2 then function decryptMsg() is called. If value of intChoice is else then function exit() is called which terminates the execution of do-while loop and also terminates the execution of this program .

10-6. Use the Simple Substitution Cipher Method

Problem

You want to implement a cryptographic system using the Simple Substitution cipher method.

Merits:

  • Level of security offered is good

  • System requirements are not very high

  • Logic used is simple to implement

Demerits:

  • Execution of program is not very fast

  • Ciphertext can be deciphered using brute force technique with a very high speed computer

  • Transportation of keys securely is difficult

Solution

Write a C program that implements a cryptographic system using the Simple Substitution cipher method, with the following specifications:

  • Program defines the functions: (a) generateKey() to generate the encrypt key and decrypt key, (b) menu() to display menu for users on the screen, (c) encryptMsg() to encrypt the plaintext, and (d) decryptMsg() to decrypt the ciphertext.

  • Program also defines the char type arrays msgOriginal, msgEncrypt, and msgDecrypt to store the messages. Array should accommodate 100 characters.

The Code

Code of C program written with these specifications is given below. Type the following C program in a text editor and save it in the folder C:\Code with the filename crypt6.c:

/* This program implements a cryptographic system using the Simple Substitution */ /* cipher method. */                                                                    /* BL */ #include <stdio.h>                                                 /* L1 */ #include <stdlib.h>                                                /* L2 */ #include <string.h>                                                /* L3 */                                                                    /* BL */ char msgOriginal[100];                                             /* L4 */ char msgEncrypt[100];                                              /* L5 */ char msgDecrypt[100];                                              /* L6 */ int intEncryptKey[26], intDecryptKey[26];                          /* L7 */ int intChoice, i, j, seed, length, randNum, num, flag = 1, tag = 0;   /* L8 */                                                                    /* BL */ void generateKey()                                                 /* L9 */ {                                                                  /* L10 */   printf("\nEnter seed S (1 <= S <= 30000): ");                    /* L11 */   scanf("%d", &seed);                                              /* L12 */   srand(seed);                                                     /* L13 */   for(i=0; i < 26; i++)                                            /* L14 */     intEncryptKey[i] = -1;                                         /* L15 */   for(i=0; i < 26; i++) {                                          /* L16 */     do {                                                           /* L17 */       randNum = rand();                                            /* L18 */       num = randNum % 26;                                          /* L19 */       flag = 1;                                                    /* L20 */       for(j = 0; j < 26; j++)                                      /* L21 */         if (intEncryptKey[j] == num)                               /* L22 */            flag = 0;                                               /* L23 */         if (flag == 1){                                            /* L24 */           intEncryptKey[i] = num;                                  /* L25 */           tag = tag + 1;                                           /* L26 */         }                                                          /* L27 */     } while ((!flag) && (tag < 26 ));                              /* L28 */   }                                                                /* L29 */   printf("\nEncryption KEY = ");                                   /* L30 */   for(i=0; i < 26; i++)                                            /* L31 */     printf("%c", intEncryptKey[i] + 65);                           /* L32 */   for(i = 0; i < 26; i++) {                                        /* L33 */     for(j = 0; j < 26; j++) {                                      /* L34 */       if(i  == intEncryptKey[j]) {                                 /* L35 */         intDecryptKey[i] = j ;                                     /* L36 */         break;                                                     /* L37 */       }                                                            /* L38 */     }                                                              /* L39 */   }                                                                /* L40 */   printf("\nDecryption KEY = ");                                   /* L41 */   for(i=0; i < 26; i++)                                            /* L42 */     printf("%c", intDecryptKey[i] + 65);                           /* L43 */ }                                                                  /* L44 */                                                                    /* BL  */ void menu()                                                        /* L45 */ {                                                                  /* L46 */   printf("\nEnter 1 to Encrypt a Message.");                       /* L47 */   printf("\nEnter 2 to Decrypt an Encrypted Message.");            /* L48 */   printf("\nEnter 3 to Stop the Execution of Program.");           /* L49 */   printf("\nNow Enter Your Choice (1, 2 or 3) and Strike Enter Key: "); /* L50 */   scanf("%d", &intChoice);                                         /* L51 */ }                                                                  /* L52 */                                                                    /* BL  */ void encryptMsg()                                                  /* L53 */ {                                                                  /* L54 */   printf("Enter the Message to Encrypt in FULL CAPS, Do Not Include \n"); /* L55 */   printf("Spaces and Punctuation Symbols (upto 100 characters): \n"); /* L56 */   fflush(stdin);                                                   /* L57 */   gets(msgOriginal);                                               /* L58 */   length = strlen(msgOriginal);                                    /* L59 */   for (i = 0; i < length; i++)                                     /* L60 */     msgEncrypt[i] = (intEncryptKey[(msgOriginal[i]) - 65]) + 65;   /* L61 */   msgEncrypt[length] = '\0';                                       /* L62 */   printf("\nEncrypted Message: %s", msgEncrypt);                   /* L63 */ }                                                                  /* L64 */                                                                    /* BL */ void decryptMsg()                                                  /* L65 */ {                                                                  /* L66 */   printf("Enter the Message to Decrypt (upto 100 chars): \n");     /* L67 */   fflush(stdin);                                                   /* L68 */   gets(msgEncrypt);                                                /* L69 */   length = strlen(msgEncrypt);                                     /* L70 */   for (i = 0; i < length; i++)                                     /* L71 */    msgDecrypt[i] = (intDecryptKey[(msgEncrypt[i]) - 65]) + 65;     /* L72 */    msgDecrypt[length] = '\0';                                      /* L73 */    printf("\nDecrypted Message: %s", msgDecrypt);                  /* L74 */ }                                                                  /* L75 */                                                                    /* BL  */ void main()                                                        /* L76 */ {                                                                  /* L77 */   generateKey();                                                   /* L78 */   do {                                                             /* L79 */     menu();                                                        /* L80 */     switch (intChoice) {                                           /* L81 */       case 1:                                                      /* L82 */                encryptMsg();                                       /* L83 */                break;                                              /* L84 */       case 2:                                                      /* L85 */                decryptMsg();                                       /* L86 */                break;                                              /* L87 */       default:                                                     /* L88 */                printf("\nThank you.\n");                           /* L89 */                exit(0);                                            /* L90 */     }                                                              /* L91 */   } while (1);                                                     /* L92 */ }                                                                  /* L93 */

Compile and execute this program. A run of this program is given below:

Enter seed S (1 <= S <= 30000): 2000    Encryption KEY = KJVWBAIZRUHNFXGDMTLPOQSCEY Decryption KEY = FEXPYMOKGBASQLUTVIWRJCDNZH Enter 1 to Encrypt a Message. Enter 2 to Decrypt an Encrypted Message. Enter 3 to Stop the Execution of Program. Now Enter Your Choice (1, 2, or 3) and Strike Enter Key: 1    Enter the Message to Encrypt in FULL CAPS, Do Not Include Spaces and Punctuation Symbols (upto 100 characters): CANDCRYPTOGRAPHYISVERYPOWERFULCOMBINATION    Encrypted Message: VKXWVTEDPGITKDDZERLQBTEDGSBTAONVGFJRXKPRGX Enter 1 to Encrypt a Message. Enter 2 to Decrypt an Encrypted Message.   Enter 3 to Stop the Execution of Program. Now Enter Your Choice (1, 2, or 3) and Strike Enter Key: 2    Enter the Message to Decrypt (upto 100 characters): VKXWVTEDPGITKDDZERLQBTEDGSBTAONVGFJRXKPRGX    Decrypted Message: CANDCRYPTOGRAPHYISVERYPOWERFULCOMBINATION Enter 1 to Encrypt a Message. Enter 2 to Decrypt an Encrypted Message. Enter 3 to Stop the Execution of Program. Now Enter Your Choice (1, 2, or 3) and Strike Enter Key: 3    Thank you.

How It Works

Firstly, let us discuss the working of Simple Substitution cipher. In Simple Substitution cipher both encryption and decryption keys are 26 letters long. Here, to keep the things simple, plaintext will be formed using only capital alphabets A..Z. In order to create encryption key, simply place all the 26 capital alphabets in random order, one by one, and you get encryption key. The encryption key generated in a sample run of this program is given below:

Encryption KEY = KJVWBAIZRUHNFXGDMTLPOQSCEY

There are whopping 403,291,461,126,605,635,584,000,000 number of encryption keys possible for Simple Substitution cipher. This rules out the chances of hacking the ciphertext created under Simple Substitution cipher. Even if you employ a computer that would try out a billion keys every second, it would take about twelve billion years to try out all the possible keys.

Suppose your plaintext is CAT. Let us encrypt it. Encryption key is reproduced below alongwith standard alphabet for quick comparison:

Standard Alphabet = ABCDEFGHIJKLMNOPQRSTUVWXYZ Encryption KEY    = KJVWBAIZRUHNFXGDMTLPOQSCEY

Letter C (which is third letter in standard alphabet) would encrypt to letter V because third letter in encryption key is V. Letter A (which is first letter in standard alphabet) would encrypt to letter K because first letter in encryption key is K. Letter T (which is twentieth letter in standard alphabet) would encrypt to letter P because twentieth letter in encryption key is P. Thus ciphertext is VKP.

Now let us decrypt this ciphertext. The decryption key corresponding to this encryption key is given below along with standard alphabet for quick comparison :

Standard Alphabet = ABCDEFGHIJKLMNOPQRSTUVWXYZ Decryption KEY    = FEXPYMOKGBASQLUTVIWRJCDNZH

Ciphertext is VKP. Letter V would decrypt to letter C because twenty second letter in decryption key is C. Letter K would decrypt to letter A because eleventh letter in decryption key is A. Letter P would decrypt to letter T because sixteenth letter in decryption key is T. Thus plaintext retrieved from ciphertext is CAT, as expected.

Encryption key is generated by placing the 26 alphabets randomly, one by one. However, generation of decryption key is dependent on encryption key. Let us see how this, above given, decryption key is generated.

First letter in decryption key is F because in encryption key A is the sixth letter and sixth letter in standard alphabet is F. Second letter in decryption key is E because in encryption key B is the fifth letter and fifth letter in standard alphabet is E. Third letter in decryption key is X because in encryption key C is the twenty fourth letter and twenty fourth letter in standard alphabet is X. And so on.

While generating the encryption key in this program, random numbers are generated using the function rand(). Random number generation in computers is not truly random. However, in this program, provision is made to enter the “seed” (an integer) that can push the random number generation in computers close to true random number generation. User can type different seed everytime he/she runs a program and get a different encryption key.

Now let us discuss the working of this program. LOCs 4-8 consist of variable declaration. In LOCs 4-6 three char type arrays are declared, namely, msgOriginal, msgEncrypt, and msgDecrypt. The size of these char arrays is 100. In LOC 7, two int type arrays are declared, namely, intEncryptKey and intDecryptKey. The size of these int arrays is 26. In LOC 8, few int type variables are declared.

LOCs 9-44 consist of definition of the function generateKey(). This function generates two keys, namely, encrypt key and decrypt key. LOC 11 asks the user to enter the “seed” which is nothing but an integer in the range 1 to 30,000. The “seed” entered by user is read and stored in the variable seed in the LOC 12. This “seed” is used for generating a random number. LOCs 14-15 consist of a for loop which places the number -1 in every cell of the array intEncryptKey. Number -1 in any cell indicates that proper key is not yet placed in that cell. LOCs 16-29 consist of a for loop. In this for loop encrypt key is generated, i.e., 26 capital letters are filled in the array intEncryptKey at random. This encrypt key is displayed on the screen in the LOCs 30-32. LOCs 31-40 again consist of a for loop. In this for loop decrypt key is generated. This decrypt is, however, dependent on encrypt key and it is displayed on the screen in the LOCs 41-43.

LOCs 45-52 consist of definition of function menu(). This function displays menu for users on the screen so that various options in this program can be used conveniently. LOCs 47-50 ask the user to enter an appropriate choice and also describe the various options available to user. LOC 51 reads the choice entered by user and stores this choice in the int variable intChoice.

LOCs 53-64 consist of definition of the function encryptMsg(). This function converts the plaintext into ciphertext. LOCs 55-56 ask the user to enter plaintext. LOC 58 reads this plaintext and stores it in the variable msgOriginal. LOCs 60-61 consist of a for loop. In this for loop the plaintext is converted into a ciphertext according to simple substitution cipher logic. LOC 63 displays the encrypted message on the screen .

LOCs 65-75 consist of definition of the function decryptMsg(). This function retrieves the plaintext from the ciphertext. LOC 67 asks the user to enter the ciphertext. The ciphertext entered by user is read and stored in the variable msgEncrypt in LOC 69. LOCs 71-72 consist of a for loop which retrieves the plaintext from the ciphertext and stores it in msgDecrypt. In LOC 74, the decrypted message is displayed on the screen.

LOCs 76-93 consist of definition of the function main(). LOC 78 consists of a call to the function generateKey(). LOCs 79-92 consist of a do-while loop. This seems to be an infinite loop, however, function exit() in the LOC 90 effectively stops the execution of this loop. LOC 80 calls the function menu() which displays the menu for users on the screen. The choice entered by user is stored in the int variable intChoice. LOCs 81-91 consist of a switch statement. Value stored in intChoice is passsed to this statement. If value of intChoice is 1 then function encryptMsg() is called. If value of intChoice is 2 then function decryptMsg() is called. If value of intChoice is else then function exit() is called which terminates the execution of do-while loop and also terminates the execution of this program .

10-7. Use the Vigenère Cipher Method

Problem

You want to implement a cryptographic system using the Vigenère cipher method.

Merits:

  • Level of security offered is better than Caesar cipher method. For about 300 years, this cipher was believed to be unbreakable, however, Charles Babbage and Friedrich Kasiski independently invented a method of breaking it in the middle of the nineteenth century

  • Not very difficult to implement and debug

  • Combines the benefits of Caesar cipher method and multiple keys

Demerits:

  • Logic used is not very simple

  • Ciphertext can be deciphered using brute force technique with a very high speed computer

  • Transportation of keys securely is difficult

Solution

Write a C program that implements a cryptographic system using the Vigenère cipher method, with the following specifications:

  • Program defines the functions: (a) getKey() to accept text key from user, (b) menu() to display menu for users on the screen, (c) encryptMsg() to encrypt the plaintext, and (d) decryptMsg() to decrypt the ciphertext.

  • Program also defines the char type arrays msgOriginal, msgEncrypt, and msgDecrypt to store the messages. Array should accommodate 100 characters.

The Code

Code of C program written with these specifications is given below. Type the following C program in a text editor and save it in the folder C:\Code with the filename crypt7.c:

/* This program implements a cryptographic system using the Vigenere cipher method. */                                                                    /* BL */ #include<stdio.h>                                                  /* L1 */ #include <string.h>                                                /* L2 */                                                                    /* BL */ char msgOriginal[100];                                             /* L3 */ char msgEncrypt[100];                                              /* L4 */ char msgDecrypt[100];                                              /* L5 */ char msgKey[15];                                                   /* L6 */ int intChoice, lenKey, lenMsg, intKey[15];                         /* L7 */                                                                    /* BL */ void getKey()                                                      /* L8 */ {                                                                  /* L9 */   int i, j;                                                        /* L10 */   fflush(stdin);                                                   /* L11 */   printf("\nEnter TEXT KEY in FULL CAPS, Do Not Include Spaces and \n"); /* L12 */   printf("Punctuation Symbols (upto 15 characters): \n");          /* L13 */   gets(msgKey);                                                    /* L14 */   lenKey = strlen(msgKey);                                         /* L15 */   for(i = 0; i < lenKey; i++)                                      /* L16 */         intKey[i] = msgKey[i] - 65;                                /* L17 */ }                                                                  /* L18 */                                                                    /* BL */ void menu()                                                        /* L19 */ {                                                                  /* L20 */   printf("\nEnter 1 to Encrypt a Message.");                       /* L21 */   printf("\nEnter 2 to Decrypt an Encrypted Message.");            /* L22 */   printf("\nEnter 3 to Stop the Execution of Program.");           /* L23 */   printf("\nNow Enter Your Choice (1, 2 or 3) and Strike Enter Key: "); /* L24 */   scanf("%d", &intChoice);                                         /* L25 */ }                                                                  /* L26 */                                                                    /* BL  */ void encryptMsg()                                                  /* L27 */ {                                                                  /* L28 */   int i, j, ch;                                                    /* L29 */   fflush(stdin);                                                   /* L30 */   printf("Enter the Message to be Encrypted (upto 100 alphabets), "); /* L31 */   printf("do not include \nspaces and punctuation symbols:\n");    /* L32 */   gets(msgOriginal);                                               /* L33 */   lenMsg = strlen(msgOriginal);                                    /* L34 */     for(i = 0; i < lenMsg; i++) {                                  /* L35 */       j = i % lenKey;                                              /* L36 */       ch = msgOriginal[i];                                         /* L37 */       if(ch >= 'a' && ch <= 'z') {                                 /* L38 */         ch = ch + intKey[j];                                       /* L39 */         if(ch > 'z')                                               /* L40 */           ch = ch - 'z' + 'a' - 1;                                 /* L41 */          msgEncrypt[i] = ch;                                       /* L42 */       }                                                            /* L43 */       else if(ch >= 'A' && ch <= 'Z'){                             /* L44 */         ch = ch + intKey[j];                                       /* L45 */         if(ch > 'Z')                                               /* L46 */           ch = ch - 'Z' + 'A' - 1;                                 /* L47 */         msgEncrypt[i] = ch;                                        /* L48 */       }                                                            /* L49 */     }                                                              /* L50 */   msgEncrypt[lenMsg] = '\0';                                       /* L51 */   printf("\nEncrypted Message: %s", msgEncrypt);                   /* L52 */ }                                                                  /* L53 */                                                                    /* BL  */ void decryptMsg()                                                  /* L54 */ {                                                                  /* L55 */   int i, j, ch;                                                    /* L56 */   fflush(stdin);                                                   /* L57 */   printf("Enter the Message to be Decrypted (upto 100 alphabets):\n"); /* L58 */   gets(msgEncrypt);                                                /* L59 */   lenMsg = strlen(msgEncrypt);                                     /* L60 */   for(i = 0; i < lenMsg; i++) {                                    /* L61 */     j = i % lenKey;                                                /* L62 */     ch = msgEncrypt[i];                                            /* L63 */     if(ch >= 'a' && ch <= 'z') {                                   /* L64 */       ch = ch - intKey[j];                                         /* L65 */       if(ch < 'a')                                                 /* L66 */         ch = ch + 'z' - 'a' + 1;                                   /* L67 */       msgDecrypt[i] = ch;                                          /* L68 */     }                                                              /* L69 */     else if(ch >= 'A' && ch <= 'Z'){                               /* L70 */       ch = ch - intKey[j];                                         /* L71 */       if(ch < 'A')                                                 /* L72 */         ch = ch + 'Z' - 'A' + 1;                                   /* L73 */       msgDecrypt[i] = ch;                                          /* L74 */     }                                                              /* L75 */   }                                                                /* L76 */   msgDecrypt[lenMsg] = '\0';                                       /* L77 */   printf("\nDecrypted Message: %s", msgDecrypt);                   /* L78 */ }                                                                  /* L79 */                                                                    /* BL  */ void main()                                                        /* L80 */ {                                                                  /* L81 */   getKey();                                                        /* L82 */   do {                                                             /* L83 */     menu();                                                        /* L84 */     switch (intChoice) {                                           /* L85 */       case 1:                                                      /* L86 */                encryptMsg();                                       /* L87 */                break;                                              /* L88 */       case 2:                                                      /* L89 */                decryptMsg();                                       /* L90 */                break;                                              /* L91 */       default:                                                     /* L92 */                printf("\nThank you.\n");                           /* L93 */                exit(0);                                            /* L94 */     }                                                              /* L95 */   } while (1);                                                     /* L96 */ }                                                                  /* L97 */

Compile and execute this program. A run of this program is given below :

Enter TEXT KEY in FULL CAPS, Do Not Include Spaces and Punctuation Symbols (upto 15 characters): WORLDPEACE    Enter 1 to Encrypt a Message. Enter 2 to Decrypt an Encrypted Message. Enter 3 to Stop the Execution of Program. Now Enter Your Choice (1, 2, or 3) and Strike Enter Key: 1    Enter the Message to be Encrypted (upto 100 alphabets), do not include Spaces and punctuation symbols: CandCryptographyIsVeryPowerfulCombination    Encrypted Message: YoeoFgcpvscfraknMsXinmGzztvfwpYcdmlcetksj Enter 1 to Encrypt a Message. Enter 2 to Decrypt an Encrypted Message. Enter 3 to Stop the Execution of Program. Now Enter Your Choice (1, 2, or 3) and Strike Enter Key: 2    Enter the Message to be Decrypted (upto 100 alphabets): YoeoFgcpvscfraknMsXinmGzztvfwpYcdmlcetksj    Decrypted Message: CandCryptographyIsVeryPowerfulCombination Enter 1 to Encrypt a Message. Enter 2 to Decrypt an Encrypted Message. Enter 3 to Stop the Execution of Program. Now Enter Your Choice (1, 2, or 3) and Strike Enter Key: 3    Thank you.

How It Works

Firstly, let us discuss the working of Vigenère cipher. Vigenère cipher is nothing but the Caesar cipher with multiple keys. It carries the name of Italian cryptographer Blaise de Vigenère. However, most possibly, it was inveted by another Italian cryptographer Giovan Battista Bellaso. As it uses multiple keys, it is also called Polyalphabetic Substitution cipher. Let the word CAT be the Vigenere cipher key. The letters in A to Z are serially numbered as 0 to 25, respectively, as shown in figure 10-2. Here, letter C means key is 2, letter A means key 0, and letter T means key is 19. During encryption and decryption these keys are used in cyclical order. During encryption, first letter in plaintext is encrypted using the key = 2, second letter in plaintext is encrypted using the key = 0, third letter in plaintext is encrypted using the key = 19, fourth letter in plaintext is encrypted using the key = 2, and so on. During decryption also these keys are used in the same order.

For simplicity, let us form a sample plaintext using only capital alphabets. In program, however, provision is made for both upper and lower case letters. Suppose plaintext is COMPUTER and let us encrypt it using the key CAT. The keys will be used in the following order:

Plaintext    =     C     O     M     P     U     T     E     R Keys         =     2     0     19    2     0     19    2     0 Ciphertext   =     E     O     F     R     U     M     G     R

The first letter in plaintext is C and its serial number is 2, also key is 2, therefore it encrypts to (serial number of C + key = 2 + 2 = 4 =) E. Second letter in plaintext is O and its serial number is 14, also key is 0, therefore and it encrypts to (serial number of O + key = 14 + 0 = 14 =) O. Third letter in plaintext is M and its serial number is 12, also key is 19, therefore it encrypts to (serial number of M + key = 12 + 19 = 31 = 31 - 26 = 5 =) F. Fourth letter in plaintext is P and its serial number is 15, also key is 2, therefore it encrypts to (serial number of P + key = 15 + 2 = 17 =) R. Proceeding in this manner, we get the ciphertext EOFRUMGR. You can retrieve the plaintext from ciphertext simply by proceeding in the reverse manner.

Now let us discuss the working of this program. LOCs 3-7 consist of variable declaration. In LOCs 3-6 four char type arrays are declared, namely, msgOriginal, msgEncrypt, msgDecrypt, and msgKey. The size of first three char arrays is 100 and that of fourth array is 15. In LOC 7, an int type array intKey of size 15 is declared. Also, in LOC 7, few int type variables are declared .

LOCs 8-18 consist of definition of the function getKey(). This function accepts text key from user. LOCs 12-13 ask the user to enter the text key in upper case. Text key entered by user is read in LOC 14 and stored in the variable msgKey. LOCs 16-17 consist of a for loops and in this loop serial numbers of letters in msgKey are filled in the array intKey.

LOCs 19-26 consist of definition of function menu(). This function displays menu for users on the screen so that various options in this program can be used conveniently. LOCs 21-24 ask the user to enter an appropriate choice and also describe the various options available to user. LOC 25 reads the choice entered by user and stores this choice in the int variable intChoice.

LOCs 27-53 consist of definition of the function encryptMsg(). This function converts the plaintext into ciphertext. LOCs 31-32 ask the user to enter plaintext. LOC 33 reads this plaintext and stores it in the variable msgOriginal. LOCs 35-50 consist of a for loop. In this for loop the plaintext is converted into a ciphertext according to Vigenere cipher logic. LOC 52 displays the encrypted message on the screen.

LOCs 54-79 consist of definition of the function decryptMsg(). This function retrieves the plaintext from the ciphertext. LOC 58 asks the user to enter the ciphertext. The ciphertext entered by user is read and stored in the variable msgEncrypt in LOC 59. LOCs 61-76 consist of a for loop which retrieves the plaintext from the ciphertext and stores it in msgDecrypt. In LOC 78, the decrypted message is displayed on the screen.

LOCs 80-97 consist of definition of the function main(). LOC 82 consists of a call to the function getKey(). LOCs 83-96 consist of a do-while loop. This seems to be an infinite loop, however, function exit() in the LOC 94 effectively stops the execution of this loop. LOC 84 calls the function menu() which displays the menu for users on the screen. The choice entered by user is stored in the int variable intChoice. LOCs 85-95 consist of a switch statement. Value stored in intChoice is passsed to this statement. If value of intChoice is 1 then function encryptMsg() is called. If value of intChoice is 2 then function decryptMsg() is called. If value of intChoice is else then function exit() is called which terminates the execution of do-while loop and also terminates the execution of this program .

10-8. Use the One-Time Pad Cipher Method

Problem

You want to implement a cryptographic system using the One-Time Pad cipher method.

Merits:

  • Almost unbreakable cipher; breaks only if key is compromised otherwise it is unbreakable

  • Based on simple logic

  • System requirement is not very high

Demerits:

  • Key is as long as plaintext hence generation of key is time consuming if message is very long

  • Key needs to be generated on the “use and throw” basis

  • Due to abnormal size of key, handling and storage of key is troublesome

  • Transportation of key securely is very difficult compared to other methods

Solution

Write a C program that implements a cryptographic system using the One-Time Pad cipher method, with the following specifications:

  • Program defines the functions: (a) generateKey() to generate the key, (b) menu() to display menu for users on the screen, (c) encryptMsg() to encrypt the plaintext, and (d) decryptMsg() to decrypt the ciphertext.

  • Program also defines the char type arrays msgOriginal, msgEncrypt, and msgDecrypt to store the messages. Array should accommodate 100 characters .

The Code

Code of C program written with these specifications is given below. Type the following C program in a text editor and save it in the folder C:\Code with the filename crypt8.c:

/* This program implements a cryptographic system using the One-Time Pad cipher method. */                                                                    /* BL */ #include <stdio.h>                                                 /* L1 */ #include<string.h>                                                 /* L2 */                                                                    /* BL */ char msgOriginal[100];                                             /* L3 */ char msgEncrypt[100];                                              /* L4 */ char msgDecrypt[100];                                              /* L5 */ char msgKey[100];                                                  /* L6 */ int intChoice, lenKey, lenMsg, intKey[100];                        /* L7 */                                                                    /* BL */ void generateKey()                                                 /* L8 */ {                                                                  /* L9 */   int i, randNum, num, seed;                                       /* L10 */   lenKey = lenMsg;                                                 /* L11 */   printf("\nEnter seed S (1 <= S <= 30000): ");                    /* L12 */   scanf("%d", &seed);                                              /* L13 */   srand(seed);                                                     /* L14 */   for(i = 0; i < lenKey; i++) {                                    /* L15 */       randNum = rand();                                            /* L16 */       num = randNum % 26;                                          /* L17 */       msgKey[i] = num + 65;                                        /* L18 */       intKey[i] = num;                                             /* L19 */   }                                                                /* L20 */   msgKey[lenKey] = '\0';                                           /* L21 */   printf("\nKey: %s", msgKey);                                     /* L22 */ }                                                                  /* L23 */                                                                    /* BL */ void menu()                                                        /* L24 */ {                                                                  /* L25 */   printf("\nEnter 1 to Encrypt a Message.");                       /* L26 */   printf("\nEnter 2 to Stop the Execution of Program.");           /* L27 */   printf("\nNow Enter Your Choice (1 or 2) and Strike Enter Key: "); /* L28 */   scanf("%d", &intChoice);                                         /* L29 */ }                                                                  /* L30 */                                                                    /* BL  */ void encryptMsg()                                                  /* L31 */ {                                                                  /* L32 */   int i, j, ch;                                                    /* L33 */   fflush(stdin);                                                   /* L34 */   printf("Enter the Message to be Encrypted (upto 100 alphabets), ");  /* L35 */   printf("Do Not Include \nSpaces and Punctuation Symbols:\n");    /* L36 */   gets(msgOriginal);                                               /* L37 */   lenMsg = strlen(msgOriginal);                                    /* L38 */   generateKey();                                                   /* L39 */     for(i = 0; i < lenMsg; i++) {                                  /* L40 */       ch = msgOriginal[i];                                         /* L41 */       if(ch >= 'a' && ch <= 'z') {                                 /* L42 */         ch = ch + intKey[i];                                       /* L43 */         if(ch > 'z')                                               /* L44 */           ch = ch - 'z' + 'a' - 1;                                 /* L45 */          msgEncrypt[i] = ch;                                       /* L46 */       }                                                            /* L47 */       else if(ch >= 'A' && ch <= 'Z'){                             /* L48 */         ch = ch + intKey[i];                                       /* L49 */         if(ch > 'Z')                                               /* L50 */           ch = ch - 'Z' + 'A' - 1;                                 /* L51 */         msgEncrypt[i] = ch;                                        /* L52 */       }                                                            /* L53 */     }                                                              /* L54 */   msgEncrypt[lenMsg] = '\0';                                       /* L55 */   printf("\nEncrypted Message: %s", msgEncrypt);                   /* L56 */ }                                                                  /* L57 */                                                                    /* BL  */ void decryptMsg()                                                  /* L58 */ {                                                                  /* L59 */   int i, j, ch;                                                    /* L60 */   fflush(stdin);                                                   /* L61 */   printf("\nEnter the Message to be Decrypted (upto 100 alphabets):\n"); /* L62 */   gets(msgEncrypt);                                                /* L63 */   lenMsg = strlen(msgEncrypt);                                     /* L64 */   for(i = 0; i < lenMsg; i++) {                                    /* L65 */     ch = msgEncrypt[i];                                            /* L66 */     if(ch >= 'a' && ch <= 'z') {                                   /* L67 */       ch = ch - intKey[i];                                         /* L68 */       if(ch < 'a')                                                 /* L69 */         ch = ch + 'z' - 'a' + 1;                                   /* L70 */       msgDecrypt[i] = ch;                                          /* L71 */     }                                                              /* L72 */     else if(ch >= 'A' && ch <= 'Z'){                               /* L73 */       ch = ch - intKey[i];                                         /* L74 */       if(ch < 'A')                                                 /* L75 */         ch = ch + 'Z' - 'A' + 1;                                   /* L76 */       msgDecrypt[i] = ch;                                          /* L77 */     }                                                              /* L78 */   }                                                                /* L79 */   msgDecrypt[lenMsg] = '\0';                                       /* L80 */   printf("\nDecrypted Message: %s", msgDecrypt);                   /* L81 */ }                                                                  /* L82 */                                                                    /* BL  */ void main()                                                        /* L83 */ {                                                                  /* L84 */   do {                                                             /* L85 */     menu();                                                        /* L86 */     switch (intChoice) {                                           /* L87 */       case 1:                                                      /* L88 */                encryptMsg();                                       /* L89 */                decryptMsg();                                       /* L90 */                break;                                              /* L91 */       default:                                                     /* L92 */                printf("\nThank you.\n");                           /* L93 */                exit(0);                                            /* L94 */     }                                                              /* L95 */   } while (1);                                                     /* L96 */ }                                                                  /* L97 */

Compile and execute this program. A run of this program is given below:

Enter 1 to Encrypt a Message. Enter 2 to Stop the Execution of Program. Now Enter Your Choice (1 or 2) and Strike Enter Key: 1    Enter the Message to be Encrypted (upto 100 alphabets), Do Not Include Spaces and Punctuation Symbols: CandCryptographyIsVeryPowerfulCombination    Enter seed S (1 <= S <= 30000): 2000 Key: KJVWBAIZRUHRKNAFFHRBXNKUGHDMJHTDKLRWTZVMZ Encrypted Message: MjizDrgokinikchdNzMfolZiclurdsVrwmzjtsdam Enter the Message to be Decrypted (upto 100 alphabets): MjizDrgokinikchdNzMfolZiclurdsVrwmzjtsdam    Decrypted Message: CandCryptographyIsVeryPowerfulCombination Enter 1 to Encrypt a Message. Enter 2 to Stop the Execution of Program. Now Enter Your Choice (1 or 2) and Strike Enter Key: 2    Thank you.  

How It Works

Firstly, let us discuss the working of One-Time Pad cipher. One-Time Pad cipher is impossible to crack. It is a Vigenère cipher with the following modifications:

  • The text key is exactly as long as the plaintext.

  • The text key is made simply by placing the randomly picked characters, one by one.

  • The text key is generated on “use and throw” basis. The key once used is never used again.

Suppose, the plaintext is COMPUTER. Now to encrypt this plaintext you are required to generate the key that is eight characters long and it consists of randomly picked characters. Let the text key be BDLVACFX. The keys corresponding to these letters are given below (see also Figure 10-2):

Letters in Text Key   =    B    D    L    V    A    C    F    X Keys                  =    1    3    11   21   0    2    5    23

Using these keys, the plaintext COMPUTER is encrypted as follows:

Plaintext             =    C    O    M    P    U    T    E    R Keys                  =    1    3    11   21   0    2    5    23 Ciphertext            =    D    R    X    K    U    V    J    O

The plaintext COMPUTER is encrypted to DRXKUVJO. Decryption is simply reverse of the encryption.

Now let us discuss the working of this program. LOCs 3-7 consist of variable declaration. In LOCs 3-6 four char type arrays, each of size 100, are declared, namely, msgOriginal, msgEncrypt, msgDecrypt, and msgKey. In LOC 7, an int type array intKey of size 100 is declared. Also, in LOC 7, few int type variables are declared.

LOCs 8-23 consist of definition of the function generateKey(). This function generates the key. The length of key is same as that of plaintext, i.e., msgOriginal. LOC 12 asks the user to enter the “seed” which is nothing but an integer in the range 1 to 30,000. The “seed” entered by user is read in the LOC 13 and stored in the int variable seed. LOCs 15-20 consist of a for loop in which the key is generated and stored in the variable msgKey. The key consist of only upper case letters. LOC 22 displays the key on the screen.

LOCs 24-30 consist of definition of function menu(). This function displays menu for users on the screen so that various options in this program can be used conveniently. LOCs 26-28 ask the user to enter an appropriate choice and also describe the various options available to user. LOC 29 reads the choice entered by user and stores this choice in the int variable intChoice.

LOCs 31-57 consist of definition of the function encryptMsg(). This function converts the plaintext into ciphertext. LOCs 35-36 ask the user to enter plaintext. LOC 37 reads this plaintext and stores it in the variable msgOriginal. LOC 38 computes the length of msgOriginal. LOC 39 calls the function generateKey(). LOCs 40-54 consist of a for loop. In this for loop the plaintext is converted into a ciphertext according to One-Time Pad cipher logic. LOC 56 displays the encrypted message on the screen.

LOCs 58-82 consist of definition of the function decryptMsg(). This function retrieves the plaintext from the ciphertext. LOC 62 asks the user to enter the ciphertext. The ciphertext entered by user is read and stored in the variable msgEncrypt in LOC 63. LOCs 65-79 consist of a for loop which retrieves the plaintext from the ciphertext and stores it in msgDecrypt. In LOC 81, the decrypted message is displayed on the screen.

LOCs 83-97 consist of definition of the function main(). LOCs 85-96 consist of a do-while loop. This seems to be an infinite loop, however, function exit() in the LOC 94 effectively stops the execution of this loop. LOC 86 calls the function menu() which displays the menu for users on the screen. The choice entered by user is stored in the int variable intChoice. LOCs 87-95 consist of a switch statement. Value stored in intChoice is passsed to this statement. If value of intChoice is 1 then case 1 is activated and functions encryptMsg() and decryptMsg() are called in succession. If value of intChoice is else then function exit() is called which terminates the execution of do-while loop and also terminates the execution of this program .

10-9. Use the RSA Cipher Method

Problem

You want to implement a cryptographic system using the RSA cipher method.

Merits:

  • Almost unbreakable cipher

  • Being public key cryptographic system, problem of transportation of key securely is done away with

  • This cipher - or any public key cipher - provides digital signatures that cannot be repudiated

Demerits:

  • Uses complex arithmetic and hence difficult to implement and debug

  • System requirement is high

  • Implementation of this cipher may not be economical

  • Program execution is slow

Solution

Write a C program that implements a cryptographic system using the RSA cipher method, with the following specifications:

  • Program defines the functions prime(), findPrime(), computeKeys(), cd(), encryptMsg(), and decryptMsg(). Function prime() detects whether a given integer is prime or not. Function findPrime() finds the nth prime number. Functions cd() and computeKeys() together find the permissible values of d and e. Function encryptMsg() converts the plaintext into ciphertext. Function decryptMsg() retrieves the plaintext from ciphertext.

  • Program defines the char type array msgOriginal, and int type arrays d, e, temp, msgEncrypt, and msgDecrypt. Size of all arrays should be 100.

The Code

Code of C program written with these specifications is given below. Type the following C program in a text editor and save it in the folder C:\Code with the filename crypt9.c:

/* This program implements a cryptographic system using the RSA cipher method. */                                                                   /* BL */ #include <stdio.h>                                                /* L1 */ #include <math.h>                                                 /* L2 */ #include <string.h>                                               /* L3 */                                                                   /* BL */ long int i, j, p, q, n, t, flag;                                  /* L4 */ long int e[100], d[100], temp[100], msgDecrypt[100], msgEncrypt[100]; /* L5 */ char msgOriginal[100];                                            /* L6 */ int prime(long int);                                              /* L7 */ int findPrime(long int s);                                        /* L8 */ void computeKeys();                                               /* L9 */ long int cd(long int);                                            /* L10 */ void encryptMsg();                                                /* L11 */ void decryptMsg();                                                /* L12 */                                                                   /* BL  */ void main() {                                                     /* L13 */   long int s;                                                     /* L14 */   do{                                                             /* L15 */     printf("Enter the serial number S of 1st prime number (10 <= S <= 40): "); /* L16 */     scanf("%ld", &s) ;                                            /* L17 */   } while ((s < 10) || (s > 40));                                 /* L18 */   p = findPrime(s);                                               /* L19 */   printf("First prime number p is: %d \n", p) ;                   /* L20 */   do{                                                             /* L21 */     printf("Enter the serial number S of 2nd prime number (10 <= S <= 40): "); /* L22 */     scanf("%ld", &s) ;                                            /* L23 */   } while ((s < 10) || (s > 40));                                 /* L24 */   q = findPrime(s);                                               /* L25 */   printf("Second prime number q is: %d \n", q) ;                  /* L26 */   printf("\nEnter the Message to be Encrypted, Do Not Include Spaces:\n"); /* L27 */   fflush(stdin);                                                  /* L28 */   scanf("%s",msgOriginal);                                        /* L29 */   for (i = 0; msgOriginal[i] != NULL; i++)                        /* L30 */     msgDecrypt[i] = msgOriginal[i];                               /* L31 */   n = p * q;                                                      /* L32 */   t = (p - 1) * (q - 1);                                          /* L33 */   computeKeys();                                                  /* L34 */   printf("\nPossible Values of e and d Are:\n");                  /* L35 */   for (i = 0; i < j - 1; i++)                                     /* L36 */     printf("\n %ld \t %ld", e[i], d[i]);                          /* L37 */   printf("\nSample Public Key: (%ld,  %ld)", n, e[i-1]);          /* L38 */   printf("\nSample Private Key: (%ld,  %ld)", n, d[i-1]);         /* L39 */   encryptMsg();                                                   /* L40 */   decryptMsg();                                                   /* L41 */ }                                                                 /* L42 */                                                                   /* BL  */ int findPrime(long int s)                                         /* L43 */ {                                                                 /* L44 */   int f, d, tag;                                                  /* L45 */   f = 2;                                                          /* L46 */   i = 1;                                                          /* L47 */   while(i <= s){                                                  /* L48 */     tag = 1;                                                      /* L49 */     for(d = 2 ; d <= f-1 ; d++){                                  /* L50 */       if(f % d == 0) {                                            /* L51 */         tag = 0;                                                  /* L52 */         break ;                                                   /* L53 */       }                                                           /* L54 */     }                                                             /* L55 */     if(tag == 1) {                                                /* L56 */       if (i == s)                                                 /* L57 */         return(f);                                                /* L58 */       i++ ;                                                       /* L59 */     }                                                             /* L60 */     f++ ;                                                         /* L61 */   }                                                               /* L62 */   return(0);                                                      /* L63 */ }                                                                 /* L64 */                                                                   /* BL  */ int prime(long int pr)                                            /* L65 */ {                                                                 /* L66 */   int i;                                                          /* L67 */   j=sqrt(pr);                                                     /* L68 */   for (i = 2; i <= j; i++) {                                      /* L69 */     if(pr % i == 0)                                               /* L70 */       return 0;                                                   /* L71 */   }                                                               /* L72 */   return 1;                                                       /* L73 */ }                                                                 /* L74 */                                                                   /* BL  */ void computeKeys()                                                /* L75 */ {                                                                 /* L76 */   int k;                                                          /* L77 */   k = 0;                                                          /* L78 */   for (i = 2; i < t; i++) {                                       /* L79 */     if(t % i == 0)                                                /* L80 */       continue;                                                   /* L81 */     flag = prime(i);                                              /* L82 */     if(flag == 1 && i != p && i != q) {                           /* L83 */       e[k] = i;                                                   /* L84 */       flag = cd(e[k]);                                            /* L85 */         if(flag > 0) {                                            /* L86 */           d[k] = flag;                                            /* L87 */           k++;                                                    /* L88 */         }                                                         /* L89 */         if(k == 99)                                               /* L90 */         break;                                                    /* L91 */     }                                                             /* L92 */   }                                                               /* L93 */ }                                                                 /* L94 */                                                                   /* BL  */ long int cd(long int x)                                           /* L95 */ {                                                                 /* L96 */   long int k = 1;                                                 /* L97 */   while(1) {                                                      /* L98 */     k = k + t;                                                    /* L99 */     if(k % x == 0)                                                /* L100 */       return(k/x);                                                /* L101 */   }                                                               /* L102 */ }                                                                 /* L103 */                                                                   /* BL   */ void encryptMsg()                                                 /* L104 */ {                                                                 /* L105 */   long int pt, ct, key = e[0], k, length;                         /* L106 */   i = 0;                                                          /* L107 */   length = strlen(msgOriginal);                                   /* L108 */   while(i != length) {                                            /* L109 */     pt = msgDecrypt[i];                                           /* L110 */     pt = pt-96;                                                   /* L111 */     k = 1;                                                        /* L112 */     for (j = 0; j < key; j++) {                                   /* L113 */       k = k * pt;                                                 /* L114 */       k = k % n;                                                  /* L115 */     }                                                             /* L116 */     temp[i] = k;                                                  /* L117 */     ct = k + 96;                                                  /* L118 */     msgEncrypt[i] = ct;                                           /* L119 */     i++;                                                          /* L120 */   }                                                               /* L121 */   msgEncrypt[i] =- 1;                                             /* L122 */   printf("\nThe Encrypted Message:\n");                           /* L123 */   for (i = 0; msgEncrypt[i] != -1; i++)                           /* L124 */     printf("%c", msgEncrypt[i]);                                  /* L125 */ }                                                                 /* L126 */                                                                   /* BL   */ void decryptMsg()                                                 /* L127 */ {                                                                 /* L128 */   long int pt, ct, key = d[0], k;                                 /* L129 */   i = 0;                                                          /* L130 */   while(msgEncrypt[i] != -1) {                                    /* L131 */     ct = temp[i];                                                 /* L132 */     k = 1;                                                        /* L133 */     for (j = 0; j < key; j++) {                                   /* L134 */       k = k * ct;                                                 /* L135 */       k = k % n;                                                  /* L136 */     }                                                             /* L137 */     pt = k + 96;                                                  /* L138 */     msgDecrypt[i] = pt;                                           /* L139 */     i++;                                                          /* L140 */   }                                                               /* L141 */   msgDecrypt[i] =- 1;                                             /* L142 */   printf("\nThe Decrypted Message:\n");                           /* L143 */   for (i = 0; msgDecrypt[i] != -1; i++)                           /* L144 */     printf("%c", msgDecrypt[i]);                                  /* L145 */   printf("\nThank you. \n ");                                     /* L146 */ }                                                                 /* L147 */

Compile and execute this program. A run of this program is shown in Figure 10-3.

Figure 10-3.
figure 3

A sample run of the program crypt9. A part of the output is cropped out to save the space.

How It Works

Firstly, let us discuss the working of RSA cipher. All the preceding cryptographic systems are known as private key cryptographic systems. In private key cryptogaphy, you are required to send to receiver of message: (a) ciphertext and (b) secret key. But when you send ciphertext alongwith a secret key then very purpose of ciphering is challenged. Because anyone with a key can decrypt the ciphertext. In practice, when when both parties (sender and receiver) agree to use private key cryptography then they personally meet to share the secret key, and then only ciphertext is sent to receiving party, time to time.

The problem of sharing the secret key is solved by public key cryptographic systems. The very first such system is called RSA cipher. It is also most popular cryptographic system. It was first described by Ron Rivest, Adi Shamir and Leonard Adleman in 1977, hence the name RSA (R for Ron, S for Shamir and A for Adleman).

Public key cipher has two keys, one for encryption and other for decryption. Private key cipher has only one key that is used for encryption as well as decryption. All the preceding cryptographic systems are private key cryptographic systems. In some of the preceding programs, there is mention of two keys, one for encryption and another for decryption (for example, Recipe 10.6, Simple Substitution cipher). But actually it is only one key and decryption key is nothing but encryption key in another suitable form.

Private Key cipher is also called as Symmetric cipher and Public Key cipher is also called as Asymmetric cipher. In Public Key cipher, encryption key is called public key and decryption key is called private key. Public key is shared with all, however, private key is secret and it is in possession of the receiver (of the message) only. Thus public key is used for encryption and private key is used for decryption.

In RSA cipher, the generic procedure of encryption and decryption is as follows:

  • Create two very large prime numbers randomly. These numbers are called p and q.

  • Multiply p by q and the result is called n. Therefore, n = p * q.

  • Calculate the product (p - 1) * (q - 1) and call it t. Therefore, t = (p - 1) * (q - 1).

  • Create a random number e such that e is relatively prime with t. Also, 1 < e < t.

  • Calculate the modular inverse of (e % t) and call it d. It means, find the value d such that (d * e) % t = 1. Also d < t.

  • Public key is (e, n) and private key is (d, n).

  • Let letter M from plaintext is encrypted to letter C. It is done as follows: C = Me % n.

  • Letter C from ciphertext is decrypted back to M. It is done as follows: M = Cd % n.

The RSA cipher derives its strenght from the fact that if two large prime numbers are multiplied then the resulting number is difficult to factorize.

Now let us discuss the working of this program. LOCs 4-6 consist of variable declaration. LOC 4 declares few long int type variables. LOC 5 declares five long int type arrays. LOC 6 declares a char type array msgOriginal. The size of all arrays is 100. LOCs 7-12 consist of six function prototypes.

LOCs 13-42 consist of definition of the function main(). LOCs 15-18 consist of a do-while loop. This loop asks the user to enter the serial number S of the first prime number where S is an integer in the range 10 to 40. The number entered by user is read in the LOC 17 and is stored in the variable s. In LOC 19, call is made to function findPrime() and s is passed to it as an input argument. Function findPrime() finds the sth prime number and returns it and returned value is assigned to variable p. In LOC 20, the value of first prime number p is displayed on the screen. LOCs 21-26 contain the code that is similar to code in the LOCs 15-20. Only difference is that code in the LOCs 15-20 is related to first prime number p and code in the LOCs 21-26 is related to the second prime number q. LOC 27 asks the user to enter the plaintext. The plaintext entered by user is read in the LOC 28 and is assigned to the variable msgOriginal. LOCs 30-31 consist of a for loop that copies the array msgOriginal to array msgDecrypt. This is done to facilitate some computations in the function encryptMsg(). In function decryptMsg(), however, the contents of msgDecrypt are overwritten.

In LOC 32, value of n is computed. In LOC 33, value of t is computed. In LOC 34, function computeKeys() is called. Permissible values of e and d are displayed on the screen in the LOCs 35-37. Sample public key and sample private key are displayed on the screen in the LOCs 38-39. In the LOC 40, function encryptMsg() is called that converts plaintext into ciphertext. In the LOC 41, function decryptMsg() is called that retrieves the plaintext from the ciphertext.

LOCs 43-64 consist of definition of the function findPrime(). This function finds the sth prime number and s is passed to it as an input argument. First prime number is 2, second prime number is 3, third prime number is 5, and so on. This function starts with integer 2 (see LOC 46, variable f is used for this integer) and then goes to check every next integer for its primeness. If that integer is prime and its serial number is s then it is returned (see LOC 58).

LOCs 65-74 consist of definition of the function prime(). Long integer variable pr is passed to this function as an input argument. This function checks whether pr is prime number or not; if pr is prime number then it returns 1 (see LOC 73), otherwise it returns 0 (see LOC 71).

LOCs 75-94 consist of definition of the function computeKeys(). LOCs 95-103 consist of definition of the function cd(). These two functions together compute the permissible values of d and e using the standard formulae in RSA cipher method.

LOCs 104-126 consist of definition of the function encryptMsg(). This function converts the plaintext into ciphertext. In this function plaintext is converted into ciphertext using the standard formulae in RSA cipher method. Ciphertext is stored in the variable msgEncrypt. Encrypted message is displayed on the screen in the LOCs 123-125.

LOCs 127-147 consist of definition of the function decryptMsg(). This function retrieves the plaintext from the ciphertext using the standard formulae in RSA cipher method. Retrieved plaintext (i.e., decrypted message) is stored in variable msgDecrypt. Decrypted message is displayed on the screen in the LOCs 143-145 .