Guest User

Untitled

a guest
Apr 28th, 2015
270
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.18 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;
  45.  
  46.     __asm
  47.     {
  48.         mov     DWORD PTR[ebp - 4], 0
  49.         jmp     L2
  50.  
  51.     L3:
  52.         mov     eax, DWORD PTR[ebp - 4]
  53.         movzx       eax, BYTE PTR EChars[eax]
  54.         mov     BYTE PTR[ebp - 5], al
  55.  
  56.         // OG code
  57.         push        eax                 // Save values contained within register to stack
  58.         push        ecx
  59.  
  60.         movzx   ecx, temp_char
  61.         push            ecx                 // Push argument #2
  62.         lea         eax, EKey
  63.         push        eax                 // Push argument #1
  64.         call        encrypt
  65.         add         esp, 8              // Clean parameters of stack
  66.         mov         temp_char, al       // Move the temp character into a register    
  67.  
  68.         pop          ecx
  69.         pop         eax
  70.  
  71.         // end of OG code
  72.  
  73.         mov     eax, DWORD PTR[ebp - 4]
  74.         movzx       edx, BYTE PTR[ebp - 5]
  75.         mov     BYTE PTR DChars[eax], dl
  76.  
  77.     L2:
  78.         mov     eax, DWORD PTR[ebp - 4]
  79.         cmp     eax, DWORD PTR[ebp - 20]
  80.         jl      L3
  81.  
  82.     }
  83.     return;
  84.  
  85.     __asm
  86.     {
  87.     encrypt:
  88.             push    ebp                 // Set stack
  89.             mov     ebp, esp            // Set up the base pointer
  90.  
  91.             mov     eax, [ebp + 8]      // Move value of parameter 1 into EAX
  92.             mov     ecx, [ebp + 12]     // Move value of parameter 2 into ECX
  93.             push    edi                 // Used for string and memory array copying
  94.             push    ecx                 // Loop counter for pushing character onto stack
  95.  
  96.             not     byte ptr[eax]       // Negation
  97.             add     byte ptr[eax], 0x04 // Adds hex 4 to EKey
  98.             movzx   edi, byte ptr[eax]  // Moves value of EKey into EDI using zeroes
  99.             pop     eax                 // Pop the character value from stack
  100.             xor     eax, edi            // XOR character to give encrypted value of source
  101.             pop     edi                 // Pop original address of EDI from the stack
  102.  
  103.             rol     al, 1               // Rotates the encrypted value of source by 1 bit (left)
  104.             rol     al, 1               // Rotates the encrypted value of source by 1 bit (left) again
  105.             add     al, 0x04            // Adds hex 4 to encrypted value of source
  106.  
  107.             mov     esp, ebp            // Deallocate values
  108.             pop     ebp                 // Restore the base pointer
  109.             ret
  110.     }
  111.  
  112.     //--- End of Assembly code
  113. }
  114. // end of encrypt_chars function
  115.  
  116. //---------------------------------------------------------------------------------------------------------------
  117.  
  118.  
  119. //---------------------------------------------------------------------------------------------------------------
  120. //----------------- DECRYPTION ROUTINES -------------------------------------------------------------------------
  121. //
  122. void decrypt_chars(int length, char EKey)
  123. {
  124.     char temp_char;
  125.  
  126.     for (int i = 0; i < length; i++)        // decrypt characters one at a time
  127.     {
  128.         temp_char = EChars[i];              // temp_char now contains the address values of the individual character
  129.         __asm
  130.         {
  131.                     push    eax                 // Save values contained within register to stack
  132.                 push    ecx
  133.  
  134.                 movzx   ecx, temp_char
  135.                 push    ecx                 // Push argument #2
  136.                 lea     eax, EKey
  137.                 push    eax                 // Push argument #1
  138.                 call    decrypt
  139.                 add     esp, 8              // Clean parameters of stack
  140.                 mov     temp_char, al       // Move the temp character into a register    
  141.  
  142.                 pop     ecx
  143.                 pop     eax
  144.         }
  145.         DChars[i] = temp_char;              // Store decrypted char in the decrypted chars array  
  146.     }
  147.  
  148.     return;
  149.  
  150.     __asm
  151.     {
  152.     decrypt:
  153.             push    ebp                 // Set stack
  154.             mov     ebp, esp            // Set up the base pointer
  155.  
  156.             mov     eax, [ebp + 8]      // Move value of parameter 1 into EAX
  157.             mov     ecx, [ebp + 12]     // Move value of parameter 2 into ECX
  158.             push    edi                 // Used for string and memory array copying
  159.             push    ecx                 // Loop counter for pushing character onto stack
  160.  
  161.             not     byte ptr[eax]       // Negation of actual EKey
  162.             add     byte ptr[eax], 0x04 // Adds hex 4 to actual EKey
  163.             movzx   edi, byte ptr[eax]  // Moves value of EKey into EDI using zeroes
  164.             pop     eax                 // Pop the character value from stack
  165.  
  166.             sub     al, 0x04            // Subtract hex 4 to encrypted value of source
  167.             ror     al, 1               // Rotates the encrypted value of source by 1 bit (right)
  168.             ror     al, 1               // Rotates the encrypted value of source by 1 bit (right) again
  169.             xor     eax, edi            // XOR character to give decrypted value of source
  170.             pop     edi                 // Pop original value of EDI from the stack        
  171.  
  172.             mov     esp, ebp            // Deallocate values
  173.             pop     ebp                 // Restore the base pointer
  174.             ret
  175.     }
  176. }
  177. // end of decrypt_chars function
  178. //---------------------------------------------------------------------------------------------------------------
  179.  
  180. int main(void)
  181. {
  182.     int  char_count;        // The number of actual characters entered (upto MAXCHARS limit).
  183.     char EKey;                      // Encryption key.
  184.  
  185.     cout << "\nPlease enter your Encryption Key (EKey) letter: "; get_char(EKey);
  186.  
  187.     cout << "\nNow enter upto " << MAXCHARS << " alphanumeric characters:\n";
  188.     get_original_chars(char_count);
  189.     cout << "\n\nOriginal source string = " << OChars << "\tHex = ";
  190.     for (int i = 0; i<char_count; i++) cout << hex << setw(2) << setfill('0') << ((int(OChars[i])) & 0xFF) << "  ";
  191.  
  192.     encrypt_chars(char_count, EKey);
  193.     cout << "\n\nEncrypted string = " << EChars << "\tHex = ";
  194.     for (int i = 0; i<char_count; i++) cout << ((int(EChars[i])) & 0xFF) << "  ";
  195.  
  196.     decrypt_chars(char_count, EKey);
  197.     cout << "\n\nDecrypted string = " << DChars << "\tHex = ";
  198.     for (int i = 0; i<char_count; i++) cout << ((int(DChars[i])) & 0xFF) << "  ";
  199.  
  200.     cout << "\n\nPress a key to end...";
  201.     while (!_kbhit());                                     //hold the screen until a key is pressed
  202.     return (0);
  203.  
  204.  
  205. } // end of whole encryption/decryption program --------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment