Tutorial on how to manipulate a text file using the ASCii value?

Hello, I am looking to create a line of code that reads in a text file and then manipulates the ASCII value of the string to unscramble the text found in the file; and for it to output the text in another text file. I have been unable to find a tutorial on how to create this line of code, so does there exist one? I do not have the line of code typed out as of right now.

Thank You.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
std::ifstreram input("filename");
//to read word by word
std::string word;
while(input >> word){
  //...
}
//to read line by line
std::string line;
while(std::getline(input, line)){
   //...
}
//to copy all the content to a string
// https://stackoverflow.com/questions/2602013/read-whole-ascii-file-into-c-stdstring
std::stringstream buffer;
buffer << input.rdbuf();
std::string file = buffer.str();


> to unscramble the text
I don't understand what you mean
Last edited on
define unscramble. Clearly you want an encryption / decryption program, but there are literally billions of ways you can do that from the simple (caeser cypher type toys) to the weird (xor with random) to the robust (salted hashes / product of primes blah blah) and more.

is the code already encrypted?

a really simple algorithm that is pretty solid is this:
read in a key. turn that key from text into an integer.
seed random with that integer.
xor each character with the next random number from your random sequence.

exact same code (with the right key (password)) will decrypt it as well.
you may get unprintable / gibberish ascii values in the encrypted text. If you only want readable characters, you may need to xor with a value < 127, to do that %127 will work, or you may want to add 20 or so as well, to get rid of the low level ascii codes, you can adapt it as needed.


working with the ascii table is dead simple.
pretend unsigned char is an integer from 0 to 255. you have a value input (scrambled or not, its irrelevant) and a function y=f(x) ... and the reverse of that x= invf(y) ... numbers in, numbers out. the letters will take care of themselves when you print it out to file or screen. The only real complexity, as I said above, is if you want to force the data to skip over the ranges where the characters are not printable (there are a dozen or so codes that cant be printed in the ascii table, for old serial port legacy info, zero string terminal, and so on).
Last edited on
Topic archived. No new replies allowed.