Ceaser Cipher - small logic errors
Nov 14, 2011 at 11:34am UTC
My assignment was to write a Caeser Cipher which I have done. It compiles and runs fine in Code::Blocks however I have 2 small logic errors which are bugging me and I can't figure them out.
1. When the counter goes past the highest number on ascii chart it throws me an error when returning output. (e.g. encrypting "Nintendo" with a key of 24)
2. If the key is larger than 26 it throws off my whole algorithm. (e.g. decrypting "TekwbqiQtqci" with a key of 42)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
#include <iostream>
#include <cstring>
using namespace std;
//encryption function
void caesar_encrypt(char src[], int key, char dst[])
{
//convert string to ascii number
for (int counter = 0; counter < strlen(src); counter++)
{
//uppercase letter flag
if (src[counter] >= 65 && src[counter] <= 90)
{
//each letter adds the key to it
src[counter] = src[counter] + key;
//if the sum is > 90
if (src[counter] > 90)
{
//subtract 26 to bring it back to a letter
src[counter] = src[counter] - 26;
}
//if sum is < 65
else if (src[counter] < 65)
{
//add 26 to bring it back to a letter
src[counter] = (src[counter] + 26);
}
}
//lowercase letter flag
else if (src[counter] >= 97 && src[counter] <= 122)
{
src[counter] = src[counter] + key;
if (src[counter] > 122)
{
src[counter] = src[counter] - 26;
}
else if (src[counter] < 97)
{
src[counter] = src[counter] + 26;
}
}
}
cout << "Result:" << endl;
//convert string back to letter
for (int encrypter = 0; encrypter < strlen(src); encrypter++)
{
cout << src[encrypter];
}
cout << endl;
}
//decryption function
void caeser_decrypt(char src[], int key, char dst[])
{
for (int counter = 0; counter < strlen(src); counter++)
{
if (src[counter] >= 65 && src[counter] <= 90)
{
//subtract the key from letter
src[counter] = src[counter] - key;
if (src[counter] > 90)
{
src[counter] = src[counter] - 26;
}
else if (src[counter] < 65)
{
src[counter] = src[counter] + 26;
}
}
else if (src[counter] >= 97 && src[counter] <= 122)
{
src[counter] = src[counter] - key;
if (src[counter] > 122)
{
src[counter] = src[counter] - 26;
}
else if (src[counter] < 97)
{
src[counter] = src[counter] + 26;
}
}
}
cout << "Result:" << endl;
for (int encrypter = 0; encrypter < strlen(src); encrypter++)
{
cout << src[encrypter];
}
cout << endl;
}
int main()
{
//for user to enter encrypt or decrypt
char s1[8];
//used to compare users choice to encrypt/decrypt
char s1a[8] = "encrypt" ;
char text[100];
int key;
char cipher[100];
cout << "Enter operation: encrypt or decrypt" << endl;
cin >> s1;
cout << "Enter key" << endl;
cin >> key;
cout << "Enter text to encrypt/decrypt" << endl;
cin >> text;
if (strcmp(s1, s1a) == 0)
{
caesar_encrypt(text, key, cipher);
}
else
{
caeser_decrypt(text, key, cipher);
}
}
*edit I figured out my key error by taking the remainder of the key.
1 2 3 4
if (key > 26)
{
key = key % 26;
}
Last edited on Nov 14, 2011 at 11:48am UTC
Topic archived. No new replies allowed.