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?
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
Get Answers For Free
Most questions answered within 1 hours.