News:

MASM32 SDK Description, downloads and other helpful links
MASM32.com New Forum Link
masmforum WebSite

polyalphabetic susbstitution cipher... help!

Started by gusto, November 24, 2005, 07:18:31 PM

Previous topic - Next topic

jorge

Hello. I am using readchar in a loop to read some input(I'm copying and pasting it). The problem is that when the "enter" key is pressed the cursor goes back to the begining of the line, not always a new line! This causes text to be overwritten. Therefore the encryption is not working. I am supposed to stop reading when the '#' character appears. Here's what I have so far. Thanks for any help!!

gettext proc
;Receives the text to be encrypted and puts it in "buffer"
pushad
mov   edx,offset splain
call   writestring
call   crlf
call   crlf
mov   ecx,bufmax
mov   edx,0
ls:
call   readchar
cmp   al,'#'
je   @F
mov   buffer[edx],al
inc   edx
xor   al,al
loop   ls
@@:
call   crlf
call   crlf
mov   bufsize,edx
inc   bufsize
call   crlf
popad
ret
gettext endp

MichaelW

gusto,

Your new alphabet generator macros have three problems:

The first byte at the abece label is a zero byte. To define a byte label without defining any data you could use:

abece label byte


The first data definition should have been byte 40h+i instead of 40h+1.

The second while loop will not run because you did not reinitialize i.

You could catch these errors if, while developing the code, you would have it display the combined alphabet each time it ran (the combined alphabet should be terminated with a null byte so you can use WriteString).

Also, it would be easier to code the macros (because you could avoid having to look up the character codes) and to understand what the macros are doing if instead of hex character codes you used the actual characters:

i='A'
while i LE 'Z'
  byte i
i=i+1
endm


You need only a few pieces of information to do the encryption and decryption. From this page:
http://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher
Quote
Vigenère can also be viewed algebraically. If the letters A–Z are taken to be the numbers 0–25, and addition is performed modulo 26, then Vigenère encryption can be written,

Ci=Pi+Ki (mod 26)

and decryption,

Pi=Ci-Ki (mod 26)

To encrypt, you must first create the repeated key, equal in length to the plaintext, as shown on the referenced page. The plaintext and the key must be in uppercase, and both the plaintext and the ciphertext should be terminated with a null byte (to make it easy to detect when you have reached the end of either).

Then for each character in the plaintext:

ciphertext = alphabet[(plaintext – 'A') + (repeatedkey – 'A')]

The look up in the combined alphabet effectively replaces the mod 26 operation. The result for (plaintext – 'A') + (repeatedkey – 'A') will be in the range 0-51.

To decrypt, for each character in the ciphertext:

plaintext = alphabet[((ciphertext – 'A') - (repeatedkey – 'A') + 26)]

The + 26 operation is necessary because the result for (ciphertext – 'A') - (repeatedkey – 'A') could be anywhere in the range –25 to +25, and the index values for the combined alphabet must be in the range 0-51.
eschew obfuscation

MichaelW

Jorge,

I think the problem is that the ReadChar procedure calls the Read Keyboard with Echo function, which upon reading a carriage return character (0Dh) sends the standard output device a carriage return but not a linefeed. If you do not wish to exit from the procedure when you detect a carriage return, you could try using the Read Keyboard Without Echo function (Inerrupt 21h, function 8) instead. You will probably need to provide code to echo the characters as they are typed (other than the carriage return character), and for this you could use the Display Character function (Interrupt 21h, function 2).

eschew obfuscation

pumasoul

Dude are you in my class, Prof. Burrs, CCNY? man i need help with that program also, i cant get project 5 please can you help me?