Help with simple encryption

Hello everyone,

I am having trouble with an assignment. The assignment consists of a basic encryption and decryption program already written for me, I just have to write the encryption function. What we have to get the program to do is enter an integer and a text, and get the program to increment each letter in the text by the integer given. I did this by using a for loop and incrementing each value in the string by the integer.

However, I still can't get it to decrypt and I need the program to work with only a-z letters (if I increment each letter by 3 and I have the letter Z, it should go to Z+3 = C).

I attached the description of the attachment and below are the codes: The first file does not need to be edited.

Thanks for helping :)

Link to picture of assignment: http://i41.tinypic.com/v4ta3s.jpg

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
/*******************Programming Assignment 1***************/
/****************Caesar's substitution cipher**************/
 
/*****************YOU MUST NOT EDIT THIS FILE**************/
 
/****Substitute alphabets in a string using rotation key***/
/****Leave all other characters unchanged******************/
/****Confirm correct encryption by decrypting**************/
/****to recover original text******************************/
 
#include "encrypt.h"
#include "studentID.h"
 
using namespace std;
 
 
int main () {
        // define the rotation index variable and
        //the new rotation index variable to recover the text
        int rot, newrot;
       
        // define the input string (input), encrypted string (output)
        // and the recovered string (decrypted)
        string input, output, decrypted;
 
        // top level loop to perform encryptions
        while (1) {
                std::cout << "Please enter rotation key (or -1 to exit)" << endl;
                // read in the rotation index from user
                std::cin >> rot;
 
                // if the user is done (rot == -1), then exit
                if (rot == -1) break;
 
                // read in the text to be encrypted (terminated by new line)
                // use "ignore" method of stream cin to get rid of
                // accumulated new line characters
                std::cout << "Please enter text to be encrypted" << endl;
                cin.ignore();
                getline(std::cin, input);
 
                // encrypt the input text
                output = encrypt(input, rot);
                // print out the encrypted text
                cout << endl << "The encrypted text is " << endl << output << endl;
 
                // calculate a new rotation value to recover original text
                // from the encrypted text -- used for testing only
                newrot = 26 - (rot%26);
                // recover original text by calling encrypt with new rotation value
                decrypted = encrypt(output, newrot);
 
                // print out the recovered text -- should match original
                cout << endl << "The recovered text is " << endl << decrypted << endl << endl;
        } // end while loop
} // end main function 


This part needs to be edited:

1
2
3
4
5
6
7
8
9
10
11
12
/*****************YOU MUST EDIT THIS FILE******************/
/************Complete function encrypt below***************/

#include "encrypt.h"
using namespace std;


string encrypt (string input, int rot) {
    for (string::size_type i = 0; i < input.length(); ++i)
    input[i]+=rot;
    return input;
} //end function encrypt 
Appologies for being blunt, but the mistake in that code is quite obvious...

I'll give you a hint: A character is only 8 bits, and there are only 256 unique permutations of 8 bits. You should be able to figure it out from there.
Basically, you're trying to keep any non-alphabet characters unaffected by the encryption, yes?
You're also struggling with the concept of wrapping rot around the alphabet.

Well, 'A' is dec 65 on the ascii table, and 'Z' is 90. 'a' is 97 and 'z' is 122.
You could turn that into a simple if statement.

Additionally, your teacher actually makes use of the modulus operator on line 49 of the first file, and actually explains its usage in the image you provided.
Last edited on
Topic archived. No new replies allowed.