Guest User

code

a guest
Apr 28th, 2015
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.39 KB | None | 0 0
  1. #include <conio.h>              // for kbhit
  2. #include <iostream>             // for cin >> and cout <<
  3. #include <iomanip>              // for fancy output
  4. using namespace std;
  5.  
  6. #define MAXCHARS 6              // feel free to alter this, but 6 is the minimum
  7. #define dollarchar '$'          // string terminator
  8.  
  9. char OChars[MAXCHARS],
  10. EChars[MAXCHARS],
  11. DChars[MAXCHARS] = "Soon!";    // Global Original, Encrypted, Decrypted character strings
  12.  
  13. //----------------------------- C++ Functions ----------------------------------------------------------
  14.  
  15. void get_char(char& a_character)
  16. {
  17.     cin >> a_character;
  18.     while (((a_character < '0') | (a_character > 'z')) && (a_character != dollarchar))
  19.     {
  20.         cout << "Alphanumeric characters only, please try again > ";
  21.         cin >> a_character;
  22.     }
  23. }
  24. //-------------------------------------------------------------------------------------------------------------
  25.  
  26. void get_original_chars(int& length)
  27. {
  28.     char next_char;
  29.     length = 0;
  30.     get_char(next_char);
  31.  
  32.     while ((length < MAXCHARS) && (next_char != dollarchar))
  33.     {
  34.         OChars[length++] = next_char;
  35.         get_char(next_char);
  36.     }
  37. }
  38.  
  39. //---------------------------------------------------------------------------------------------------------------
  40. //----------------- ENCRYPTION ROUTINES -------------------------------------------------------------------------
  41.  
  42. void encrypt_chars(int length, char EKey)
  43. {
  44.     char temp_char;                         // char temporary store
  45.  
  46.     for (int i = 0; i < length; i++)        // encrypt characters one at a time
  47.     {
  48.         temp_char = OChars[i];              // temp_char now contains the address values of the individual character
  49.         __asm
  50.         {
  51.                 push    eax                 // Save values contained within register to stack
  52.                 push    ecx
  53.  
  54.                 movzx   ecx, temp_char
  55.                 push    ecx                 // Push argument #2
  56.                 lea     eax, EKey
  57.                 push    eax                 // Push argument #1
  58.                 call    encrypt4
  59.                 add     esp, 8              // Clean parameters of stack
  60.                 mov     temp_char, al       // Move the temp character into a register    
  61.  
  62.                 pop     ecx
  63.                 pop     eax
  64.         }
  65.         EChars[i] = temp_char;              // Store encrypted char in the encrypted chars array
  66.     }
  67.     return;
  68.  
  69.     // Inputs: register EAX = 32-bit address of Ekey,
  70.     // ECX = the character to be encrypted (in the low 8-bit field, CL).
  71.     // Output: register EAX = the encrypted value of the source character (in the low 8-bit field, AL).
  72.  
  73.     __asm
  74.     {
  75.     encrypt4:
  76.             push    ebp                 // Set stack
  77.             mov     ebp, esp            // Set up the base pointer
  78.  
  79.             mov     eax, [ebp + 8]      // Move value of parameter 1 into EAX
  80.             mov     ecx, [ebp + 12]     // Move value of parameter 2 into ECX
  81.             push    edi                 // Used for string and memory array copying
  82.             push    ecx                 // Loop counter for pushing character onto stack
  83.  
  84.             not     byte ptr[eax]       // Negation
  85.             add     byte ptr[eax], 0x04 // Adds hex 4 to EKey
  86.             movzx   edi, byte ptr[eax]  // Moves value of EKey into EDI using zeroes
  87.             pop     eax                 // Pop the character value from stack
  88.             xor     eax, edi            // XOR character to give encrypted value of source
  89.             pop     edi                 // Pop original address of EDI from the stack
  90.  
  91.             rol     al, 1               // Rotates the encrypted value of source by 1 bit (left)
  92.             rol     al, 1               // Rotates the encrypted value of source by 1 bit (left) again
  93.             add     al, 0x04            // Adds hex 4 to encrypted value of source
  94.  
  95.             mov     esp, ebp            // Deallocate values
  96.             pop     ebp                 // Restore the base pointer
  97.             ret
  98.     }
  99.  
  100.     //--- End of Assembly code
  101. }
  102. // end of encrypt_chars function
  103. //---------------------------------------------------------------------------------------------------------------
  104.  
  105.  
  106. //---------------------------------------------------------------------------------------------------------------
  107. //----------------- DECRYPTION ROUTINES -------------------------------------------------------------------------
  108. //
  109. void decrypt_chars(int length, char EKey)
  110. {
  111.     char temp_char;
  112.  
  113.     for (int i = 0; i < length; i++)        // decrypt characters one at a time
  114.     {
  115.         temp_char = EChars[i];              // temp_char now contains the address values of the individual character
  116.         __asm
  117.         {
  118.                     push    eax                 // Save values contained within register to stack
  119.                 push    ecx
  120.  
  121.                 movzx   ecx, temp_char
  122.                 push    ecx                 // Push argument #2
  123.                 lea     eax, EKey
  124.                 push    eax                 // Push argument #1
  125.                 call    decrypt
  126.                 add     esp, 8              // Clean parameters of stack
  127.                 mov     temp_char, al       // Move the temp character into a register    
  128.  
  129.                 pop     ecx
  130.                 pop     eax
  131.         }
  132.         DChars[i] = temp_char;              // Store decrypted char in the decrypted chars array  
  133.     }
  134.  
  135.     return;
  136.  
  137.     __asm
  138.     {
  139.     decrypt:
  140.             push    ebp         // Set stack
  141.             mov     ebp, esp            // Set up the base pointer
  142.  
  143.             mov     eax, [ebp + 8]      // Move value of parameter 1 into EAX
  144.             mov     ecx, [ebp + 12]     // Move value of parameter 2 into ECX
  145.             push    edi                 // Used for string and memory array copying
  146.             push    ecx                 // Loop counter for pushing character onto stack
  147.  
  148.             not     byte ptr[eax]       // Negation of actual EKey
  149.             add     byte ptr[eax], 0x04 // Adds hex 4 to actual EKey
  150.             movzx   edi, byte ptr[eax]  // Moves value of EKey into EDI using zeroes
  151.             pop     eax                 // Pop the character value from stack
  152.  
  153.             sub     al, 0x04            // Subtract hex 4 to encrypted value of source
  154.             ror     al, 1               // Rotates the encrypted value of source by 1 bit (right)
  155.             ror     al, 1               // Rotates the encrypted value of source by 1 bit (right) again
  156.             xor     eax, edi            // XOR character to give decrypted value of source
  157.             pop     edi                 // Pop original value of EDI from the stack        
  158.  
  159.             mov     esp, ebp            // Deallocate values
  160.             pop     ebp                 // Restore the base pointer
  161.             ret
  162.     }
  163. }
  164. // end of decrypt_chars function
  165. //---------------------------------------------------------------------------------------------------------------
  166.  
  167. int main(void)
  168. {
  169.     int  char_count;        // The number of actual characters entered (upto MAXCHARS limit).
  170.     char EKey;                      // Encryption key.
  171.  
  172.     cout << "\nPlease enter your Encryption Key (EKey) letter: "; get_char(EKey);
  173.  
  174.     cout << "\nNow enter upto " << MAXCHARS << " alphanumeric characters:\n";
  175.     get_original_chars(char_count);
  176.     cout << "\n\nOriginal source string = " << OChars << "\tHex = ";
  177.     for (int i = 0; i<char_count; i++) cout << hex << setw(2) << setfill('0') << ((int(OChars[i])) & 0xFF) << "  ";
  178.  
  179.     encrypt_chars(char_count, EKey);
  180.     cout << "\n\nEncrypted string = " << EChars << "\tHex = ";
  181.     for (int i = 0; i<char_count; i++) cout << ((int(EChars[i])) & 0xFF) << "  ";
  182.  
  183.     decrypt_chars(char_count, EKey);
  184.     cout << "\n\nDecrypted string = " << DChars << "\tHex = ";
  185.     for (int i = 0; i<char_count; i++) cout << ((int(DChars[i])) & 0xFF) << "  ";
  186.  
  187.     cout << "\n\nPress a key to end...";
  188.     while (!_kbhit());                                     //hold the screen until a key is pressed
  189.     return (0);
  190.  
  191.  
  192. } // end of whole encryption/decryption program --------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment