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

gusto

new project, to encrypt a word using polyalphabetic cipher, that is using a key word, may it be different each time the program is run (entered by user) to encrypt text.. so far i used an example program in the book that encrypt text but not polyalphbetically. I am also instructed that i have to use the XLAT instruction for my encryption, and im not sure what that instruction does.. so far i have this program, it encrypts but not the way it is supposed to because i dont know how to use the 'KEY' the user enters to do the actualy encryption... also where do i get the length of the 'KEY' the user enters? im getting 3778 or something from entereing 3 letters.... well here's the program, any input would be helpful.. thank you.


include \masm615\include\irvine16.inc

key = 239
bufmax = 35

.data

sprompt byte "Enter the plain text: ",0
sencrypt byte "CIPHERTEXT: ",0
skey byte "KEY: ",0
theend byte "END OF DATA",0
lengthbuf byte "LENGTH = ",0
buffer byte bufmax dup(0)
bufsize dword ?

.code

main proc
startup

start:
mov edx,offset skey
call writestring
call crlf

call inputthekey
mov edx,offset lengthbuf
call writestring
mov edx,bufsize
call writedec
call crlf

mov edx,offset sprompt
call writestring
call crlf
call  inputthestring

call translatebuffer
mov edx,offset sencrypt
call displaymessage

mov edx,offset theend
call writestring


exit
main endp

mytbl proc ; to be used with xlat later, not sure if correct

i=1
while  i  LE  26
byte  40h+i
i=i+1
endm
while  i  LE  26
byte  40h+i
i=i+1
endm
mytbl endp


inputthekey proc

pushad
mov ecx,bufmax
mov edx,offset buffer
call readstring
mov bufsize,eax
call crlf
popad
ret
inputthekey endp

inputthestring proc
pushad
mov ecx,bufmax
mov edx,offset buffer
call readstring
mov bufsize,eax
call crlf
popad
ret
inputthestring endp


displaymessage proc
pushad
call writestring
call crlf
mov edx,offset buffer
call writestring
call crlf
popad
ret
displaymessage endp


translatebuffer proc
pushad
mov ecx,bufsize
mov esi,0
L1:
xor buffer[esi],key
inc esi
loop l1

popad
ret
translatebuffer endp

quit: exit

end main


gusto

yes it seems it is polyalphabetic, guess he mistyped it on the project sheet  :clap:

MichaelW

From opcodes.hlp in the MASM32 package, for XLAT/XLATB:
Quote
Replaces the byte in AL with byte from a user table addressed by BX.  The original value of AL is the index into the translate table. The best way to describe this is MOV AL,[BX+AL]
MOV AL,[BX+AL] is not a valid instruction, but it does express what the XLAT instruction does. So for an example, in the code below, after the XLAT instruction AL would contain 'C'.

.data
table  db 'ABCD'
.code
mov  bx, offset table
mov  al, 2
xlat


A polyalphabetic cipher is not one that uses a keyword, but one that uses multiple substitution alphabets. Are you supposed to have the user enter multiple substitution alphabets, or are you supposed to somehow generate the alphabets from the entered keyword?

In the mytbl procedure the second while loop will not be entered because the value of i will be 27 after the first while loop. The first while loop will expand to the byte sequence 41h-5Ah, in the code segment. A macro that would place the table in the data segment would probably be a better choice, because you can access data in the data segment without using a segment override.

The irvine16 library includes a Str_length procedure that returns the length of a null-terminated string.

eschew obfuscation

gusto

im spose to use the KEYword to help cypher the text, and he asks for the length of the keyword, so i guess if the word is 3 digits long, then im spose to add 1,2,3 to watever letter the normal text is. ex: KEY: 'ABC', Length: '3', plain text:'AAA', CIPHERED:'BCD' .. something along those lines.. the 'mytbl' that i wrote was something he wrote in class and said 'this will help ur program write out the alphabet two times', yet he didnt explain how to even incorporporate it in anything, or even how to go about workin the program out, im working off a basicbasic example i found in the book..im so lost.. lol, so much for a happy thanksgiving  ::)

tenkey

Quote from: MichaelW on November 25, 2005, 03:01:21 AM
A polyalphabetic cipher is not one that uses a keyword, but one that uses multiple substitution alphabets. Are you supposed to have the user enter multiple substitution alphabets, or are you supposed to somehow generate the alphabets from the entered keyword?
Er, the wikipedia article you cited does not say there is no keyword. In fact, the examples such as Vigenere explicitly mention keywords.

Each letter in the keyword represents a different substitution alphabet. The simplest way is to use shifted alphabets (as in Vigenere) where, for example, the B alphabet table is the A alphabet table shifted one position. But in fact, each table can be an arbitrary one.

The keyword is generally used this way...
Plaintext:     R i g h t n o w
Keyword=CAT:   C A T C A T C A
Ciphered text: a b c d e f g h

where
a = R translated using table C
b = i translated using table A
c = g translated using table T
d = h translated using table C
and so on and so forth...

The exact ciphered text, which I've randomly written as abcdefgh, depends on the translation tables used.
A programming language is low level when its programs require attention to the irrelevant.
Alan Perlis, Epigram #8

MichaelW

Quote from: tenkey on November 25, 2005, 08:12:40 AM
Quote from: MichaelW on November 25, 2005, 03:01:21 AM
A polyalphabetic cipher is not one that uses a keyword, but one that uses multiple substitution alphabets. Are you supposed to have the user enter multiple substitution alphabets, or are you supposed to somehow generate the alphabets from the entered keyword?
Er, the wikipedia article you cited does not say there is no keyword. In fact, the examples such as Vigenere explicitly mention keywords.
I did not mean to imply that there is no keyword, just that the presence of a keyword does not make a cipher polyalphabetic. But I see now that I misinterpreted "new project, to encrypt a word using polyalphabetic cipher, that is using a key word," as "new project, to encrypt a word using polyalphabetic cipher, IOW, using a key word,"

