-2

I've looked around everywhere for a C++ code that takes a message from the user, and encodes it by increasing the ASCII value of each character (obviously not very secure, but simple enough). I've managed to put together a program that returns a character of a few values higher, but can not figure out how to do it with a full message including spaces. I plan to make a decoder that does the opposite afterwards. Any help would be really appreciated. Thanks in advance.

Single Value C++ Program -

    #include <iostream>
using namespace std;
int main(){
 char ascii;
 cout << "Enter a character: ";
 cin >> ascii;
 cout << "Its ascii value is: " << (int) ascii << endl;
 return 0;
}

Working Encoder Example in VBS -

set x = WScript.CreateObject("WScript.Shell") 
entxtde = inputbox("Enter text to be encoded") 

entxtde = StrReverse(entxtde)
x.Run "%windir%\notepad" 
wscript.sleep 1000 
x.sendkeys encode(entxtde) 

function encode(s) 
For i = 1 To Len(s) 
newtxt = Mid(s, i, 1) 
newtxt = Chr(Asc(newtxt)+3) 
coded = coded & newtxt
 Next 
encode = coded 
End Function

3 Answers3

0
std::string originalString = "";
std::string newString = "";
int incrementValue = 1;
std::cout << "Input a string to encode: ";
std::cin >> originalString;
for(int i = 0; i < originalString.length(); i++) {
   newString += (originalString.at(i) + incrementValue);
}
std::cout >> "New string is " + newString

Just change incrementValue to change how it's encoded. "Hello" = "Ifmmp" if incrementValue = 1
To reverse it, just change it to subtract incrementValue instead of add in the same kind of for loop. Simple enough I think

Darkrifts
  • 211
  • 2
  • 11
0

This may be done in a single line, or two if you want to stay under 80 columns. Where value is the string you wish to encrypt, and offset the offset value:

auto caesar = [offset](CharT c){return c + offset;};
std::transform(value.begin(), value.end(), value.begin(), caesar);

For bonus points you can make it work with any kind of string by templating on character type:

template <typename CharT>
std::basic_string<CharT> caesarEncode(std::basic_string<CharT> value, CharT offset){
    auto caesar = [offset](CharT c){return c + offset;};
    std::transform(value.begin(), value.end(), value.begin(), caesar);
    return value;
}

Since it looks like you may be experiencing difficulties actually obtaining a string with whitespace, you may get one using the getline function of the standard library, which by default obtains one full line of the source stream.

// narrow (CharT = char)
std::string value;
std::getline(std::cin, value);

// wide (CharT = wchar_t)
std::wstring wvalue;
std::getline(std::wcin, wvalue);

for which actually encoding the string would be done as follows:

char offset = 12;
auto encoded = caesarEncode(value, offset);

wchar_t woffset = 12;
auto wencoded = caesarEncode(wvalue, woffset);

You can see an example in practice here on coliru.

jaggedSpire
  • 4,423
  • 2
  • 26
  • 52
-1

It's simple really. First you take your input as a string. Then, iterate through each character adding by what you want. To ensure the value remains valid and easy to reverse, you mod that value by the max value of a char, i.e. 255.

int main () {
    std::string input; // to store the text
    std::getline(std::cin, input); // get the text

    const int _add = 12; // value added
    const int max_size = 255; // max value of a char
    for (int i = 0; i < input.size(); ++i)
        input[i] = (input[i] + _add) % max_size;
    /* now the input is properly modified */
}

note: _add is an int to prevent overflow errors.

Mooing Duck
  • 64,318
  • 19
  • 100
  • 158
  • In Visual Studios, you can force all strings to be wide. In which case, input.size() won't return the length of `char`. – MyLone Trojan Jul 02 '16 at 00:04
  • Although it is usually unnecessary, It won't yield incorrect results. I don't think I deserve the downvote :( – MyLone Trojan Jul 02 '16 at 00:06
  • Dude, cplusplus has been my reference! It says in `size()` will return length in bytes. cppreference says what you're saying. I have been duped – MyLone Trojan Jul 02 '16 at 00:16
  • Well, technically `std::string::size` is in bytes and codepoints both, because they're the same. But cplusplus doesn't even mention wstring nor basic_string. Everyone avoids that site. – Mooing Duck Jul 02 '16 at 00:21
  • It does mention them [http://www.cplusplus.com/reference/string/]. why do people avoid it? I'm unaware. – MyLone Trojan Jul 02 '16 at 00:23
  • 1
    Oh, there it is: ["Returns the length of the string, in terms of number of characters."](http://www.cplusplus.com/reference/string/basic_string/size/) Also see: [What's wrong with cplusplus.com?](http://stackoverflow.com/questions/6520052/whats-wrong-with-cplusplus-com) – Mooing Duck Jul 02 '16 at 00:28