Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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] = "Soon!"; // 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;
- __asm
- {
- mov DWORD PTR[ebp - 4], 0
- jmp L2
- L3:
- mov eax, DWORD PTR[ebp - 4]
- movzx eax, BYTE PTR EChars[eax]
- mov BYTE PTR[ebp - 5], al
- // OG code
- push eax // Save values contained within register to stack
- push ecx
- movzx ecx, temp_char
- push ecx // Push argument #2
- lea eax, EKey
- push eax // Push argument #1
- call encrypt
- add esp, 8 // Clean parameters of stack
- mov temp_char, al // Move the temp character into a register
- pop ecx
- pop eax
- // end of OG code
- mov eax, DWORD PTR[ebp - 4]
- movzx edx, BYTE PTR[ebp - 5]
- mov BYTE PTR DChars[eax], dl
- L2:
- mov eax, DWORD PTR[ebp - 4]
- cmp eax, DWORD PTR[ebp - 20]
- jl L3
- }
- return;
- __asm
- {
- encrypt:
- push ebp // Set stack
- mov ebp, esp // Set up the base pointer
- mov eax, [ebp + 8] // Move value of parameter 1 into EAX
- mov ecx, [ebp + 12] // Move value of parameter 2 into ECX
- push edi // Used for string and memory array copying
- push ecx // Loop counter for pushing character onto stack
- not byte ptr[eax] // Negation
- add byte ptr[eax], 0x04 // Adds hex 4 to EKey
- movzx edi, byte ptr[eax] // Moves value of EKey into EDI using zeroes
- pop eax // Pop the character value from stack
- xor eax, edi // XOR character to give encrypted value of source
- pop edi // Pop original address of EDI from the stack
- rol al, 1 // Rotates the encrypted value of source by 1 bit (left)
- rol al, 1 // Rotates the encrypted value of source by 1 bit (left) again
- add al, 0x04 // Adds hex 4 to encrypted value of source
- mov esp, ebp // Deallocate values
- pop ebp // Restore the base pointer
- ret
- }
- //--- End of Assembly code
- }
- // end of encrypt_chars function
- //---------------------------------------------------------------------------------------------------------------
- //---------------------------------------------------------------------------------------------------------------
- //----------------- DECRYPTION ROUTINES -------------------------------------------------------------------------
- //
- void decrypt_chars(int length, char EKey)
- {
- char temp_char;
- for (int i = 0; i < length; i++) // decrypt characters one at a time
- {
- temp_char = EChars[i]; // temp_char now contains the address values of the individual character
- __asm
- {
- push eax // Save values contained within register to stack
- push ecx
- movzx ecx, temp_char
- push ecx // Push argument #2
- lea eax, EKey
- push eax // Push argument #1
- call decrypt
- add esp, 8 // Clean parameters of stack
- mov temp_char, al // Move the temp character into a register
- pop ecx
- pop eax
- }
- DChars[i] = temp_char; // Store decrypted char in the decrypted chars array
- }
- return;
- __asm
- {
- decrypt:
- push ebp // Set stack
- mov ebp, esp // Set up the base pointer
- mov eax, [ebp + 8] // Move value of parameter 1 into EAX
- mov ecx, [ebp + 12] // Move value of parameter 2 into ECX
- push edi // Used for string and memory array copying
- push ecx // Loop counter for pushing character onto stack
- not byte ptr[eax] // Negation of actual EKey
- add byte ptr[eax], 0x04 // Adds hex 4 to actual EKey
- movzx edi, byte ptr[eax] // Moves value of EKey into EDI using zeroes
- pop eax // Pop the character value from stack
- sub al, 0x04 // Subtract hex 4 to encrypted value of source
- ror al, 1 // Rotates the encrypted value of source by 1 bit (right)
- ror al, 1 // Rotates the encrypted value of source by 1 bit (right) again
- xor eax, edi // XOR character to give decrypted value of source
- pop edi // Pop original value of EDI from the stack
- mov esp, ebp // Deallocate values
- pop ebp // Restore the base pointer
- ret
- }
- }
- // 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