Encryption program help

Ok so im making an encryption program and its supposed to go through the text document and replace words with symbols and such and im stuck at the first part where you need it to replace a symbol.

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 <iostream>
#include <string>
#include <fstream>
#include <sstream>

using namespace std;

void encrypt()
{
    ifstream fIn;
    string file_name;
    string store_text;
    char lower[27] = {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '\0'};
    char replace[1] = {'@'};

    cin.ignore(100, '\n');

    cout << "what is the exact name of the file for encryption" << endl;
    getline(cin, file_name);

    fIn.open(file_name.c_str());

    stringstream ss(stringstream::in | stringstream::app);

    while(getline(fIn, store_text))
    {
        ss << store_text << endl;
    }

    cout << store_text;

    store_text = ss.str();

    while((store_text.find(lower[0])) != string::npos)
    {
        replace[0];
    }

    cout << store_text;
}

void decrypt()
{

}

int main()
{
    int choice;


    cout << "What do you want to do?" << endl;
    cout << "1) encrypt file" << endl;
    cout << "2) decrypt file" << endl;
    cin >> choice;

    if(choice == 1)
    {
        encrypt();
    }
    else if(choice == 2)
    {
        decrypt();
    }
}
bump
See this little example and note the differences in your while loop http://ideone.com/Jx0i8F
I cant figure out how to replace stuff from the file. It just wont work

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
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;

int main()
{
    ifstream fIn;

    string file;
    string store;
    string s;
    char f[3] = {'a', 'b', 'c'};
    char rep = '@';
    int pos = string::npos;

    cout << "what file?" << endl;
    getline(cin, file);

    fIn.open(file.c_str());

    stringstream ss(stringstream::in | stringstream::app);

    while(getline(cin, store))
    {
        ss << store << endl;
    }

    store = ss.str();

    while((pos = store.find(f[0])) != string::npos)
    {
        store[pos] = rep;
    }
    cout << store << endl;
}
Your not changing the file at all just the contents of it stored in memory. You have to write the string to a file after you change it.
It wont display the results though, i want to display the results in the program to make sure its working correctly first which it isnt.
Add the out flag to stringstream constructor stringstream ss(stringstream::in | stringstream::out | stringstream::app);
Awesome thanks, now is there any way to make this shorter, theres tons of while loops.

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
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>

using namespace std;

void encrypt()
{
    ifstream fIn;
    string file_name;
    string store_text;
    char lower[27] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','\0'};
    char upper[27] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','\0'};
    char replace[27] = {'‰','×','Ç','¼','Ѝ','ƒ','þ','ß','¤','Œ','Ø','¥','ð','½','å','¹','°','Ä','¼','¡','ð','±','','¶','¿','™','\0'};
    int pos = string::npos;
    cin.ignore(100, '\n');

    cout << "what is the exact name of the file for encryption" << endl;
    getline(cin, file_name);

    fIn.open(file_name.c_str());

    stringstream ss(stringstream::in | stringstream::app | stringstream::out);

    while(getline(fIn, store_text))
    {
        ss << store_text << endl;
    }

    store_text = ss.str();

    while((pos = store_text.find(lower[0])) != string::npos)
    {
        store_text[pos] = replace[0];
    }

    while((pos = store_text.find(lower[1])) != string::npos)
    {
        store_text[pos] = replace[1];
    }

    while((pos = store_text.find(lower[2])) != string::npos)
    {
        store_text[pos] = replace[2];
    }

    while((pos = store_text.find(lower[3])) != string::npos)
    {
        store_text[pos] = replace[3];
    }

    while((pos = store_text.find(lower[4])) != string::npos)
    {
        store_text[pos] = replace[4];
    }

    while((pos = store_text.find(lower[5])) != string::npos)
    {
        store_text[pos] = replace[5];
    }

    while((pos = store_text.find(lower[6])) != string::npos)
    {
        store_text[pos] = replace[6];
    }

    while((pos = store_text.find(lower[7])) != string::npos)
    {
        store_text[pos] = replace[7];
    }

    while((pos = store_text.find(lower[8])) != string::npos)
    {
        store_text[pos] = replace[8];
    }

    while((pos = store_text.find(lower[9])) != string::npos)
    {
        store_text[pos] = replace[9];
    }

    while((pos = store_text.find(lower[10])) != string::npos)
    {
        store_text[pos] = replace[10];
    }

    while((pos = store_text.find(lower[11])) != string::npos)
    {
        store_text[pos] = replace[11];
    }

    while((pos = store_text.find(lower[12])) != string::npos)
    {
        store_text[pos] = replace[12];
    }

    while((pos = store_text.find(lower[13])) != string::npos)
    {
        store_text[pos] = replace[13];
    }

    while((pos = store_text.find(lower[14])) != string::npos)
    {
        store_text[pos] = replace[14];
    }

    while((pos = store_text.find(lower[15])) != string::npos)
    {
        store_text[pos] = replace[15];
    }

    while((pos = store_text.find(lower[16])) != string::npos)
    {
        store_text[pos] = replace[16];
    }

    while((pos = store_text.find(lower[17])) != string::npos)
    {
        store_text[pos] = replace[17];
    }

    while((pos = store_text.find(lower[18])) != string::npos)
    {
        store_text[pos] = replace[18];
    }

    while((pos = store_text.find(lower[19])) != string::npos)
    {
        store_text[pos] = replace[19];
    }

    while((pos = store_text.find(lower[20])) != string::npos)
    {
        store_text[pos] = replace[20];
    }

    while((pos = store_text.find(lower[21])) != string::npos)
    {
        store_text[pos] = replace[21];
    }

    while((pos = store_text.find(lower[22])) != string::npos)
    {
        store_text[pos] = replace[22];
    }

    while((pos = store_text.find(lower[23])) != string::npos)
    {
        store_text[pos] = replace[23];
    }

    while((pos = store_text.find(lower[24])) != string::npos)
    {
        store_text[pos] = replace[24];
    }

    while((pos = store_text.find(lower[25])) != string::npos)
    {
        store_text[pos] = replace[25];
    }

    while((pos = store_text.find(lower[26])) != string::npos)
    {
        store_text[pos] = replace[26];
    }

    //Upper Case

    cout << store_text;
}

void decrypt()
{

}

int main()
{
    int choice;


    cout << "What do you want to do?" << endl;
    cout << "1) encrypt file" << endl;
    cout << "2) decrypt file" << endl;
    cin >> choice;

    if(choice == 1)
    {
        encrypt();
    }
    else if(choice == 2)
    {
        decrypt();
    }
}
Use a for loop and use a variable to determine the element of the arrays that will be used for searching and substitution

By the way, I'm puzzled by the fact that npos equals -1... what does that represent
Last edited on
Crikey - you need some for loops !! See how they are all the same except for the value of the replace subscript.

Also I don't think you need the upper or lower arrays. You can make use of the value of a char to calculate the subscript in the replace array. You can do this because a char is just a small int.

You should always look out for repetitive code and identify patterns, to help see a better solution.
how would that look you guys im a bit confused
Think about this:

1
2
3
4
char NewChar;

NewChar = replace[store_text[pos] - 'A'}


This works because the char is a small int. If the char is 'B' then 'B' - 'A' is 1, so this is position 1 in the array.

Now you just need a loop to cycle through the original string.
"This works because the char is a small int. If the char is 'B' then 'B' - 'A' is 1, so this is position 1 in the array"

That just fucked my head lol, sorry but i dont quite understand. I get i can use a for loop for it to search through all the letters and replace with a symbol but i dont quite understand what your talking about.
http://www.ascii-code.com/


AS I said a char is just a small int - these are equivalent:

1
2
3
4
5
6
7
8
9
10
11
char MyChar;

MyChar = 'A';
MyChar = 65;  //decimal integer
MyChar = 0101; //octal
MyChar = 0x41; //hexadecimal

unsigned short Pos = 'D' - 'A'; //pos is 3 - the 4th position in the array because arrays start at zero

MyChar = 'A' + 3; //Mychar is now 'D'


And you can do math with them Which ever format you use.

I get i can use a for loop for it to search through all the letters and replace with a symbol but i dont quite understand what your talking about.


The code in my last post calculates a position in the replace array, though I forgot to mention that the char needs to be upper case to work. This stuff relies on the chars being adjacent in the character set.

You can use std::toupper ( need to #include <locale>) to convert to uppercase.

Is this a bit clearer now?
not really, and i tried to change all my while loops to one while loop and a for loop, but its not encrypting the text, what am i doing wrong?

1
2
3
4
5
6
7
8
while(pos != string::npos)
    {
        for(int i = 0; i < 27; i++)
        {
            store_text2.find(lower[0+1]);
            replace[0+1] = store_text2[pos];
        }
    }



Entire 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
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
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>

using namespace std;

void encrypt()
{
    ifstream fIn;
    ifstream passIn;
    string file_name2;
    string store_text2;
    char lower[27] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','\0'};
    char upper[27] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','\0'};
    char replace[28] = {'‰','×','Ç','¼','Ѝ','ƒ','þ','ß','¤','Œ','Ø','¥','¬','½','å','¹','°','Ä','ª','¡','ð','±','','¶','¿','™','®', '\0'};
    char space = ' ';
    int pos = string::npos;

    cin.ignore(100, '\n');

    cout << "what is the exact name of the file for encryption" << endl;
    getline(cin, file_name2);

    fIn.open(file_name2.c_str());

    stringstream ss(stringstream::in | stringstream::app | stringstream::out);

    while(getline(fIn, store_text2))
    {
        ss << store_text2 << endl;
    }

    store_text2 = ss.str();

    while(pos != string::npos)
    {
        for(int i = 0; i < 27; i++)
        {
            store_text2.find(lower[0+1]);
            replace[0+1] = store_text2[pos];
        }
    }



    ofstream fOut;

    fOut.open(file_name2.c_str());

    fOut << store_text2;

    cout << "File Encrypted\n" << endl;

    cout << store_text2;

    cin.get();
}

void decrypt()
{
    ifstream fIn;
    string file_name;
    char space = ' ';
    string store_text;
    char lower[27] = {'a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','\0'};
    char upper[27] = {'A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z','\0'};
    char replace[28] = {'‰','×','Ç','¼','Ѝ','ƒ','þ','ß','¤','Œ','Ø','¥','¬','½','å','¹','°','Ä','ª','¡','ð','±','','¶','¿','™','®', '\0'};
    int pos = string::npos;
    cin.ignore(100, '\n');

    cout << "what is the exact name of the file for decryption" << endl;
    getline(cin, file_name);

    fIn.open(file_name.c_str());

    stringstream ss(stringstream::in | stringstream::app | stringstream::out);

    while(getline(fIn, store_text))
    {
        ss << store_text << endl;
    }

    store_text = ss.str();

    while(pos != string::npos)
    {
        for(int i = 0; i < 26; i++)
        {
            store_text.find(replace[0+1]);
            store_text[pos] = lower[0+1];
        }
    }

    ofstream fOut2;

    fOut2.open(file_name.c_str());

    fOut2 << store_text;

    cout << "File Encrypted\n" << endl;

    cout << store_text;

    cin.get();
}

int main()
{
    int choice;

    cout << "Encryptor Version 1.0.0 Alpha\n" << endl;

    cout << "What do you want to do?" << endl;
    cout << "1) Encrypt file" << endl;
    cout << "2) Decrypt file" << endl;
    cin >> choice;

    if(choice == 1)
    {
        encrypt();
    }
    else if(choice == 2)
    {
        decrypt();
    }
}
Last edited on
bump
Try replacing lines 36 - 43 with
1
2
3
4
5
6
7
8
 for(int i = 0; i < 27; i++)
    {
	pos = 0;
        while((pos = store_text2.find(lower[i]), pos) != string::npos)
        {            
            store_text2[pos] = replace[pos%26];
        }
    }
Last edited on
I disagree with having the lower & upper arrays at all, or using the find function.

Just cycle through the string itself, calc the correct position in the replace array (I gave some clues about that), put that char into the stored string.

Try this, It is not tested, I just typed it straight in - should give you an idea.

1
2
3
4
5
6
7
8
9
10
11
12
#include <locale> //for std::toupper
std::string InputStr;
std::string OutputStr;

//only need 26 of them in the replace array
//no need to specify how many there are - the compiler will count them for you
char replace[] = {'‰','×','Ç','¼','Ѝ','ƒ','þ','ß','¤','Œ','Ø','¥','¬','½','å','¹','°','Ä','ª','¡','ð','±','','¶','¿','™'};

for (std::size_t i=0; i<InputStr.size(); i++) {
      unsigned short Pos = std::toupper( InputStr.at(i)) - 'A';
     OutputStr.at(i ) = replace[ Pos];
}


To avoid using the replace array, it would be even easier to just add 109 to the value of the char, which gives A -> ®, B-> ¯ , C -> °

Although having the replace array with chars in a random order makes the encryption harder to break.



1
2
3
4
5
6
7
8
#include <locale> //for std::toupper
std::string InputStr;
std::string OutputStr;


for (std::size_t i=0; i<InputStr.size(); i++) {
     OutputStr.at(i ) = std::toupper( InputStr.at(i)) + 109;
}


Try that - see how you get along.


Topic archived. No new replies allowed.