Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # Vernam cipher
- # an implementation devised by Gilbert Vernam in the early 1900s
- #-- ------------
- def cipherVernam( key="", txt=""):
- """ return a sequence of ASCII codepoints (tuple of ints)"""
- #
- lenTxt = len(txt)
- if ( lenTxt == 0 or len(key) < lenTxt ) : return () # empty tuple
- # ----------- -----------------
- # get a list of ints (ASCII codepoints) for pad and text
- #
- keyOrds = [ ord(c) for c in key ] # assume key (pad value) is given as a String (for demo)
- #
- if ( isinstance( txt, str )) :
- #
- txtOrds = [ ord(c) for c in txt ] # map String to list of integer ordinals (codepoints)
- else:
- txtOrds = txt # else .. already a list of (codepoint) integers
- #------------------
- print( key,':', txt ) # DEBUG .. REMOVE !
- print( keyOrds,':', txtOrds ) # DEBUG .. REMOVE !
- #---------
- # map text using XOR
- vOrds = []
- for x in range( lenTxt ) :
- #
- vOrds.append( keyOrds[x] ^ txtOrds[x] ) # ^ is XOR
- #
- return tuple( vOrds )
- #
- #-- ----------------
- def showVernamRESULT( ordSequence ) :
- print( 'OUTPUT follows ..')
- print( 'DECIMAL ..')
- print( ordSequence )
- print( 'BINARY ..')
- resChars = [ bin(n) for n in ordSequence ]
- print( ",".join(resChars))
- print( 'HEX ..')
- resChars = [ hex(n) for n in ordSequence ]
- print( ",".join(resChars))
- print( 'ASCII ..')
- resChars = [ ascii(chr(n)) for n in ordSequence ]
- print( ",".join(resChars))
- print( 'TEXT with non-printables excluded ..')
- resChars = []
- for thisOrd in ordSequence :
- if ( thisOrd >= 32 and thisOrd < 127 ) : resChars.append( chr( thisOrd ))
- #
- print( "".join(resChars)) # make a String of it
- #-----------------
- # TEST calls ..
- print('-----------------------------------------------------------------')
- result = cipherVernam('GRWF', 'MEET' )
- showVernamRESULT( result )
- print('-----------------------------------------------------------------')
- result = cipherVernam('KTLZCPMYGNVED', [15, 1, 24, 25, 11, 22, 4, 11, 2, 12, 31, 23, 0])
- showVernamRESULT( result )
- print('-----------------------------------------------------------------')
- result = cipherVernam('LBMBTC', [31,10,12,12,0,2] )
- showVernamRESULT( result )
- print('-----------------------------------------------------------------')
- result = cipherVernam('0123456789', 'ROCKPYTHON' )
- showVernamRESULT( result )
- print('-----------------------------------------------------------------')
- result = cipherVernam('0123456789', (98, 126, 113, 120, 100, 108, 98, 127, 119, 119) )
- showVernamRESULT( result )
- print('-----------------------------------------------------------------')
- result = cipherVernam('CMGLNYHUJLYGNVEDLFJ', 'OKTLZCPMVLRZUZA' )
- showVernamRESULT( result )
- print('-----------------------------------------------------------------')
- #result = cipherVernam('GOOD', [71,82,87,70] )
- result = cipherVernam('GOOD', [ 0b1000111, 0b1010010, 0b1010111, 0b1000110 ] )
- showVernamRESULT( result )
- print('-----------------------------------------------------------------')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement