Jump to content


Check out our Community Blogs

Register and join over 40,000 other developers!


Recent Status Updates

View All Updates

Photo
- - - - -

i could really use some help with this.

encryption

  • Please log in to reply
8 replies to this topic

#1 jj90

jj90

    CC Lurker

  • New Member
  • Pip
  • 4 posts

Posted 29 March 2012 - 03:27 AM

so in uni and i have an x86 assembly program to write. i have the program wrote however i need to tweak the decryption part of it so that it works correctly.

i would appreciate a more trained eye to see if they cansee my problem.

heres the code that is most important and iv attached the .cpp file too. thanks for help in advance

encrypt:

void encrypt_chars (int length, char EKey)
{   char temp_char;                        // original/encrypted char temporary store

    for (int i = 0; i < length; i++){
        temp_char = OChars [i];            // get next char from original string
        __asm {                            // call the encrypt subroutine
            push   eax                    // save register values on stack to be safe
            push   ecx
            movsx  ecx,temp_char        // enregister the source character
            movsx  eax,EKey                // and encryption key.
            call   encrypt7                // encrypt the character
            mov    temp_char,al            // only need lower byte of EAX to return encrypted char
            pop    ecx                    // restore original register values from stack
            pop    eax
        }
        EChars [i] = temp_char;            // Store encrypted char in the encrypted chars array
    }
   return;

   __asm {

//EAX = Encryption Key
//ECX = the character to be encrypted.
// Outputs: register EAX = the encrypted value of the source character.

    //encrypt:
        //mov eax,ecx        // get character
        //inc eax            // simply add 1 to character! EKey value not used in this simple version.
        //ret

encrypt7:mov edi,eax    //puts key(a) into edi
          sub ecx, 0x0A  //subtracts 10 from ecx
         push ecx       //stores ecx to the stack
          and eax,0xAA   //ands the value of eax with 170
          not eax        //inverts the new value of eax
          mov edx,eax    //moves the eax value into the edx register
          mov eax,edi    //moves edi (key a) to eax
          or  eax,0xAA   // ors value of eax with 170 puts result in edx
          add eax,edx    //ands eax with edx
          mov edx,eax    //moves edx to eax
          pop eax        //retreives the top value from the stack and puts it in eax
          xor eax,edx    //exclusivly ors eax with edx
         rol al,1       //rotates al register 1 place to the left in binary
         rol al,1       //rotates al register 1 place to the left in binary
          ret           //returns the value

decrypt:
void decrypt_chars (int length, char EKey)
{   char temp_char;                        // original/encrypted char temporary store

    for (int i = 0; i < length; i++){
        temp_char = EChars [i];            // get next char from original string
        __asm {                            // call the encrypt subroutine
            push   eax                    // save register values on stack to be safe
            push   ecx
            movsx  ecx,temp_char        // enregister the source character
            movsx  eax,EKey                // and encryption key.
            call   decrypt7                // encrypt the character
            mov    temp_char,al            // only need lower byte of EAX to return encrypted char
            pop    ecx                    // restore original register values from stack
            pop    eax
        }
        DChars [i] = temp_char;            // Store encrypted char in the encrypted chars array    }
   return;
   
// ---------------------------- start of ASM code ---------------------------------------------------------------
   __asm {

decrypt7:   push eax 
            push ecx 
            and eax,0x1C
            rol eax,2
            inc eax 
            mov edx,eax 
            pop eax              
            rol al,1 
            or eax,edx
            pop ecx 
            xor eax,ecx 
                        ret

Attached Files

  • Attached File  main.cpp   6.36KB   368 downloads

Edited by dargueta, 29 March 2012 - 06:25 PM.
Please use code tags!

  • 0

#2 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 29 March 2012 - 06:26 PM

Why are you using assembly language for this?
  • 0

sudo rm -rf / && echo $'Sanitize your inputs!'


#3 jj90

jj90

    CC Lurker

  • New Member
  • Pip
  • 4 posts

Posted 30 March 2012 - 11:30 AM

i have no idea as the first and only assignment its a *****

Edited by dargueta, 30 March 2012 - 04:32 PM.
No profanity.

  • 0

#4 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 30 March 2012 - 04:54 PM

Please don't use profanity. We have rules against this.

So, about your code:
void get_char (char& a_character)
{
    [B][COLOR=#ff0000]cin >>[/COLOR][/B] a_character;
    while (((a_character < '0') [B][COLOR=#ff0000]|[/COLOR][/B] (a_character > 'z')) && (a_character != dollarchar))
    {    cout << "Alphanumeric characters only, please try again > ";
        cin >> a_character;
    }
}

1) You should use cin.get(), not the >> operator. >> can return an entire string; if you try putting that into a character pointer...boom.

2) Your while condition is wrong, as 0-9, A-Z and a-z are not contiguous. If you look at an ASCII table, 0-9 correspond to character codes 48-57; A-Z are 65-90, and a-z are 97-122. Instead, include cctype and use the function isalnum().

3) Be careful with your operators. | is the bitwise OR operator, || is a logical OR operator. Same goes with & and &&. They're not the same. If you need me to explain the difference in more detail, just ask.

4) I'd advise using a do-while loop instead, since this always executes once.

void get_original_chars (int& length)
{    char next_char;
    length = 0;
    get_char (next_char);


    while ((length < MAXCHARS) && (next_char != dollarchar))
    {
        [COLOR=#ff0000]OChars[/COLOR] [length++] = next_char;
        get_char (next_char);
    }
}

1) Don't use global variables unless you absolutely have to. Instead, add another parameter that takes a pointer to the necessary variable. Using global variables is just asking for trouble. If you rename a variable, or declare a local variable that has the same name as the global one...problems.

2) Instead of declaring OChars as char OChars[MAX_LENGTH], declare it with a size of MAX_LENGTH + 1. That way you can add a null at the end of the string and won't have to use show_string().For example:
void get_original_chars(int& length)
{
    // blah...
    // loop ends here. length was already incremented to the next position so we don't add 1 here.
    OChars[length] = '\0';
}

Since C strings are always terminated with a null, you can now do cout << OChars instead of show_string(). In fact, you can delete that function entirely.


Now for the assembly language part. In the instructions it says the algorithm just adds one to the character. What you're doing seems unnecessarily complicated. What exactly are you trying to do with the whole ROL/ROR and AND stuff?
  • 0

sudo rm -rf / && echo $'Sanitize your inputs!'


#5 jj90

jj90

    CC Lurker

  • New Member
  • Pip
  • 4 posts

Posted 31 March 2012 - 06:51 PM

sorry about that, just been getting pretty annoyed with this thing.

wow, great suggestions. ill have a go at chopping and changing the c++ after the weekend. :o)

and the asm im a little lost with. the .cpp file works as it should up to the asm decryption. my encryption key is 'a' and from what i know it does 2 changes to the key before applying it to the character in the string. but after spending hours staring at debugging registers and outputs to see the changing values. the closest i came to getting my original string was a random selection of lower case letters (better than the obscure symbols ive had, i suppose)

if that answered your question.
  • 0

#6 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 31 March 2012 - 08:49 PM

Okay, but how does it apply to the character in the string?
  • 0

sudo rm -rf / && echo $'Sanitize your inputs!'


#7 jj90

jj90

    CC Lurker

  • New Member
  • Pip
  • 4 posts

Posted 01 April 2012 - 02:37 AM

only having a basic knowledge i would say that the asm routine just manipulates the registers. i don't know for sure what it actually does to each character however
  • 0

#8 steven

steven

    CC Lurker

  • Member
  • Pip
  • 9 posts
  • Location:England

Posted 05 April 2012 - 07:40 AM

1) You should use cin.get(), not the >> operator. >> can return an entire string; if you try putting that into a character pointer...boom.


It was fine as it was passed by reference. It would skip input though.
  • 0

#9 dargueta

dargueta

    I chown trolls.

  • Moderator
  • 4854 posts
  • Programming Language:C, Java, C++, PHP, Python, JavaScript, Perl, Assembly, Bash, Others
  • Learning:Objective-C

Posted 05 April 2012 - 08:52 AM

Oh yeah...template functions and stuff. So no "boom," but I imagine it would skip whitespace and other delimiters like the >> operator does. Either way, you can't go wrong with get().
  • 0

sudo rm -rf / && echo $'Sanitize your inputs!'






Also tagged with one or more of these keywords: encryption

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download