A little assistance with one of my encryption programs.

After a little bit of pain and suffering i've discovered that my encryption programs do not work with each other. They work swell independantly, and i've fixed a bug in my hill cipher. But my mono alphabetic substitution cipher misbehaving, and i have a feeling that is the culprit.

Originally I had planned on just writing mono cipher for a 26 letter alphabet, but then decided to change it into a 52 letter alphabet. I had at first including a function 'tolower()' to change the user input into an all lower case string. incidently after I tested it a couple of times today I noticed that when I decrypted a file all the letters were in lowercase (which I though I had fixed by now). so I went in and removed all the references to 'tolower()' but now guess what? there are spaces instead of letters in the decrypted messages where the capital letters ought to be!!!!

Here is my source code:
Yes, I know its a mess.


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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
#include <iostream>
#include <string>
#include <fstream>

using namespace std;

short option = 3;
short method = 3;
int opt();
char key[54];

char lookup[53] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

char enc(char c);
char dec(char c);
string phrase;
bool is_a_good(string & line);
bool init(const char beta, const string & text);
void condense(string & str);

void krypt();




int main()
{
    opt();
    start:

    cout << "Enter a phrase containing all letters of the alphabet. \n(Each letter should appear only once.) "<< endl;
    do{
        //cin >> phrase;
        getline(cin, phrase);
        condense(phrase);
    }while (!is_a_good(phrase));

    cout << "phrase = " << phrase << " \n";
    for (int i = 0; i < 27; ++i)
        {
            key[i] = phrase[i];
        }

    string s;

    if (option == 0)
        {
            if (method == 0)
                krypt();
     
            else {
            cout << "encrypt?  " << endl; getline(cin, s);
            for (int i = 0; i < s.length(); ++i)
                {
                    s[i] = enc(s[i]);
                }
            cout << "ENCRYPTED: " << s << endl; }
        }
    if (option == 1)
        {
            if (method == 0)
                krypt();
            else {
            cout << "decrypt?  "; getline(cin, s);
            for (int i = 0; i < s.length(); ++i)
                {
                    s[i] = dec(s[i]);
                }
            cout << "DECRYPTED: " << s << endl; }
        }
    goto start;
}


int opt()
{
    cout << "Encrypt or decrypt, 0 or 1? " << endl;
    cin >> option; cin.ignore();
    cout << "Krypt a file or krypt text from keyboard? 0 or 1\n";
    cin >> method; cin.ignore();
    return 0;
}


char enc(char c)
{
    int index = 0; char letter;
    if (isalpha(c))
        {
            if (!isalpha(c))
                c = tolower(c);
            //c = tolower(c);
            while (c != lookup[index])
                {
                    ++index;
                }
            c = key[index];
        }
    letter = c;
    return letter;
}

char dec(char c)
{
    int index = 0; char letter;
    if (isalpha(c))
        {
            //c = tolower(c);
            if (!isalpha(c))
                c = tolower(c);
            while (c != key[index])
                {
                    ++index;
                }
            c = lookup[index];
        }
    letter = c;
    return letter;

}

bool is_a_good(string & line)
{
    short time = 0;
    short testsize = 26;
    if (method == 0)
        testsize *= 2;
    bool ok = true;
    char alpha = 'a';//testing
    if (line.length() != testsize)
        {//cout << "wrong sizE! "<< endl;
        return !ok;}
    for (int i = 0; i < line.length(); ++i)
        {
            ++time;
            if (time == 27)
                alpha = 'A';
            //if (method != 0)
              //  line[i] = tolower(line[i]);
            if (!isalpha(line[i]))
                line[i] = tolower(line[i]);
            if (!init(alpha, line))
                {
                    cout << "alpha = " << alpha << " " << endl;
                    cout << "time = " << time << endl;
                return !ok;}
            ++alpha;
            //cout << "alpha = " << alpha << " \n" << endl;
        }


    return true;
}

bool init(const char beta, const string & text)
{
    bool good = false;
    for (int i = 0; i < text.length(); ++i)
        {
            if (text[i] == beta)
                return true;
        }
    return false;


}

void condense(string & str)
{
    string newstr = str;
    string temp = "";
    cout << "first str = " << str << " \n";
    char t;
    for (int i = 0; i < newstr.length(); ++i)
        {
            t = newstr[i];
            if (!init(t, temp) )
                if (isalpha(t))
                    temp += t;
        }
    str = temp;
    cout << "str = " << str << " \n" << endl << " \n";



    return;
}

void krypt()
{
    string s;
    char l;
    ifstream fin;
    ofstream fout;
    ifstream tin;
    string sin, sout;
    cout << "krypt which file? \n";
    cin >> sin;
    cout << "save it to .... \n";
    cin >> sout;

    tin.open(sin.c_str(), ios::binary);
    if (!tin.is_open())
        {cout << sin << " does not exist. " << endl; exit(1); }
    tin.close();
    tin.open(sout.c_str(), ios::binary);
    if (tin.is_open())
        { cout << sout << " already exists. " << endl; exit(1); }
    tin.close();
//cout << "hey you guys!!!" << endl;
    fin.open(sin.c_str(), ios::binary);
    fout.open(sout.c_str(), ios::binary);
    if (option == 0 )
        {
            do{
                //getline(fin, s);
                fin.get(l);
                if (fin.eof())
                    break;
                        //s[i] = enc(s[i]);
                //if (isalpha(l))//testing
                    l = enc(l);
                fout.put(l);
            }while (!fin.eof() );
        }
    else if (option == 1 )
        {
            do{
                fin.get(l);
                if (fin.eof())
                    break;
                //if (isalpha(l))//testing
                    l = dec(l);
                fout.put(l);
            }while (!fin.eof() );

        }
    else cout << "invalid selection. \n";

    return;
}
Boy, you sure are spending a lot of effort in something so utterly pointless.

http://www.cryptopp.com/
I tried looking at that list but I couldn't find one for a classic mono alphabetic substitution cipher.
Topic archived. No new replies allowed.