Note that there are some explanatory texts on larger screens.

plurals
  1. POCorrupted stack; Bit manipulation with strings; MSVC++
    primarykey
    data
    text
    <p>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...</p> <p>Hello fellow stackers.</p> <p>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 :) .</p> <p>I decided to code a simple (I think) encryption program, more of a test than anything else.</p> <p>It's divided in 2 parts:</p> <p>Alghorithm.h (Yeah, I know it's misspelled.) which contains class 'Crypter'</p> <pre><code>#include &lt;iostream&gt; #include &lt;cmath&gt; using namespace std; class Crypter { private: char Crypt[4]; public: Crypter() { getCrypt: cout&lt;&lt;"\nEnter a 4 character password: "; cin.getline(Crypt,5); if (cin.fail()) { cout&lt;&lt;"\n\nError while reading password. 4 CHARACTERS MAXIMUM.\n\n"; cin.sync(); cin.clear(); goto getCrypt; } } void Encrypt(string&amp; data) { for (int i=0; i&lt;data.size();i++) { int offset=i; if (offset&gt;5) { while (offset&gt;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]; //-&gt; 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]; //-&gt; encrypt using xOR Crypt[0] then adding crypt[3] } else { data[i]=(data[i]^Crypt[1])+Crypt[1];//-&gt; encrypt using xOR Crypt[1] then adding crypt[1] } } else { data[i]=((data[i]^Crypt[3])^Crypt[0])+Crypt[2];//-&gt; encrypt using xOR Crypt[1] then xOR Crypt[0] and finally adding crypt[1] } } return; } }; </code></pre> <p>and main.cpp </p> <pre><code>#include "Alghorithm.h" int main() { string data="This sentence is going to be encrypted"; Crypter Crypter; cout&lt;&lt;"\nEncrypting..."; Crypter.Encrypt(data); cout&lt;&lt;"\n\nEncryption Successful\n\nEncrypted string: "&lt;&lt;data&lt;&lt;endl; cin.sync(); cin.clear(); } </code></pre> <p>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.</p> <p>This is the problem:</p> <p>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;</p> <p>Most of them warn about an error caused by a corruption in the stack around Crypter 'variable'.</p> <p>I'm not even close to be considered an experienced programmer so I came here asking for your help.</p> <p>Since the error only pops up when the program is ending does it mean it is a problem with the cleanup?</p> <p>I'm totaly lost here. Any responses are appreciated.</p> <p>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);</p> <p>Criticise my crappy code at will :)( It seems to have a problem since the resultant encrypted string differes from debug to release mode ?:/?.</p> <p>-João</p> <p>Screens:</p> <p><em>How the program looks like before i press ENTER(Debug)</em>: <a href="http://i44.tinypic.com/3ert2.png" rel="nofollow">http://i44.tinypic.com/3ert2.png</a></p> <p><em>How the program looks like before i press ENTER(Release)</em> <a href="http://i42.tinypic.com/2nld45f.png" rel="nofollow">http://i42.tinypic.com/2nld45f.png</a></p> <p><em>Error shown after pressing ENTER (Debug)</em> <a href="http://i41.tinypic.com/bhltoy.png" rel="nofollow">http://i41.tinypic.com/bhltoy.png</a></p> <p><em>1st Error shown after pressing ENTER (Release)</em> <a href="http://i42.tinypic.com/28lchac.png" rel="nofollow">http://i42.tinypic.com/28lchac.png</a></p> <p><em>2nd Error shown after pressing ENTER (Release)</em> <a href="http://i40.tinypic.com/2h3ttmw.png" rel="nofollow">http://i40.tinypic.com/2h3ttmw.png</a></p> <p><em>3rd Error shown after pressing ENTER (Release)</em> <a href="http://i41.tinypic.com/2lc5suw.png" rel="nofollow">http://i41.tinypic.com/2lc5suw.png</a></p> <p><em>ASM Code provided by the debbuger</em> <a href="http://pastebin.com/TTXUn0T2" rel="nofollow">http://pastebin.com/TTXUn0T2</a></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload