Advertisement
Guest User

Untitled

a guest
Apr 18th, 2015
200
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.24 KB | None | 0 0
  1. // The encryption program in C++ and ASM with a very simple encryption method - it simply adds 1 to the character.
  2. // The encryption method is written in ASM. You will replace this with your allocated version for the assignment.
  3. // In this version parameters are passed via registers (see 'encrypt' for details).
  4. // Filename: "4473 FoCA 2015 Encryption Original with ASM.cpp"
  5. //
  6. // Kieran Willis, last update: Mar 2015
  7. // *** PLEASE CHANGE THE LINE ABOVE TO YOUR NAME & DATE ***
  8.  
  9. #include <conio.h> // for kbhit
  10. #include <iostream> // for cin >> and cout <<
  11. #include <iomanip> // for fancy output
  12. using namespace std;
  13.  
  14. #define MAXCHARS 6 // feel free to alter this, but 6 is the minimum
  15. #define dollarchar '$' // string terminator
  16.  
  17. char OChars[MAXCHARS],
  18. EChars[MAXCHARS],
  19. DChars[MAXCHARS]; // Global Original, Encrypted, Decrypted character strings
  20.  
  21. //----------------------------- C++ Functions ----------------------------------------------------------
  22.  
  23. void get_char (char& a_character)
  24. {
  25. cin >> a_character;
  26. while (((a_character < '0') | (a_character > 'z')) && (a_character != dollarchar))
  27. { cout << "Alphanumeric characters only, please try again > ";
  28. cin >> a_character;
  29. }
  30. }
  31. //-------------------------------------------------------------------------------------------------------------
  32.  
  33. void get_original_chars (int& length)
  34. { char next_char;
  35. length = 0;
  36. get_char (next_char);
  37.  
  38. while ((length < MAXCHARS) && (next_char != dollarchar))
  39. {
  40. OChars [length++] = next_char;
  41. get_char (next_char);
  42. }
  43. }
  44.  
  45. //---------------------------------------------------------------------------------------------------------------
  46. //----------------- ENCRYPTION ROUTINES -------------------------------------------------------------------------
  47.  
  48. void encrypt_chars (int length, char EKey)
  49. { char temp_char; // char temporary store
  50.  
  51. for (int i = 0; i < length; i++) // encrypt characters one at a time
  52. {
  53. temp_char = OChars [i]; //
  54. __asm { //
  55. push eax // save register values on stack to be safe
  56. push ecx //
  57. //
  58. movzx ecx,temp_char // set up registers (Nb this isn't StdCall or Cdecl)
  59. lea eax,EKey //
  60.  
  61.  
  62. // caller - pushing the values of parameters (before calling main encryption)
  63. push ecx //copies first letter of string in to ESP
  64. push eax
  65.  
  66. call encrypt2 // encrypt the character
  67.  
  68. add esp, 8
  69.  
  70. mov temp_char,al
  71.  
  72. //
  73. pop ecx // restore original register values from stack
  74. pop eax //
  75. }
  76. EChars [i] = temp_char; // Store encrypted char in the encrypted chars array
  77. }
  78. return;
  79.  
  80.  
  81. // Encrypt subroutine. You should paste in the encryption routine you've been allocated from Bb and
  82. // overwrite this initial, simple, version. Ensure you change the ‘call’ above to use the
  83. // correct 'encryptnn' label where nn is your encryption routine number.
  84. // Inputs: register EAX = 32-bit address of Ekey,
  85. // ECX = the character to be encrypted (in the low 8-bit field, CL).
  86. // Output: register EAX = the encrypted value of the source character (in the low 8-bit field, AL).
  87. __asm {
  88.  
  89. encrypt2:
  90.  
  91. //registers destroyed at end = ecx, edx
  92.  
  93.  
  94.  
  95. push ebp // preserve ebp on the stack
  96. mov ebp, esp // set ebp's new fixed value from esp
  97.  
  98. push ecx // preserve ecx on the stack
  99.  
  100. mov ecx, [ebp + 12] // move address of stack in to ecx
  101. mov eax, [ebp + 8]
  102.  
  103.  
  104. xor edx,edx // set edx to 0 - fast and efficient
  105. mov dl,byte ptr[eax] // store byte pointed at by eax in to dl dl contains ekay
  106. and dl,0xF3 // perform logical and with immediate hex value F3
  107. ror dl,1 // rotates dl right once
  108. ror dl,1 // rotates dl right once
  109. ror dl,1 // rotates dl right once - alters ekey
  110. ror dl,1 // rotates dl right once
  111. inc dl // adds 1 to the value of dl
  112. pop ecx // takes top element in the stack and places it in to ecx register (was never modified, no change)
  113. add ecx,edx // add the contents of edx to ecx
  114. 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
  115. ror cl,1 // rotates cl right once
  116. mov eax,ecx // takes value in ecx register and places the value in eax register
  117.  
  118.  
  119. mov esp, ebp
  120. pop ebp //place original value of ebp back in to ebp from the stack
  121.  
  122. ret // return from subroutine (16 due to 4 parameters)
  123.  
  124. }
  125.  
  126. //--- End of Assembly code
  127. }
  128. // end of encrypt_chars function
  129. //---------------------------------------------------------------------------------------------------------------
  130.  
  131.  
  132.  
  133.  
  134. //---------------------------------------------------------------------------------------------------------------
  135. //----------------- DECRYPTION ROUTINES -------------------------------------------------------------------------
  136. //
  137. void decrypt_chars (int length, char EKey)
  138.  
  139.  
  140.  
  141.  
  142. { char temp_char; // char temporary store
  143.  
  144. for (int i = 0; i < length; i++) // encrypt characters one at a time
  145. {
  146. temp_char = EChars[i]; //
  147. __asm { //
  148. push eax // save register values on stack to be safe
  149. push ecx //
  150. //
  151. movzx ecx, temp_char // set up registers (Nb this isn't StdCall or Cdecl)
  152. lea eax, EKey //
  153.  
  154.  
  155. // caller - pushing the values of parameters (before calling main encryption)
  156. mov edx, [ebp + 8] //copies address value of eKey in to edx
  157. push eax //save value of eax on to the stack
  158. push ecx //save value of ecx on to the stack
  159. push edx //save value of edx on to the stack
  160.  
  161.  
  162. call decrypt2 // encrypt the character
  163.  
  164. //add esp, 8
  165.  
  166. mov temp_char, al //
  167. //
  168. pop ecx // restore original register values from stack
  169. pop eax //
  170. }
  171. DChars[i] = temp_char; // Store encrypted char in the encrypted chars array
  172. }
  173. return;
  174.  
  175.  
  176. // Encrypt subroutine. You should paste in the encryption routine you've been allocated from Bb and
  177. // overwrite this initial, simple, version. Ensure you change the ‘call’ above to use the
  178. // correct 'encryptnn' label where nn is your encryption routine number.
  179. // Inputs: register EAX = 32-bit address of Ekey,
  180. // ECX = the character to be encrypted (in the low 8-bit field, CL).
  181. // Output: register EAX = the encrypted value of the source character (in the low 8-bit field, AL).
  182. __asm {
  183.  
  184. decrypt2:
  185.  
  186. //registers destroyed at end = ecx, edx
  187.  
  188.  
  189.  
  190. push ebp
  191. mov ebp, esp
  192. mov edx, [ebp + 12]
  193.  
  194.  
  195.  
  196. push ecx
  197. xor edx, edx
  198. mov dl, byte ptr[eax]
  199. and dl, 0xF3
  200. rol cl, 1 //changed for decrypt
  201. ror dl, 1
  202. ror dl, 1
  203. ror dl, 1
  204. ror dl, 1
  205. inc dl
  206. pop ecx
  207. sub ecx, edx //changed for decrypt
  208. mov byte ptr[eax], dl
  209. mov eax, ecx
  210.  
  211. pop ebp
  212.  
  213. ret 12// causing break problem- prev ret 12 - current 8
  214.  
  215. }
  216.  
  217. }
  218. // end of decrypt_chars function
  219. //---------------------------------------------------------------------------------------------------------------
  220.  
  221.  
  222.  
  223.  
  224.  
  225. int main(void)
  226. {
  227. int char_count; // The number of actual characters entered (upto MAXCHARS limit).
  228. char EKey; // Encryption key.
  229.  
  230. cout << "\nPlease enter your Encryption Key (EKey) letter: "; get_char (EKey);
  231.  
  232. cout << "\nNow enter upto " << MAXCHARS << " alphanumeric characters:\n";
  233. get_original_chars (char_count);
  234. cout << "\n\nOriginal source string = " << OChars << "\tHex = ";
  235. for (int i=0; i<char_count; i++) cout<<hex<<setw(2)<<setfill('0')<< ((int (OChars[i]))&0xFF)<<" ";
  236.  
  237. encrypt_chars (char_count, EKey);
  238. cout << "\n\nEncrypted string = " << EChars << "\tHex = ";
  239. for (int i=0; i<char_count; i++) cout<< ((int (EChars[i]))&0xFF)<<" ";
  240.  
  241. decrypt_chars (char_count, EKey);
  242. cout << "\n\nDecrypted string = " << DChars << "\tHex = ";
  243. for (int i=0; i<char_count; i++) cout<< ((int (DChars[i]))&0xFF)<<" ";
  244.  
  245. cout << "\n\nPress a key to end...";
  246. while ( !_kbhit()); //hold the screen until a key is pressed
  247. return (0);
  248.  
  249.  
  250. } // end of whole encryption/decryption program --------------------------------------------------------------------
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement