Skip to main content
gamedev.net
🔒 Locked

Encryption

Started by brandonman Jul 31, 2007 at 4:52 PM 18 replies 1.5k views
Original Post
brandonman
brandonman
Hi everyone. I'm trying to write some simple encryption code. I have it working but When I write it to a file, I just get 102, completely different than what it creates. Heres my code:

#include <iostream>
#include <fstream>
using namespace std;

int i;
ofstream myfile;
int main()
{
char str[100] = "brandon rules! Did you Know that?";
for(int i=0;i<strlen(str);i++)
{

  myfile.open ("encrypted.txt");
str+=5/3*2;
cout << str;
myfile << str;
}
myfile.close();
system("PAUSE");
}


rozz666
rozz666
You don't need global int i, since you've got another in the loop.
myfile should be opened once, so move it out of the loop.
str += 5/3*2 means str += 2;


brandonman
brandonman
now nothing comes up in the text file.
Palidine
Palidine
Quote:
Original post by brandonman
now nothing comes up in the text file.


post code
kohlrak
kohlrak
Here's another problem...

5/3*2=3.6666666666666666666666666666667

My as well just plus 3... Another idea would be to do something like this...

for (loc=0; str[loc]!=0; loc++)	str[loc]=str[loc]^'@';


Then print out the string. Note that i chose @ for it's not used often, if you try to read in @ after encoding, you'll have problems because it'll become 0. Also, if you encode it then print the whole string all at once, you'll notice your performance go up a little bit, but that's for other functions. I've noticed that for some odd reson C++ functions are slower. Either way, printing it out after each calc is slow, but if you do both things seperately in one big operation, your performance takes a boost. Of course, you could always speed that up a bit by treating 4 chars as an int rather than doing 1 char at a time. For this project you may not care, but for dealing with larger strings (encrpyting a large 2 gig file or something) it will mean all the differance. I can say that from experience.

Quote:
As a general rule, if you post in For Beginners and your code contains the word 'char', you have a bug. std::string roxors teh big one one one one.


There's a reason i prefer arrays over that awkward thing. =p
smitty1276
smitty1276
Quote:
Original post by kohlrak
Here's another problem...

5/3*2=3.6666666666666666666666666666667

My as well just plus 3...
I know this was already noted above, but that statement is incorrect.

5/3*2 ==> ((int)5/(int)3)*2 ==> (1)*2 ==> 2




kohlrak
kohlrak
Depending on the compiler it might notice the accurecy loss of doing the division first and note that division and multiplication have a property where you get the same results if you switch the two (outside of the computers' limitation that is) and just do the mul first, resulting in the 3.

Also, some compilers will even check the overflow flag in the EIP register (or the FPU's equivalent) or look at edx to make sure it didn't overflow, and if it did, throw an exception. characters 255, 254, and 253 are in danger because of that, not that you'll use them anytime soon anyway.
brandonman
brandonman
Quote:
Original post by kohlrak
Here's another problem...

5/3*2=3.6666666666666666666666666666667

My as well just plus 3... Another idea would be to do something like this...

for (loc=0; str[loc]!=0; loc++)	str[loc]=str[loc]^'@';


Then print out the string. Note that i chose @ for it's not used often, if you try to read in @ after encoding, you'll have problems because it'll become 0. Also, if you encode it then print the whole string all at once, you'll notice your performance go up a little bit, but that's for other functions. I've noticed that for some odd reson C++ functions are slower. Either way, printing it out after each calc is slow, but if you do both things seperately in one big operation, your performance takes a boost. Of course, you could always speed that up a bit by treating 4 chars as an int rather than doing 1 char at a time. For this project you may not care, but for dealing with larger strings (encrpyting a large 2 gig file or something) it will mean all the differance. I can say that from experience.

Quote:
As a general rule, if you post in For Beginners and your code contains the word 'char', you have a bug. std::string roxors teh big one one one one.


There's a reason i prefer arrays over that awkward thing. =p


didn't do anything.
code:
#include <iostream>#include <fstream>using namespace std;int loc;int i;ofstream myfile;int main(){  myfile.open ("encrypted.txt");    char str[100] = "brandon rules! Did you Know that?";for (loc=0; str[loc]!=0; loc++){	str[loc]=str[loc]^'@';myfile.open ("encrypted.txt");cout << str[loc];myfile << str[loc];}myfile.close();cout << endl; cout << endl;system("PAUSE");}
smitty1276
smitty1276
You're opening the file every pass in the loop. Didn't somebody already tell you to take that out?
brandonman
brandonman
still nothing.
smitty1276
smitty1276
Quote:
Original post by kohlrak
Depending on the compiler it might notice the accurecy loss of doing the division first and note that division and multiplication have a property where you get the same results if you switch the two (outside of the computers' limitation that is) and just do the mul first, resulting in the 3.
This is, perhaps, off topic, but no standards-compliant compiler will do that. There are very specific order-of-operation rules, and the binary division and multiplication operators have the same precedence, operating left to right.

5/3*2 will ALWAYS evaluate to 2.

To the OP, if you're actually modifying the original string anyway, why not:

int main(){  char str[100] = "brandon rules! Did you Know that?";  for(unsigned int i=0;i<strlen(str);i++)    str ^= '@'; //<-- put whatever you want there for your encrypt function  myFile.open("encrypted.txt");  myFile << str;  myFile.close}
brandonman
brandonman
that worked! Thanks.
smitty1276
smitty1276
Sorry, I should have looked more closely at your code before copying lines out of it...

You need to use

myFile.open("encrypted.txt", std::ios::app);

That's to append. You can also use std::ios::out.
Antheus
Antheus
And with the magic of standard library:
#include <fstream>#include <string>#include <algorithm>char encrypt( char c ) { return c ^ '@'; }int main(int argc, char* argv[]){	std::string s("Hello");	std::ofstream f("d:\\encrypted.txt");	std::transform( s.begin(), s.end(), std::ostream_iterator<char>(f), encrypt);}

Zahlman
Zahlman
Quote:
Original post by kohlrak
Depending on the compiler it might notice the accurecy loss of doing the division first and note that division and multiplication have a property where you get the same results if you switch the two (outside of the computers' limitation that is) and just do the mul first, resulting in the 3.


Wrong.

The compiler does NOT have that freedom. This much of the behaviour is standardized. Integer arithmetic is not like floating point arithmetic: it must be treated strictly, because the "round-off errors" are large and frequently deliberate.

E.g.

template <unsigned int tilesize>unsigned int pixelToTile(unsigned int i) {  // Although I would usually put parentheses anyway, because things like this  // have a habit of "looking wrong", especially when sleep-deprived  return i / tilesize * tilesize;}// A really smart optimizing compiler might transform that into a bitmask when// the template is instantiated with a power-of-two tilesize, I guess...


Quote:
Also, some compilers will even check the overflow flag in the EIP register (or the FPU's equivalent) or look at edx to make sure it didn't overflow, and if it did, throw an exception. characters 255, 254, and 253 are in danger because of that, not that you'll use them anytime soon anyway.


I have no idea what you're even talking about here. "EIP" on what processor? Characters 255-253 of what?
kohlrak
kohlrak
Quote:
I have no idea what you're even talking about here. "EIP" on what processor?


Intel. It's not an intel exception thing, but i've noticed that visual C++ 2005 (i think that's the version i was using in class) threw in code to throw in a runtime error if there's an overflow.

Quote:
Characters 255-253 of what?


The char being encoded.

255+2=1 (overflow set)
254+2=0 (overflow set)

I figured this out because one time i was goofing off in class (i was taking the course for fun since i knew C++ before entering) so i was purpously overflowing variables (i completely forget why i was doing it, but it actually had a constructive purpose, and it certainly wasn't encoding), but then the program crashed and when i went in with the debugger it complained about an overflow.

Quote:
This much of the behaviour is standardized.


A compiler is not restricted to the standards, especially when the maker forgets a standard or two.
Antheus
Antheus
Quote:
Original post by kohlrak

Quote:
This much of the behaviour is standardized.


A compiler is not restricted to the standards, especially when the maker forgets a standard or two.


I believe you might be thinking of C# compiler, not C++. That one has a switch for overflow testing, and throws an exception.
smitty1276
smitty1276
Quote:
Original post by kohlrak
A compiler is not restricted to the standards, especially when the maker forgets a standard or two.
If it wants to be called a C++ compiler, it will adhere to the standards as closely as possible. NO COMPILER that claims to be a C++ compiler would totally screw up operator precedence the way you are suggesting.

kohlrak
kohlrak
Quote:
I believe you might be thinking of C# compiler, not C++. That one has a switch for overflow testing, and throws an exception.


Nope. I had gotten these erors in C++. The school dosn't use C#. It was a problem specific to that version of visual C++, because the compiler version right before it (6) didn't have that problem.

Quote:
If it wants to be called a C++ compiler, it will adhere to the standards as closely as possible. NO COMPILER that claims to be a C++ compiler would totally screw up operator precedence the way you are suggesting.


They can claim it results in better accurecy, and lie that it follows standards. Considering i really don't like C/C++, i never did search for such a compiler, so i don't have one as an example for you.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.