Question

clc; clear; % OneTimePad Cipher Encryption % Input: plainText % key, for encryption key as long...

clc;

clear; % OneTimePad Cipher Encryption % Input: plainText % key, for encryption key as long as plain text % Output: cipherText % plainText=upper('Hello') key=upper('XMCKL'); % process the plain text and key k1=double(key)-65; p1=double(plainText)-65; % Encrypt C1= p1+k1; C1= mod(C1,26)+65; %diaplay the cipher text CipherText=char(C1)

Does anyone no how I would go about making this encryption encrypt and decrypt using both uppercase and lower case letters?

Homework Answers

Answer #1

You have no way to mix and uppercase key with a lowercase character or vice versa, without making the cases match. You just have to make the key/character case-consistent for both encoding and decoding. You can do that by simply using the tolower or toupper conversions as you work through the encoding/decoding.

#include <iostream>
#include <string>

using namespace std;

int main (int argc, char **argv) {

string key = argc > 1 ? argv[1] : "LEMON",
line, /* input to encode */
cipher, /* encoded input */
decode; /* decoded cipher */
auto& k = key;
size_t kdx = 0; /* key index */

if (!k[kdx]) { /* validate key has at least one char */
cerr << "invalid key.\n";
return 1;
}

while (getline (cin, line)) { /* read each line of input */
/* encode line into cipher */
for (auto& c : line) { /* for each char in input */
if (islower(c)) /* if lower, force key lower */
cipher.push_back((tolower(k[kdx]) - 'a' + c - 'a') % 26 + 'a');
else if (isupper (c)) /* if upper, force key upper */
cipher.push_back((toupper(k[kdx]) - 'A' + c - 'A') % 26 + 'A');
else { /* otherwise -- character not supported */
cerr << "error: unsupported char '" << c << "' removing.\n";
continue;
}
kdx++; /* increment key index */
if (kdx == key.length()) /* if end of key, reset key index */
kdx = 0;
}
/* decode cipher into decode */
kdx = 0; /* reset key index */
for (auto& c : cipher) { /* for each char in cipher */
if (islower (c)) { /* if lower, force key lower */
int off = c - tolower (k[kdx]);
if (off >= 0) /* if offset >= 0, mod 26 */
decode.push_back (off % 26 + 'a');
else /* if offset < 0, + 26 */
decode.push_back (off + 26 + 'a');
}
else if (isupper (c)) { /* do the same for upper case */
int off = c - toupper (k[kdx]);
if (off >= 0)
decode.push_back (off % 26 + 'A');
else
decode.push_back (off + 26 + 'A');
}
else {
cerr << "error: invalid char in cipher '" << c << "'.\n";
return 1;
}
kdx++; /* increment key index */
if (kdx == key.length()) /* if end of key, reset key index */
kdx = 0;
}
cout << "input : " << line << '\n' <<
"key : " << key << '\n' <<
"cipher: " << cipher << '\n' <<
"decode: " << decode << '\n';

cipher.clear(); /* clear both cipher and decode */
decode.clear();
kdx = 0; /* reset key index */
}
}

Output :

$ echo "ATTACKATDAWN" | ./bin/vigenere
input : ATTACKATDAWN
key : LEMON
cipher: LXFOPVEFRNHR

$ echo "AttackAtDawn" | ./bin/vigenere lEMoN
input : AttackAtDawn
key : lEMoN
cipher: LxfopvEfRnhr
decode: AttackAtDawn

$ echo "Attack At Dawn" | ./bin/vigenere lEMoN
error: unsupported char ' ' removing.
error: unsupported char ' ' removing.
input : Attack At Dawn
key : lEMoN
cipher: LxfopvEfRnhr
decode: AttackAtDawn

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
C Programming I have this function to i want to return the cipher text, but its...
C Programming I have this function to i want to return the cipher text, but its not working, can anyone try to see what i'm doing wrong. I need it to return the cipher text. char* vigenereCipher(char *plainText, char *k) { int i; char cipher; int cipherValue; int len = strlen(k); char *cipherText = (char *)malloc(sizeof(plainText) * sizeof(char)); //Loop through the length of the plain text string for (i = 0; i < strlen(plainText); i++) { //if the character is...
USE C++!!!! Encryption and Decryption are two cryptographic techniques. Encryption is used to transform text to...
USE C++!!!! Encryption and Decryption are two cryptographic techniques. Encryption is used to transform text to meaningless characters, and decryption is used to transform meaningless characters into meaningful text. The algorithm that does the encryption is called a cipher. A simple encryption algorithm is Caesar cipher, which works as follows: replace each clear text letter by a letter chosen to be n places later in the alphabet. The number of places, n, is called the cipher key. For example, if...
Vigenère Cipher. In the Vigenère Cipher, we use a special keyword to encrypt a message. We...
Vigenère Cipher. In the Vigenère Cipher, we use a special keyword to encrypt a message. We represent each letter of the alphabet as a number from 0-25. In order, we add each letter from the keyword to the message and mod by 26. If the keyword is shorter than the message, we simply repeat the keyword. For example, lets say that the message is HOWSTUFFWORKS and the keyword is CIPHER. The following table shows how we can find the final...
1) Make a substitution table corresponding for the Monoalphabetic Substitution corresponding to the key (G, Twelfth...
1) Make a substitution table corresponding for the Monoalphabetic Substitution corresponding to the key (G, Twelfth Night). Enter the bottom row of your substitution (called the cipher alphabet) below. Your input should be 26 capital letters with no spaces. 2) Encrypt the following message with Monoalphabetic Substitution using key (Q, As You Like It). Enter your ciphertext in 4-grams in capital letters. Message: If music be the food of love play on. 3) Decrypt the following ciphertext created with Monoalphabetic...
NWS620S Tutorial 1: Symmetric Encryption - DES Encryption is the translation of data into a secret...
NWS620S Tutorial 1: Symmetric Encryption - DES Encryption is the translation of data into a secret code so that only authorised entities can read it. Encrypting data is considered a very effective way of achieving data security. To access encrypted data, you must have access to a secret key that enables you to decrypt it. Unencrypted data is called plain text; encrypted data is referred to as cipher text. There are two types of encryption: • Symmetric encryption • Asymmetric...
Below is an example of key generation, encryption, and decryption using RSA. For the examples below,...
Below is an example of key generation, encryption, and decryption using RSA. For the examples below, fill in the blanks to indicate what each part is or answer the question. Public key is (23, 11) What is 23 called? _______________, What is 11 called?_______________ Private key is (23, 13) What is 23 called?_______________, What is 13 called?_______________ 23 can be part of the public key because it is very hard to _______________ large prime numbers. ENCRYPT (m) = m^e mod...
USE C programming (pls label which file is libcipher.h and libcipher.c) Q4) A shift cipher is...
USE C programming (pls label which file is libcipher.h and libcipher.c) Q4) A shift cipher is one of the simplest encryption techniques in the field of cryptography. It is a cipher in which each letter in a plain text message is replaced by a letter some fixed number of positions up the alphabet (i.e., by right shifting the alphabetic characters in the plain text message). For example, with a right shift of 2, ’A’ is replaced by ’C’, ’B’ is...