assembly language using visual studio 2010
This program demonstrates simple Symmetric-key Encryption using the XOR instruction with a multi-byte key entered by the user. Use this key to encrypt and decrypt the plain text as shown below (try ch06_08.exe):
C:\zdsite\fullcoll\fc241\files>ch06_11
Enter the plain text: This is a string.
Enter the encryption key: 12345
Cipher text: eZZG§XA?U§BFA][V?
Decrypted: This is a string.
C:\zdsite\fullcoll\fc241\files>ch06_11
Enter the plain text: This is a string.
Enter the encryption key: 1
Cipher text: eYXB?XB?P?BECX_V?
Decrypted: This is a string.
You can use the sample Encrypt.asm as a start point. With a multi-byte key, you have to modify the procedure TranslateBuffer. Also, modify InputTheString without directly reference to the buffer variable, to make it reusable and you can call it twice to receive the plain text and the multi-byte key like this:
.data
prompt1 BYTE "Enter the plain text: ",0
prompt2 BYTE "Enter the encryption key: ",0
sEncrypt BYTE "Cipher text: ",0
sDecrypt BYTE "Decrypted: ",0
keyStr BYTE BUFMAX+1 DUP(0)
keySize DWORD ?
buffer BYTE BUFMAX+1 DUP(0)
bufSize DWORD ?
.code
main11 PROC
mov edx,OFFSET prompt1 ; display buffer prompt
mov ebx,OFFSET buffer ; point to the buffer
call InputTheString
mov bufSize,eax ; save the buffer length
mov edx,OFFSET prompt2 ; display key prompt
mov ebx,OFFSET keyStr ; point to the key
call InputTheString
mov keySize,eax ; save the key length
A better alternative to using keySize is to keep checking the zero terminator in TranslateBuffer, since ReadString automatically adds a terminator to the end of keyStr.
As for TranslateBuffer PROC, the new logic could be similar to this:
Initialize ESI and EDI to point the message and key buffer memory
Make a loop for each byte in the message
Translate a message byte by XOR with a key byte
Update ESI for next char in message
Check if all key bytes used up?
If yes, reset EDI to point the key start byte
If no, Update EDI for next byte in key
Loop continuing
Return
Advanced discussion: A possible issue is display of the Cipher text that might result in some control chars, like zero, ODh, OAh, etc. One choice is to show an encoded char's ASCII code if it below or equal to a space (20h):
Enter plain text: LvAKabc
Enter your key: AAAAA22222
Cipher text: [0D]7[00][0A][20]PQ
Decrypted text: LvAKabc
Press any key to continue...