Encrypting any text

I need to write a program that transforms all characters of text typed into std::cin into unreadable text, which then is translated back into the original typed text when run again using the outputted encrypted txt file. The cryptic txt file must be such that no programmer can write a computer program that will translate it back to the original text (i.e. replacing all characters with some other characters in paired fashion through trial and error). Any ideas on the design to achieve this?
Last edited on
a really, really simple way to do this:

1) cin password.
2) make some sort of int mashup of password (sha1 the password, or some other algorithm to mangle the password into an integer). This has to do the same thing every time, same input = same value out!
3)seed your random number generator with the integer from above. this means your random generator will produce the same stream every time for the same password.
4) for every byte in your text, xor the original with the next random value from your generator (the byte out of this is your encrypted data).

the same code will decrypt.

*** your requirement is mutex, you realize. YOUR program is a computer program, and you are a programmer. Therefore if your program can decrypt the message, your requirement fails. But no one else is going to be able to break this easily. You can make it more secure by salting the data, at which point I can't see anyone breaking it in a few thousand years.

** the data from this is not always going to be only printable characters. But its not meant to be read in a text editor, so that would be OK in practice. If your professor insists that the output be readable, then uuencode the output :) The uudecode is back to 'binary' and ready to decrypt again. (uuencode is an ancient algorithm for posting binary data online to text only forums, so I could post an image jpeg here in uuencode format and you could convert it back to a jpeg for example).

you can simplify 1 and 2 to just accept a numeric 64 bit integer password if you want to. Its a bit limiting to the user, but its fine.
Last edited on
This is a straight-up homework assignment.*

Don't worry about being unbreakable. Use the algorithms taught you in class, or use a simple one that you like from the internet. Either way, you should be able to program the cipher yourself in fewer than 20-30 LOC.

Next, you really need a main() that sends the std::cin data through the encrypt or decrypt algorithm and spits it back out on std::cout. Your main() might look something like this:

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
int encrypt( std::istream& ins, std::ostream& outs )
{
  // your stuff here
}

int decrypt( std::istream& ins, std::ostream& outs )
{
  // your stuff here
}

int usage( const std::string& argv0, std::ostream& outs, int exitcode )
{
  auto exename = argv0.substr( argv0.find_last_of( "/\\" ) + 1 );
  outs 
    << "usage:\n  "
    << exename << " help\n  "
    << exename << " encrypt\n  "
    << exename << " decrypt\n  "
    << "Encrypts or decrypts standard input to standard output using the <algorithm> algorithm.\n";
  return exitcode;
}

int main( int argc, char** argv )
{
  if (argc == 2)
    switch (std::toupper( argv[1][0] ))
    {
      case 'H': return usage( argv[0], std::cout, 0 );
      case 'E': return encrypt( std::cin, std::cout );
      case 'D': return decrypt( std::cin, std::cout );
    }
  return usage( argv[0], std::cerr, 1 );
}

Hope this helps.


* If this is not a homework assignment, you really need to do some more research into available cryptographic APIs. Probably the most difficult to grok is MS's, but even that is pretty simple to use.
^^^ if you choose to use xor (or another self-reversing) as your base algorithm, encrypt and decrypt functions are identical.
otherwise, you need the 2 function method.
Last edited on
Topic archived. No new replies allowed.