Monoalphabetic Cipher Program In C

When utilizing this software, you are required to input the unique secret key (consisting of 26 letters in a specific sequence) along with the message you wish to conceal. Subsequently, the software will iterate through your message, examining each letter individually, and modifying them according to the secret key. Notably, spaces and punctuation marks remain unaffected during this process.

The outcome of this procedure is the "ciphertext", which represents the encrypted form of your message. This encrypted data can be securely transmitted or kept confidential. It should be emphasized that monoalphabetic ciphers may provide enhanced security in contemporary contexts. These ciphers are commonly employed for educational purposes in cryptography or for historical significance, as they can be decoded by attackers with relative ease.

Example:

Let's consider a code snippet to demonstrate the implementation of the Monoalphabetic cipher program in the C programming language:

Example

#include <stdio.h>

#include <string.h>

int main() {

    char originalAlphabet[26] = "abcdefghijklmnopqrstuvwxyz";

    char replacementAlphabet[26];

printf("Enter the replacement alphabet (26 unique letters): ");

scanf("%s", replacementAlphabet);

    if (strlen(replacementAlphabet) != 26) {

printf("The replacement alphabet must contain 26 unique letters.\n");

        return 1;

    }

    int stringLength;

printf("Enter the length of the string you want to decrypt: ");

scanf("%d", &stringLength);



    char inputString[stringLength + 1]; 

printf("Enter the string you want to encrypt: ");

scanf("%s", inputString);

    char encryptedString[stringLength + 1];

    for (int i = 0; i<stringLength; i++) {

        char currentChar = inputString[i];

        if (currentChar>= 'a' &¤tChar<= 'z') {

            int index = currentChar - 'a';

encryptedString[i] = replacementAlphabet[index];

        } else {

            // If the character is not a lowercase letter, leave it unchanged

encryptedString[i] = currentChar;

        }

    }

encryptedString[stringLength] = '\0'; // Null-terminate the encrypted string

printf("Encrypted string: %s\n", encryptedString);

    return 0;

}

Output:

Output

Enter the replacement alphabet (26 unique letters): The replacement alphabet must contain 26 unique letters.
Enter the length of the string you want to decrypt: Enter the string you want to encrypt: Encrypted string: [value]

Explanation:

When we discuss the code, it describes a basic text encryption method called a substitution cipher. This cipher involves taking a message (text) and substituting each letter with a different letter from a confidential code. This process renders the message unintelligible unless the code is known.

Mapping of the alphabet with secret code:

Example

a->q         b->w        c->e        d->r       e->t      f->y    g->u  h->I          i->o         j->pk->al->sm->dn->fo->gp->hq->jr->ks->lt->zu->xv->cw->vx->by->nz->m

Setting Up the Secret Code:

The process starts with prompting you to enter a confidential key. This key must consist of 26 distinct characters, essentially representing your alphabet in a rearranged sequence. For instance, rather than "a, b, c, d...," your confidential key could be "qwertyuiopasdfghjklzxcvbnm."

Checking the Code:

It verifies that your confidential passcode consists of precisely 26 distinct letters. If this criterion is not met, it will indicate that the code must contain exactly 26 unique letters.

Getting the Message:

Next, you are required to input a message that you wish to encrypt. This can be any text that you desire to keep confidential.

Encrypting the Message:

Subsequently, the software processes your input letter by letter. Each individual letter is then examined to determine if it falls within the range of lowercase alphabets (a to z).

It examines the confidential code to determine the substitution letter in the case of a lowercase letter. For example, if the code specifies that 'a' is transformed into 'q', each occurrence of 'a' in your message will be replaced by 'q'.

If the character within your communication isn't a lowercase alphabetic character (such as a numeral or punctuation mark), it remains unchanged.

Creating the Encrypted Message:

The software generates an encoded rendition of your message by utilizing the substitutions it applied.

Displaying the Encrypted Message:

Finally, it displays the encrypted message, which represents your initial message after applying all the letter substitutions.

Conclusion:

The application functions as a text encryption tool that employs a substitution cipher technique. Initially, it prompts the user to input a unique set of 26 letters as a secret key, along with the message intended for encryption. During the encryption process, each lowercase letter in the message is substituted with its corresponding letter from the secret code, maintaining the rest of the characters unchanged. Consequently, the output is an encrypted rendition of the initial message. Despite its simplicity, this approach may not offer adequate security for contemporary needs due to the straightforward nature of the substitution pattern.

Input Required

This code uses input(). Please provide values below: