Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

how would i put a password on a text file in c++?

i am making a program to "encrypt" a text file (for school) in c++ i would like to know if its possible to put a password on such a file. and if so how.

Update:

shadow had the right idea, or at least what my teacher wants me to do, but im wondering if i could put a password on the file, so when someone goes to "my documents" and clicks "document2.txt" they are promed for a password. i would like to know if this is possible.

3 Answers

Relevance
  • 1 decade ago
    Favorite Answer

    It is apparent that you barely have an idea how encryption works. As such, anything you "invent" will be insecure and at best provide a false sense of security. However, there are methods that will at least demonstrate the idea. Just don't ever encrypt anything you actually want to keep secret. Even if you get the best most secure algorithm, using it securely requires even more skill.

    At the bottom of the list is the old Caesar Cipher. It simply shifts the alphabet and has a possible key space of 26. The old rot13 encryption from usenet is sort of a variant.

    Next on the list is a Vigenere Cipher. It actually uses a repeating keyword to key as many alphabets as there are letters in the key and amounts to a Caesar Cipher with multiple keys and a repeating pattern.

    If you get really serious, you might want to look at the ADFGVX cipher. It uses both substitution and transposition and is a bit more secure than the above. Time line wise, this cipher was used in WW1 by the Germans.

    At this point, the complexity of the cipher may force you to exceed the amount of time you have for your project.

    The Enigma is the WW2 German cipher and consists of 3 wheels that rotate after each letter is encrypted. There were various versions used during WW2 up to a 4 wheel version used by the German Navy.

    After WW2, the US used a rotating wheel cipher known as SIGABA. It was much more complex than the Enigma with more wheels and extra rotors that controlled how much a wheel would move.

    So far all the ciphers listed will only work on letters if used in their historic forms. The next few ciphers deal with binary. You can probably code a working version of one of the following in 4-8 hours depending on how well you understand the standard. You may also find free reference implementations.

    The US government decided that we needed an encryption standard and DES was invented. It has a 56 bit key and a 64 bit block length. You'll need to do some special hashing to use a password as a DES key.

    More recently, AES replaced DES as the encryption standard. At the same time we also have SHA-1 as a hash algorithm. I'll let you research this one if you really want to go this far.

    There you have a very brief history on ciphers up to not quite the present. I don't know how secure your program needs to be or how much time you have. Just know that they get more complex and more difficult to make work correctly as they get newer.

    The easy way to put a "password" on a file would be to use GPG.

    Shadow Wolf

  • Anonymous
    1 decade ago

    The "password" is the encryption key. It's not possible to keep people from opening the encrypted file without a password, but the file is useless to anyone without the key because they can't decrypt it.

  • 1 decade ago

    I don't know anything about encryptions, but I think I know what you want. You want a program that makes some secret text available only with a password. There possibly infinite ways to do this, but I can think of two simple ways:

    1) user works only with the console (e.i. DOS box).

    a) you run the program

    b) a password is asked of you

    c) you input the password

    d) the password is matches

    e) the secret text is revealed on the console

    2) have the user input password into console for console to create a text file containing the secret text

    // option 1

    #include <iostream>

    #include <string>

    using namespace std;

    int main()

    {

    string Password = "123456"; //declare variable and assign to string 123456

    string UserInput;

    int AreTheSame = 0;

    while (true)

    {

    cout<< "Enter password:" <<endl;

    cin>> UserInput;

    cout<<endl;

    cout<<endl;

    if (UserInput.compare(Password) == AreTheSame)

    {

    cout<< "I'm the secret text." <<endl;

    cout<< "Thanks for finding me."<<endl;

    cout<<endl;

    cout<<endl;

    }

    else

    {

    cout<< "Wrong Password!"<<endl;

    }

    }

    return 0;

    }

    // option 2

    #include <iostream>

    #include <string>

    #include <fstream> //file stream

    using namespace std;

    int main()

    {

    string Password = "123456"; //declare variable and assign to string 123456

    string UserInput;

    int AreTheSame = 0;

    while (true)

    {

    cout<< "Enter password:" <<endl;

    cin>> UserInput;

    cout<<endl;

    cout<<endl;

    if (UserInput.compare(Password) == AreTheSame)

    {

    ofstream SecretTextFile("MySecretText.txt");

    SecretTextFile << " I'm secret text. ";

    SecretTextFile << "Thanks for Finding me.";

    cout<<endl;

    cout<<endl;

    }

    else

    {

    cout<< "Wrong Password!"<<endl;

    }

    }

    return 0;

    }

    /* You probably noticed I didn't change much from option 1 to option to to have the secret text created in a text file on your desktop. Notice the compare function is in the string library. Notice that ofstream objects have a constructor that accepts a string argument (I think). I don't know all the functions in the string library by memory, but I knew what I was looking for when I googled. I looked for "comparing strings in c++" where i found one of my sources. The function prototypes for the compare function look super confusing but I knew what was going on when I saw the example code. You can try deciphering that on you r own. I hope you liked my source code. */

Still have questions? Get your answers by asking now.