Continue to Site

Eng-Tips is the largest engineering community on the Internet

Intelligent Work Forums for Engineering Professionals

  • Congratulations GregLocock on being selected by the Eng-Tips community for having the most helpful posts in the forums last week. Way to Go!

Need C++ unencrypt text file (school)

Status
Not open for further replies.

sprintcar

Mechanical
Oct 16, 2001
763
Taking c++ class at nite. Program encrypts text file by offsetting char by some integer. 2nd program reverses by same value to original text. Runs fine. Instructor (another engineer) wants to decode a text file w/ unknown integer value offset. I did one that searches +/- 30 integers with cout<< for user to review.

Assuming its predominately letters (cap and lower case) could you count the occurences of each char, take the ASCII value of the one with max value, set it to 'e'. This offset value would then be applied to the text file and cout<< the text for user approval? There's a letter use hierarchy in English that would apply. Seems like a LOT of Code.
Anybody have a shortcut? Thanks!! (this is an extra credit and boy, do I need it!)
 
Replies continue below

Recommended for you

Scan for any characters greater than 7F hex (Del) these are not part of the standard ASCII character set. Or look for characters less than 20 Hex (space) one generally will not find too many of these in a text document.

One aproach worth investigating:
1. Scan the document find the lowest value.
2. Subtract 20 hex from this value this. (This is probally your offset.)

Rich
 
Thanks Rich!
you could probably write this in your sleep. We haven't gotten to hex conversion, but I think its in the book. I'll give this a try.
I did find a lot of resource at
 
If the text file is a large one, then it would presumably have a carriage return and linefeed 0D 0A hex in that order, beginning with the first two characters, check the value of the following letter, then each successive letter. When you find a difference of 03 hex (also account for higher ascii codes i.e. 0D + F4 = 01 and 0A + F4 = FE) When you find a match, subtract (or add) the byte value from its presumed value and apply that across the file. This should work on most files.

Another tidbit, most text editors append a carriage return and linefeed ( 0D 0A ) as the last 2 bytes of a file, occasionally you will find one that has appended ( 0D 0A 1A ), but those are rare. Wordpad and Notepad all append the 0D 0A combination.

Hope this helps....

 
Hi,

A couple of thoughts.

1. Regarding the post above. It is possible the text will contain Carriage Returns (ASCII 13) and Line Feeds (10)

If it does, scan the whole text for the lowest value, then subtract 10 away from it then you probably have the difference.

If not then as the previous post stated look for spaces. i.e. scan the text, find the lowest and subtract 32 (the ASCII for SPACE) the result being the difference from which you subtract from the rest of the characters.

2. If the above doesn't work the the word 'the' is pretty common, and it usually is prefixed and ends with a space. If you are looking for the ASCII of the word ' the ' then it would be 32 116 104 101 32.

Now in the simplest of coding (adding an offset) the difference between each letter would be the same if all letters were added an offset to. So what you could search for is the difference between succesive characters ie
84 12 3 69 and make sure that the first and last caharcters are equal. Then to find the difference just subtract 32 from the first character value in the ' the ' string.
Any help ?, yes no let me know.

Regards
 
Thanks Guys!! Any quick code for the SCAN function and a way to isolate the value?? Remember, this is C++ 101 and I'm a Mech Eng, not a computer wiz.
Looking at the above ideas, I've been finding a lot of things in the text that the instructor hasn't even gotten close to. I got searching for the scan syntax until 2:30 this morning without a result.

This instructor thinks He's the answer to c++
 
Thanks to all your responses - and a few LONG nites - I used SEEKG to find last char in the file- did the offset based on either period, ! or ? (good punctuation) then WHILE looped those three iterations to COUT<<.
Not a trick as you guys could do, but a heck of a learning experience!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor