I'm working on a program to encrypt / decrypt data using the Vigenère Cipher. All is well except the decrypt part.
The logic behind the encryption and to compute the key is:
INPUT : "qwerty"
Key : "asd" = The computed key is "asdasd"
CIPHER: "catdxn"
For encrypting I use the following algorithm:
RESULT[i] = (INPUT[i]+key[i]) % 26
RESULT[i] += 3dH ; to transform to asci number
CIPHER[i] += RESULT[i] The problem is at decrypt:
The decrypt algorithm should be
RESULT[i] = (INPUT[i] - KEY[i]) % 26
IN CASE INPUT[i] - KEY[i] = NEGATIVE NUMBER = add 26 so the formula changes to
RESULT[i] = (INPUT[i] - KEY[i] + 26 ) % 26
RESULT[i] += 3dH
CIPHER[i] += RESULT[i] ; Get the result String
The expected result should be "qwerty" but I'm getting "usgtrm".
So following the algorithm described above I have the following code:
;inputKey = KEY,
;inputWORD = Input,
;cipherText = Result
;inputWORDLENGTH = length of input = CX
XOR DI,DI
vigDECLOOP:
cmp di,cx
JNB DONELOOPDEC
PUSH CX
mov si, offset inputWORD
ADD SI,DI
XOR DX,DX
MOV DL, DS:[SI] ; DL = INPUT[I]
xor SI,SI
MOV SI, OFFSET inputKEY
ADD SI, DI
XOR CX,CX
MOV CL, DS:[SI] ; CL = KEY[I]
SUB DL, CL
; ========
; Here in case is negative number I somehow need to add 26 to DL (result of DIV )
; IT 2: 61- 73 = EE in DL and should be -18, I know is something about the signed bit
; but I dont know what to read and where about this.
; ========
mov ax, cx ; Store in AX the result of subtracting
mov bx, 26 ; add in bx 26
div bx ; To obtain the reminder from % 26
; ========
; ========
add dl, 3Dh ; add 3dH to DL(reminder of DIV) to get the asci
xor si,si
mov si, offset cipherText
add si,di
xor dh,dh
add dl, DS:[si]
mov DS:[si],dl
INC DI
POP CX
jmp vigDECLOOP
DONELOOPDEC:
RET
The problem is at subtracting numbers and I'm totally noob on this subject. Let's say for the word "catdxn". 63, 61, 74, 64, 78, 6E AND THE KEY 'asdasd' 61, 73, 64, 61, 73, 64 At the 2'nd Iteration we have 61 - 73 ( SUB DL, CL ) = The result is FFFF FFFF FFFF FFEE or - 18. When this happens I need to add 26 to the result but I cant put my head to understand: The result of SUB DL, CL STORES IN DL = EE And this is 238 in decimal, how can I add 26 decimal or (1A Hex) to a number that is positive... it should be '61h - 73h = -18d + 26d or 1Ah = 8'. Could be really wrong about it.