Whats wrong with this encryption program?

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
#include <string>
#include <fstream>
#include <iostream>
#include <cstdio>
#include <conio.h>
#include "cinhelper.cpp"
using namespace std;

const string cipher = "aaaaaaaa";

bool encipher(const string &pass, const string & input, string &output)
{
    const int size_s = input.size(), size_c = pass.size();
    int index_c = 0;
    for (int i = 0; i < size_s; ++i, ++index_c)
        {
            if (index_c == size_c)
                index_c = 0;
            output[i] = (input[i] + pass[i]) % 256; //was 256
        }
    return true;
}

bool decipher(const string & pass, const string & input, string & output)
{
    const int size_s = input.size(), size_c = cipher.size();
    int index_c = 0;
    for (int i = 0; i < size_s; ++i, ++index_c)
        {
            if (index_c == size_c)
                index_c = 0;
            output[i] = (input[i] - cipher[i]) % 256;
        }
    return true;
}

int main()
{
    string plain, result = "damnit";
    cout << "Type a string. \n>" << flush;
    getline(cin, plain);
    result.resize(plain.size());
    cout << "You typed: " << plain << ".\n" << flush;
    cout << "Encipher or decipher?  1 or 2?\n>" << flush;
    int xyz = 0;
    if (cin.fail())
        {
            cin.clear();
            clearcin(cin);
        }
    cin >> xyz;
    if (xyz == 1)
        {
            if (encipher(cipher, plain, result))
                cout << "Your result:  " << result << ".\n" << endl;
        }
    else if (xyz == 2)
        {
            if (decipher(cipher, plain, result))
                {
                    cout << "Your result:  " << result << ".\n" << endl;
                }
        }
    else return 123;
    return 0;
}


Seems to half work.
But when I tried running it with this:
To be or not to be, that is the question; whether 'tis nobler in the mind to suffer the slings and arrows of outrageous fortune, or to take arms against a sea of troubles, and by oppossing end them.


I got this result:
Áðü├ãüðË ÓÌþc↑D`♠├, ╠z┬t╚xÈ sgd▼ıÍÊߦ¦oÎò⌂p╔└     H┬      ð /;is Qobler i┬Åö╩╩@▄█ÄÊÅÞÅöÔò╚╦æÆö▄╔┘@▄▀ëÔ¤Ï@ÊÒ╔ôı█ߦÑô@ã╬àÒ¦┘õüÄ┘ÏÞôÄıÐÌ┘þÄ╬Ü@Ò┌àö▄ëÔ┼ï┘ÅüÕÔ┘åã┘ü¦ÍÏöô═ëß╠È@ðÈäöËßþÐÒÏôøåü¦┘öÈ┌çÈ▀ÕÔô┘ÏÓ█òËËÉ@Ò┌àßØ


which is equivalent to this (when decrypted with the same program)

To be or notÜ×I Q■, thqt iâ the question. W¯ª&Ëk[H║P = ð /;is
kü¯ƒªõóMIÔ▒¡g▓³|ı╦ªo∟§Ên═P¸q▼-x¬♦j
 TÀ¯  ²³ ■±╗☺■♠♠¶>ESı©³·³╗­Y¨¹²O♣♦☻☻«E♣÷ M┴.¾³± Ý♥M◄╗5GÝ☺


Whats going on.
never mind
Topic archived. No new replies allowed.