4 Replies - 611 Views - Last Post: 29 September 2013 - 11:54 AM Rate Topic: -----

#1 sportguy98  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 29-September 13

Encryption function partially working

Posted 29 September 2013 - 10:19 AM

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 :)

/*******************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


/*******************Programming Assignment 1***************/
/****************Caesar's substitution cipher**************/

/*****************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



Woops forgot to include the description of the assignment.

Attached image(s)

  • Attached Image


Is This A Good Question/Topic? 0
  • +

Replies To: Encryption function partially working

#2 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 630
  • View blog
  • Posts: 2,107
  • Joined: 31-December 10

Re: Encryption function partially working

Posted 29 September 2013 - 10:24 AM

Have a look at an ASCII table. When you add or subtract an integer from a char, you're actually modifying the ASCII value. So you need to come up with an algorithm that cycles the char in case it goes past the alphabetic characters.
Was This Post Helpful? 0
  • +
  • -

#3 sportguy98  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 29-September 13

Re: Encryption function partially working

Posted 29 September 2013 - 10:30 AM

View Postvividexstance, on 29 September 2013 - 10:24 AM, said:

Have a look at an ASCII table. When you add or subtract an integer from a char, you're actually modifying the ASCII value. So you need to come up with an algorithm that cycles the char in case it goes past the alphabetic characters.


Thanks for your help. I am aware of the ASCII values (a=97 and z=122), I just do not know where to begin with the algorithm to cycle.

We have not yet learned how to do this in class. Could you point me in the right direction, thank you.
Was This Post Helpful? 0
  • +
  • -

#4 vividexstance  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 630
  • View blog
  • Posts: 2,107
  • Joined: 31-December 10

Re: Encryption function partially working

Posted 29 September 2013 - 10:37 AM

I would think some simple match would help you figure this out.

Take the character you want to encrypt, determine if adding/subtracting the integer from it will make it goes past the alphabetic characters. Then determine that extra amount. Set the character to 'A' or 'Z' depending on whether you're adding or subtracting. Then add the extra amount to the character.

*EDIT*: Remember that you might have an integer that doesn't make the character overrun the alphabetic characters, this is the easy case. If you need this explained a little more, let me know.

This post has been edited by vividexstance: 29 September 2013 - 10:38 AM

Was This Post Helpful? 0
  • +
  • -

#5 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1574
  • View blog
  • Posts: 2,996
  • Joined: 30-May 10

Re: Encryption function partially working

Posted 29 September 2013 - 11:54 AM

Also recycled over here -> http://cboard.cprogr...encryption.html

And here -> http://www.cplusplus...general/112073/

And here!! -> http://forums.codegu...28encryption%29

Let me help you with this problem of yours.
http://www.catb.org/...ions.html#forum

This post has been edited by Salem_c: 29 September 2013 - 11:55 AM

Was This Post Helpful? 1
  • +
  • -

Page 1 of 1