EDIT: SOLVED. zvbra appears to be right. giving only 4 chars to crypt was causing the problem. Gotta see why is that because i was told getline discarded the last character…
Hello fellow stackers.
I want to start by saying I tried to debug and solve my problem by myself but (probably due to my lack of knowledge on assembly and pretty much anything in general) i failed at doing so.
So I’ve came here in the hope you will be able to help me 🙂 .
I decided to code a simple (I think) encryption program, more of a test than anything else.
It’s divided in 2 parts:
Alghorithm.h (Yeah, I know it’s misspelled.) which contains class ‘Crypter’
#include <iostream>
#include <cmath>
using namespace std;
class Crypter
{
private:
char Crypt[4];
public:
Crypter()
{
getCrypt:
cout<<"\nEnter a 4 character password: ";
cin.getline(Crypt,5);
if (cin.fail())
{
cout<<"\n\nError while reading password. 4 CHARACTERS MAXIMUM.\n\n";
cin.sync(); cin.clear();
goto getCrypt;
}
}
void Encrypt(string& data)
{
for (int i=0; i<data.size();i++)
{
int offset=i;
if (offset>5)
{
while (offset>5)
{
offset-=6;
}
}
float base = 2.0f;
int testbit=pow(base,offset);//gets bit to test. Ex: if offset= 1 (2^1=2=binary 10) test bit is the 2nd least significant bit
if (data[i]!=(data[i]^testbit)) //if testbit is 1 (tested using xOR)
{
data[i]= data[i]^Crypt[2]; //-> encrypt using xOR Crypt[2]
testbit=pow(base,offset+2);
if(data[i]!=data[i]^testbit)// then if testbit is 1 (tested using xOR)
{
data[i]=(data[i]^Crypt[0])+Crypt[3]; //-> encrypt using xOR Crypt[0] then adding crypt[3]
}
else
{
data[i]=(data[i]^Crypt[1])+Crypt[1];//-> encrypt using xOR Crypt[1] then adding crypt[1]
}
}
else
{
data[i]=((data[i]^Crypt[3])^Crypt[0])+Crypt[2];//-> encrypt using xOR Crypt[1] then xOR Crypt[0] and finally adding crypt[1]
}
}
return;
}
};
and main.cpp
#include "Alghorithm.h"
int main()
{
string data="This sentence is going to be encrypted";
Crypter Crypter;
cout<<"\nEncrypting...";
Crypter.Encrypt(data);
cout<<"\n\nEncryption Successful\n\nEncrypted string: "<<data<<endl;
cin.sync(); cin.clear();
}
Now you programming experts must be thinking how clumsy my algorithm is
and that most parts don’t even make sense for a encryption algorithm(which is possible)
but pretend it’s a nice algorithm that makes total sense.
This is the problem:
It compiles. It runs. It encryptes the sentence and prints the encrypted
version. But right before the program ends (in the screenshots you can see i forgot
to return a value in main, but I’ve fixed that and the problem is did not change a bit) a message pops
up(while running through the compiler) in debug mode [Check Screenshots] and 3 in release mode;
Most of them warn about an error caused by a corruption in the stack around Crypter ‘variable’.
I’m not even close to be considered an experienced programmer so I came here asking for your help.
Since the error only pops up when the program is ending does it mean it is a problem with the cleanup?
I’m totaly lost here. Any responses are appreciated.
NOTE: I’ve also included part of the assembly code (the relevant part) provided by te debugger;
Hope it has some use (check my inline comment on the asm code for the ‘position’ of the error);
Criticise my crappy code at will :)( It seems to have a problem since the resultant encrypted string differes from debug to release mode ?:/?.
-João
Screens:
How the program looks like before i press ENTER(Debug): http://i44.tinypic.com/3ert2.png
How the program looks like before i press ENTER(Release) http://i42.tinypic.com/2nld45f.png
Error shown after pressing ENTER (Debug) http://i41.tinypic.com/bhltoy.png
1st Error shown after pressing ENTER (Release) http://i42.tinypic.com/28lchac.png
2nd Error shown after pressing ENTER (Release) http://i40.tinypic.com/2h3ttmw.png
3rd Error shown after pressing ENTER (Release) http://i41.tinypic.com/2lc5suw.png
ASM Code provided by the debbuger http://pastebin.com/TTXUn0T2
At least one error: you need to reserve 5 characters for the
Cryptvariable: you need the additional space to null-terminate the string.