Description: An encryption algorithm transforms \"plain text\" into \"cipher tex
ID: 656106 • Letter: D
Question
Description:
An encryption algorithm transforms "plain text" into "cipher text." Just like a door needs a key to lock and unlock it, an encryption algorithm often requires a key to encrypt and decrypt a message. For example, suppose the plain text message is HELLO WORLD. An encryption algorithm consisting of nothing more than replacing each letter with the next letter in the alphabet would produce the cipher text IFMMP XPSME. The decryption algorithm simply replaces each letter with the one before it, and presto: the plain text HELLO WORLD is produced.
Procedure: Implement, in LC-3 assembly language, an encryption and a decryption programs that meet the following requirements:
The key is 6 and stored at x3100.
Your message is restricted to a maximum of 32 characters. They are stored at x3110 to x311F. The last ASCII code of the text string must be EOT (x04).
The encryption algorithm is as follows. Each ASCII code in the message will be transformed as follows:
The low order bit (LSB) of the code will be toggled. That is, if it is a 1, it will be replaced by 0 and vice verse.
The key will be added to the result of step 1 above.
The result should be stored at location x3120 to x312F.
For example, if the input (plain text) is A and the encryption key is 6, the program should take the ASCII value of A, 01000001, toggle bit [3:0], producing 01001110 and then add the encryption key, 6. The final output character would be 01000110, which is the ASCII value F.
Decryption algorithm is as follows. Each ASCII code in the message will be transformed as follows:
Subtract the encryption key from the ASCII code.
Toggle the low order bit of the result of step 1.
The result should be stored back to location x3110 to x311F.
Explanation / Answer
KRYPTO PROGRAM
.ORIG x4000
LD R1, IFILE
LD R2, IKRIP
LEA R0, MENSAGEM
PUTS
SE_1 GETC
OUT
NOT R3, R0
ADD R3, R3, #10
ADD R3, R3, #1
BRz FIM_SE_1
STR R0, R1, #0
VAL_1 LD R3, MASCARA_1
AND R4, R0, R3
LD R5, MEMORIA_1
STR R4, R5, #0
VAL_2 LD R3, MASCARA_11
AND R4, R0, R3
ADD R4, R4, R4
ADD R4, R4, R4
ADD R4, R4, R4
LD R5, MEMORIA_2
STR R4, R5, #0
VAL_100 LD R3, MASCARA_CEM
AND R4, R0, R3
BRz VAL_1000
ADD R4, R4, #-3
VAL_1000 LD R5, MEMORIA_3
STR R4, R5, #0
LD R3, MASCARA_MIL
AND R4, R0, R3
BRz VAL_10000
ADD R4, R4, #-6
VAL_10000 LD R5, MEMORIA_4
STR R4, R5, #0
LD R3, MASCARA_10MIL
AND R4, R0, R3
BRz SOMA
ADD R4, R4, #-12
SOMA LD R5, MEMORIA_5
STR R4, R5, #0
AND R5, R5, #0
LD R3, MEMORIA_1
LDR R4, R3, #0
ADD R5, R5, R4
LD R3, MEMORIA_2
LDR R4, R3, #0
ADD R5, R5, R4
LD R3, MEMORIA_3
LDR R4, R3, #0
ADD R5, R5, R4
LD R3, MEMORIA_4
LDR R4, R3, #0
ADD R5, R5, R4
LD R3, MEMORIA_5
LDR R4, R3, #0
ADD R5, R5, R4
STR R5, R2, #0
ADD R1, R1, #1
ADD R2, R2, #1
BR SE_1
FIM_SE_1
AND R0, R0, #0
ADD R0, R0, x04
STR R0, R1, #0
STR R0, R2, #0
AND R0, R0, #0
AND R1, R1, #0
AND R2, R2, #0
AND R3, R3, #0
AND R4, R4, #0
LD R2, IKRIP
LEA R0, MEN_CRIPTADA
PUTS
SE_2 LDR R4, R2, #0
ADD R2, R2, #1
NOT R0, R4
ADD R0, R0, #1
ADD R0, R0, X04
BRz FIM_SE_2
SENAO_2 AND R0, R0, #0
ADD R0, R0, R4
OUT
BR SE_2
FIM_SE_2 HALT
IFILE .FILL x5000
IKRIP .FILL x6000
MENSAGEM .STRINGZ "Insira uma mensagem. Quando acabar prima <Enter>: "
MEN_CRIPTADA .STRINGZ "Mensagem encriptada: "
EOF .FILL #04
CHEX .FILL x-30
MASCARA_1 .FILL xE0
MASCARA_2 .FILL x1F
MASCARA_11 .FILL x03
MASCARA_CEM .FILL x04
MASCARA_MIL .FILL x08
MASCARA_10MIL .FILL x10
MEMORIA_1 .FILL x5500
MEMORIA_2 .FILL x5501
MEMORIA_3 .FILL x5502
MEMORIA_4 .FILL x5503
MEMORIA_5 .FILL x5504
.END