problem with variable type converions

Im writting a program that allows the user to create a password that is encrypted to a text file. So far, I can create a password and guess it correcetly/incorrectly, but instead of encrypting the password to the text file, it keeps writting the actual password to the file.

I have tried searching for other methods, but I haven't found anyone making a password maker/guesser like this. How can I have my password encrypted in the text file, and reverse encrypted back into the code to be guessed?

Here is my code:

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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <windows.h>

using namespace std;

struct pass_change
{
    char pass;
    int number;
};

pass_change array[50];

int main()
{
    ifstream inPass;
    ofstream outPass;
    string filename = "PassFile.txt", guess, password, passconfirm;
    int choice, n = 0;

    inPass.open(filename.c_str());

    if(!inPass)
    {
        outPass.open("PassFile.txt");

        cout << "\nA Password has not been set!";
        cout << "\n\nPlease enter a password: ";
        cin >> password;
        cout << "Re-Enter your password to confirm: ";
        cin >> passconfirm;

        while(password != passconfirm)
        {
            cout << "\n\nYour password did not match!";
            cout << "\n\nPlease enter a password: ";
            cin >> password;
            cout << "\n\nRe-Enter password to confirm: ";
            cin >> passconfirm;
        }

        cout << "\n\nYour password has been set!\n\n";
        outPass << password;

        inPass >> array[n].pass;
        while(inPass)
        {
            n++;
            inPass >> array[n].pass;
        }

        inPass.close();
        inPass.open(filename.c_str(), ios::out);

        for(int looper = 0; looper < n; looper++)
        {
            password += array[looper].pass;
        }

        goto alpha;
    }

    inPass >> array[n].pass;
    while(inPass)
    {
        array[n].number = static_cast<int>(array[n].pass);
        array[n].number = array[n].number - 13;
        array[n].pass = static_cast<char>(array[n].number);
        cout << array[n].pass;
        n++;
        inPass >> array[n].pass;
    }

    for(int looper = 0; looper < n; looper++)
    {
        password += array[looper].pass;
    }

    alpha:

    cout << "\nEnter your password: ";
    cin >> guess;
    while(guess != password)
    {
        cout << "\n\nACCESS DENIED\n\n";
        cout << "Enter 1 to guess again or anything to cancel: ";
        cin >> choice;
        if(choice == 1)
        {
            cout << "\n\nEnter your password: ";
            cin >> guess;
        }
        else
        {
            cout << "\nProgram Closing...\n\n";
            return 0;
        }
    }
    cout << "\nACCESS GRANTED\n\n";

    for(int looper = 0; looper < n; looper++)
    {
        array[looper].number = static_cast<int>(array[looper].pass);
        array[looper].number = array[looper].number + 13;
        array[looper].pass = static_cast<char>(array[looper].number);
        outPass << array[looper].pass;
        cout << array[looper].pass;
    }

return 0;
}
Instead of just writing the string to the file like you do, encrypt in first, then write the encrypted string. Then, decrypt the string after reading it in, instead of just assuming the string in the file is the password.
I just finished an intro to programming course, and we weren't taught anything of that sort. How would I go about doing that?
You should go look up some encryption method online that you want to implement, preferably something simple like a shift cipher. Then just use that method before writing/after reading the string to/from the file.
I am using a shift cipher of 13, but I'm having trouble changing a string into individual characters which I can static_cast to find their integer values to make the change. I don't know of any other ways to change the values of the characters in the password so that the password can be encrypted.
You can modify an individual character of a string very easily, like so:
str[i] += 5; // adds 5 to the ASCII value of the i-th character of str
In your actual code, you'll want to make sure you actually "wrap" around the alphabet, so adding 13 to 'z' doesn't give you '_'.
Sounds like a lot of if statements... thanks for the help. I'll keep ya posted
Topic archived. No new replies allowed.