eschew obfuscation

gusto

thanks guys, i seem to understand what polyalpha. substitution is... now my problem lies in how to actually apply it.. had anyone done examples like this, or have something i can work off of? thanks

gusto

well seems like nobody does... well can anybody tell me what the 'mytbl' procedure does? and how i can use it to encrypt my plain text? i know michaelw wrote wat call to use, but where would i insert it in my code? i keep getting the most random numbers, not the number of digits in my 'KEY'..thx

MichaelW

Try reading tenkey's post again, and then this page:

http://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher

If you are going to use a Vigenère cipher, and you are not going to limit the key to a restricted range of characters, then you will need 26 alphabets. As I stated previously, it would be better to place the table in the data segment so you can access it without a segment override. You could code a macro to create the table, but it would probably be easier to create it manually.

As I stated previously, the irvine16 library includes a Str_length procedure that returns the length of a null-terminated string. The ReadString procedure returns a null-terminated string, so you can just pass the key to the Str_length procedure to get the length. I don't know what sort of documentation you may have for the irvine16 library, but if all else fails you can just use the source file for documentation. You should have a copy of the source file, or if not you can get a copy here:

http://www.cs.fit.edu/~mmahoney/cse3101/examples/Lib16/


eschew obfuscation

gusto

my KEY has no limit of characters, but my PLAINTEXT has a limit of 35, also when '!' is enterd as a KEY my program should exit, i tried using the -test- and -cmp- calls to try to exit my program when '!' is entered but i've had no luck, i tried using its hexadecimal code and using '!' as a char

MichaelW

I don't know what your code currently looks like, but if you are using ReadChar to get the input, then you should be able to just compare al to '!'. If you are using ReadString, then it will be more difficult. For this and other purposes it would be convenient to have a procedure that could return a single character from a string, something like the BASIC MID$ function, but with the length fixed at 1. You should probably assume that the character indexes start at zero, so, for example, to get the last character of a string you would pass the index (stringLength – 1). As a starting point, take a look at the Str___ procedures in irvine16.asm. And keep in mind that for indirect memory operands with 16-bit registers you are limited to using a base (BP or BX) or index (DI or SI) register, or one of each.

eschew obfuscation

gusto


uppercase proc
i=0
repeat 97
byte i
i=i+1
endm

repeat 26
byte i-32
i=i+1
endm

repeat 133
byte i
i=i+1
endm

uppercase endp

------------------------------------------------

mov ebx,offset uppercase
mov al,[ebx]
xlat
mov [ebx],al



the proffesor gave me that code to try to use to translate whatever plaint text entered to be converted to uppercase... but it doesnt seem to be doing its job... he's lost in how to use assembler himself, and couldnt answer me... do you guys know if its right or close at all?

gusto



include \masm615\include\irvine16.inc

key = 239
bufmax = 35

.data

sprompt byte "Enter the plain text: ",0
sencrypt byte "CIPHERTEXT: ",0
skey byte "KEY: ",0
sdecrypt byte "Decrypted: ",0
theend byte "END OF DATA",0
lengthbuf byte "LENGTH = ",0
buffer byte bufmax dup(0)
bufsize dword ?

.code

main proc
startup

start:
mov edx,offset skey
call writestring
call crlf

call inputthekey
mov edx,offset lengthbuf
call writestring
mov edx,bufsize
call writedec
call crlf

mov edx,offset sprompt
call writestring
call crlf
call inputthestring

call translatebuffer
mov edx,offset sencrypt
call displaymessage

mov edx,offset theend
call writestring


exit
main endp

mytbl proc ; to be used with xlat later, not sure if correct

i=1
while i LE 26
byte 40h+i
i=i+1
endm
while i LE 26
byte 40h+i
i=i+1
endm
mytbl endp

uppercase proc
i=0
repeat 97
byte i
i=i+1
endm

repeat 26
byte i-32
i=i+1
endm

repeat 133
byte i
i=i+1
endm

uppercase endp

inputthekey proc

call readchar
cmp al,'!'
je quit

pushad
mov ecx,bufmax
mov edx,offset buffer
call readstring
mov bufsize,eax
call crlf
popad
ret
inputthekey endp

inputthestring proc
pushad
mov ecx,bufmax
mov edx,offset buffer

mov ebx,offset uppercase
mov al,[ebx]
xlat
mov [ebx],al

call readstring



mov bufsize,eax
call crlf
popad
ret
inputthestring endp


displaymessage proc
pushad
call writestring
call crlf
mov edx,offset buffer
call writestring
call crlf
popad
ret
displaymessage endp


translatebuffer proc
pushad
mov ecx,bufsize
mov esi,0
L1:
xor buffer[esi],key
inc esi
loop l1

popad
ret
translatebuffer endp

quit: exit

end main




and thats currently my code... it encrypts but not polualphabetically using the key... and the LENGTH of the string isnt coming out correct  :dazzled:.......... thanks for any help!

AeroASM

I assume that you are doing the Vigenere cipher - it is the simplest keyword-based polyalphabetic substitution cipher.

Use this text encoding: a=0, b=1 ... z=25.
Put both your plaintext and your repeated keyword into this encoding.
Now, for each letter of the plaintext, add its value to to corresponding key value and convert it to the range 0 to 25 modulo 26.
Then translate this back into ASCII.