0

Okay so I decided to write a Caesar Cipher but with a twist. Rather than simply having a numeric key I decided that it would be cool to have a short word that corresponds to offset. That way the pattern is harder to track. An example would be something like encoding the phrase "helloworld" with a key being "bad" (which corresponds to an offset of 2, 1, 4), this would lead to the encoded message being "jfpnpaqspf". I know that my code works for a simple Caesar Cipher with a numeric key but I'm having trouble on the loop for the key value. Any suggestions would be greatly appreciated!

#include <iostream>
#include <stdio.h>
using namespace std;

int main() {
    cout << "Enter the message: " << endl;
    char msg[80];
    char *myptr;
    myptr = msg;
    unsigned char cipher = 0;
    cin >> msg;
    char key[79];
    char *ptr;

    ptr = key;
    unsigned char ltr = 0;
    cout << "Enter Key: " << endl;
    cin >> key;
    while (*myptr) {
        while (*ptr) {
            ltr = *ptr;
            *ptr = ltr;
            ptr++;
        }
        cipher = *myptr;
        cipher += *ptr;
        if (cipher > 'z') {
            cipher -= 26;
        }
        if (cipher < 'a') {
            cipher += 26;
        }
        *myptr = cipher;
        myptr++;
    }
    cout << "New Message: " << msg << endl;
}
Arty
  • 14,883
  • 6
  • 36
  • 69
  • I fixed your code, [corrected code is here](https://cutt.ly/GgABQb3) or [here](https://repl.it/@moytrage/StackOverflow64666118#main.cpp), so that it produces now valid result. – Arty Nov 03 '20 at 16:16
  • One more question, how would I go about working backwards to decode something. My original cipher without the phrase as a key worked fine with doing that by sayin something like -1 as the key. Is there a way to make this program do a similar thing by decoding the message using '-bad' as the key? – Gabe Hjelle Nov 03 '20 at 16:40
  • You can just multiply by +1/-1 when adding key depending on whether it is encryption or decryption, [full code for doing this here](https://cutt.ly/dgA3gL7), see input and output box at this link, they contain example input and output. – Arty Nov 03 '20 at 16:51
  • Have a look at the Vigenere cipher. In effect, that is what you have re-invented. – rossum Nov 03 '20 at 20:42
  • @Arty but how would I do it by simply checking if the key had a negative leading it? – Gabe Hjelle Nov 04 '20 at 02:53
  • @GabeHjelle I improved my code to handle `-` in key, [see full code here](https://cutt.ly/5gSAthV). – Arty Nov 04 '20 at 03:05
  • @Arty I think you misinterpreted my question, rather than asking the user for whether they would like to encrypt or decrypt I simply want it to decrypt if the first thing inputted in the key is a negative sign. Thank You so much for all of your help! – Gabe Hjelle Nov 04 '20 at 03:12
  • @GabeHjelle Probably you opened wrong link! In the very last link above I did exactly what you want now, i.e. don't ask if it is enc/dec but just use `-` in the beginning of key, [here I post my same link again](https://cutt.ly/5gSAthV), look into `Input` and `Output` boxes at web-page by this link, they contain all is needed for input/output. – Arty Nov 04 '20 at 03:19

0 Answers0