Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // The encryption program in C++ and ASM with a very simple encryption method - it simply adds 1 to the character.
- // The encryption method is written in ASM. You will replace this with your allocated version for the assignment.
- // In this version parameters are passed via registers (see 'encrypt' for details).
- // Filename: "4473 FoCA 2015 Encryption Original with ASM.cpp"
- //
- // Kieran Willis, last update: Mar 2015
- // *** PLEASE CHANGE THE LINE ABOVE TO YOUR NAME & DATE ***
- #include <conio.h> // for kbhit
- #include <iostream> // for cin >> and cout <<
- #include <iomanip> // for fancy output
- using namespace std;
- #define MAXCHARS 6 // feel free to alter this, but 6 is the minimum
- #define dollarchar '$' // string terminator
- char OChars[MAXCHARS],
- EChars[MAXCHARS],
- DChars[MAXCHARS]; // Global Original, Encrypted, Decrypted character strings
- //----------------------------- C++ Functions ----------------------------------------------------------
- void get_char (char& a_character)
- {
- cin >> a_character;
- while (((a_character < '0') | (a_character > 'z')) && (a_character != dollarchar))
- { cout << "Alphanumeric characters only, please try again > ";
- cin >> a_character;
- }
- }
- //-------------------------------------------------------------------------------------------------------------
- void get_original_chars (int& length)
- { char next_char;
- length = 0;
- get_char (next_char);
- while ((length < MAXCHARS) && (next_char != dollarchar))
- {
- OChars [length++] = next_char;
- get_char (next_char);
- }
- }
- //---------------------------------------------------------------------------------------------------------------
- //----------------- ENCRYPTION ROUTINES -------------------------------------------------------------------------
- void encrypt_chars (int length, char EKey)
- { char temp_char; // char temporary store
- for (int i = 0; i < length; i++) // encrypt characters one at a time
- {
- temp_char = OChars [i]; //
- __asm { //
- push eax // save register values on stack to be safe
- push ecx //
- //
- movzx ecx,temp_char // set up registers (Nb this isn't StdCall or Cdecl)
- lea eax,EKey //
- // caller - pushing the values of parameters (before calling main encryption)
- push ecx //copies first letter of string in to ESP
- push eax
- call encrypt2 // encrypt the character
- add esp, 8
- mov temp_char,al
- //
- pop ecx // restore original register values from stack
- pop eax //
- }
- EChars [i] = temp_char; // Store encrypted char in the encrypted chars array
- }
- return;
- // Encrypt subroutine. You should paste in the encryption routine you've been allocated from Bb and
- // overwrite this initial, simple, version. Ensure you change the ‘call’ above to use the
- // correct 'encryptnn' label where nn is your encryption routine number.
- // Inputs: register EAX = 32-bit address of Ekey,
- // ECX = the character to be encrypted (in the low 8-bit field, CL).
- // Output: register EAX = the encrypted value of the source character (in the low 8-bit field, AL).
- __asm {
- encrypt2:
- //registers destroyed at end = ecx, edx
- push ebp // preserve ebp on the stack
- mov ebp, esp // set ebp's new fixed value from esp
- push ecx // preserve ecx on the stack
- mov ecx, [ebp + 12] // move address of stack in to ecx
- mov eax, [ebp + 8]
- xor edx,edx // set edx to 0 - fast and efficient
- mov dl,byte ptr[eax] // store byte pointed at by eax in to dl dl contains ekay
- and dl,0xF3 // perform logical and with immediate hex value F3
- ror dl,1 // rotates dl right once
- ror dl,1 // rotates dl right once
- ror dl,1 // rotates dl right once - alters ekey
- ror dl,1 // rotates dl right once
- inc dl // adds 1 to the value of dl
- pop ecx // takes top element in the stack and places it in to ecx register (was never modified, no change)
- add ecx,edx // add the contents of edx to ecx
- mov byte ptr [eax],dl // store byte value of dl in memory pointed at by eax - puts the ekey in dl back in to the address of eax
- ror cl,1 // rotates cl right once
- mov eax,ecx // takes value in ecx register and places the value in eax register
- mov esp, ebp
- pop ebp //place original value of ebp back in to ebp from the stack
- ret // return from subroutine (16 due to 4 parameters)
- }
- //--- End of Assembly code
- }
- // end of encrypt_chars function
- //---------------------------------------------------------------------------------------------------------------
- //---------------------------------------------------------------------------------------------------------------
- //----------------- DECRYPTION ROUTINES -------------------------------------------------------------------------
- //
- void decrypt_chars (int length, char EKey)
- { char temp_char; // char temporary store
- for (int i = 0; i < length; i++) // encrypt characters one at a time
- {
- temp_char = EChars[i]; //
- __asm { //
- push eax // save register values on stack to be safe
- push ecx //
- //
- movzx ecx, temp_char // set up registers (Nb this isn't StdCall or Cdecl)
- lea eax, EKey //
- // caller - pushing the values of parameters (before calling main encryption)
- mov edx, [ebp + 8] //copies address value of eKey in to edx
- push eax //save value of eax on to the stack
- push ecx //save value of ecx on to the stack
- push edx //save value of edx on to the stack
- call decrypt2 // encrypt the character
- //add esp, 8
- mov temp_char, al //
- //
- pop ecx // restore original register values from stack
- pop eax //
- }
- DChars[i] = temp_char; // Store encrypted char in the encrypted chars array
- }
- return;
- // Encrypt subroutine. You should paste in the encryption routine you've been allocated from Bb and
- // overwrite this initial, simple, version. Ensure you change the ‘call’ above to use the
- // correct 'encryptnn' label where nn is your encryption routine number.
- // Inputs: register EAX = 32-bit address of Ekey,
- // ECX = the character to be encrypted (in the low 8-bit field, CL).
- // Output: register EAX = the encrypted value of the source character (in the low 8-bit field, AL).
- __asm {
- decrypt2:
- //registers destroyed at end = ecx, edx
- push ebp
- mov ebp, esp
- mov edx, [ebp + 12]
- push ecx
- xor edx, edx
- mov dl, byte ptr[eax]
- and dl, 0xF3
- rol cl, 1 //changed for decrypt
- ror dl, 1
- ror dl, 1
- ror dl, 1
- ror dl, 1
- inc dl
- pop ecx
- sub ecx, edx //changed for decrypt
- mov byte ptr[eax], dl
- mov eax, ecx
- pop ebp
- ret 12// causing break problem- prev ret 12 - current 8
- }
- }
- // end of decrypt_chars function
- //---------------------------------------------------------------------------------------------------------------
- int main(void)
- {
- int char_count; // The number of actual characters entered (upto MAXCHARS limit).
- char EKey; // Encryption key.
- cout << "\nPlease enter your Encryption Key (EKey) letter: "; get_char (EKey);
- cout << "\nNow enter upto " << MAXCHARS << " alphanumeric characters:\n";
- get_original_chars (char_count);
- cout << "\n\nOriginal source string = " << OChars << "\tHex = ";
- for (int i=0; i<char_count; i++) cout<<hex<<setw(2)<<setfill('0')<< ((int (OChars[i]))&0xFF)<<" ";
- encrypt_chars (char_count, EKey);
- cout << "\n\nEncrypted string = " << EChars << "\tHex = ";
- for (int i=0; i<char_count; i++) cout<< ((int (EChars[i]))&0xFF)<<" ";
- decrypt_chars (char_count, EKey);
- cout << "\n\nDecrypted string = " << DChars << "\tHex = ";
- for (int i=0; i<char_count; i++) cout<< ((int (DChars[i]))&0xFF)<<" ";
- cout << "\n\nPress a key to end...";
- while ( !_kbhit()); //hold the screen until a key is pressed
- return (0);
- } // end of whole encryption/decryption program --------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement