The problem is at decrypt
No. Even the encryption is wrong! As Jester noted, you're working with ASCII codes where you should be working with the offsets [0,25] of the letters in the alphabet.
This is the original Vigenère encoding, subtracting/adding 97 to convert from/to lowercase letters:
CIPHER[i] = ((INPUT[i]-97 + KEY[i]-97) % 26) + 97
INPUT : "qwerty"
KEY : "asdasd"
CIPHER : "qohrlb"
The decrypt algorithm should be
RESULT[i] = (INPUT[i] - KEY[i]) % 26
How can decryption even be using the INPUT? That's what decryption is trying to find out!
Some errors in your current code include:
- You have the result of the subtraction in
DL
and you want to divide that by 26, but you divide CX
by 26 instead
- You use the word-sized division, but forget to zero
DX
beforehand
Next use of the byte-sized division corrects these few errors:
sub dl, cl
mov ax, dx ; Store in AX the result of subtracting
mov bl, 26
div bl ; To obtain the reminder from % 26
add ah, 3Dh ; add 3Dh to AH (remainder of DIV) to get the ascii
mov si, offset cipherText
add si, di
add [si], ah
Why do you think that adding 3Dh (61 in decimal) produces ASCII ?
Your current encryption does this:
113 119 101 114 116 121 INPUT[] : "qwerty"
97 115 100 97 115 100 KEY[] : "asdasd"
--- --- --- --- --- ---
210 234 201 211 231 221 INPUT[]+KEY[]
2 0 19 3 23 13 (INPUT[]+KEY[]) % 26
cipher 63 61 80 64 84 74 (INPUT[]+KEY[]) % 26 + 3Dh
? = P @ T J CIPHER[] : "catdxn" ???
I don't see where you get your CIPHER from. Could it be that you interpret the numbers 63, 61, and 64 as hexadecimals? Those do represent "c", "a", and "d".
And for the question about subtraction itself. It all depends on how you look at the encoded number. If DL
contains the bitpattern 11101110b (EEh) and you look at it the unsigned way, then it represents 238, but if you look at it the signed way, then it represents -18.
Just treat it as a signed number and add the required 26. You'll end up with a number in range [0,25].
sub dl, cl ; DH = 0
jns IsPositive
add dl, 26
IsPositive:
mov ax, dx ; Store in AX the result of subtracting