Advertisement
JDpaste

Vernam cipher

Sep 29th, 2021 (edited)
1,062
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.12 KB | None | 0 0
  1. # Vernam cipher
  2. # an implementation devised by Gilbert Vernam in the early 1900s
  3.  
  4. #-- ------------
  5. def cipherVernam( key="", txt=""):
  6.     """ return a sequence of ASCII codepoints (tuple of ints)"""
  7.     #
  8.     lenTxt = len(txt)
  9.     if ( lenTxt == 0 or len(key) < lenTxt ) : return () # empty tuple
  10.     #    -----------    -----------------
  11.     # get a list of ints (ASCII codepoints) for pad and text
  12.     #
  13.     keyOrds = [ ord(c) for c in key ]  # assume key (pad value) is given as a String (for demo)
  14.     #
  15.     if ( isinstance( txt, str )) :
  16.         #
  17.         txtOrds = [ ord(c) for c in txt ]  # map String to list of integer ordinals (codepoints)
  18.     else:
  19.         txtOrds = txt                      # else .. already a list of (codepoint) integers
  20.     #------------------
  21.     print( key,':', txt )         # DEBUG .. REMOVE !
  22.     print( keyOrds,':', txtOrds ) # DEBUG .. REMOVE !
  23.     #---------
  24.     # map text using XOR
  25.     vOrds = []
  26.     for x in range( lenTxt ) :
  27.         #
  28.         vOrds.append( keyOrds[x] ^ txtOrds[x] ) # ^ is XOR
  29.     #
  30.     return tuple( vOrds )
  31. #
  32. #-- ----------------
  33. def showVernamRESULT( ordSequence ) :
  34.     print( 'OUTPUT follows ..')
  35.     print( 'DECIMAL ..')
  36.     print( ordSequence )
  37.    
  38.     print( 'BINARY ..')
  39.     resChars = [ bin(n) for n in ordSequence ]
  40.     print( ",".join(resChars))
  41.    
  42.     print( 'HEX ..')
  43.     resChars = [ hex(n) for n in ordSequence ]
  44.     print( ",".join(resChars))
  45.    
  46.     print( 'ASCII ..')
  47.     resChars = [ ascii(chr(n)) for n in ordSequence ]
  48.     print( ",".join(resChars))
  49.  
  50.     print( 'TEXT with non-printables excluded ..')
  51.     resChars = []
  52.     for thisOrd in ordSequence :
  53.         if ( thisOrd >= 32 and thisOrd < 127 ) : resChars.append( chr( thisOrd ))
  54.     #
  55.     print( "".join(resChars))  # make a String of it
  56. #-----------------
  57. # TEST calls ..
  58. print('-----------------------------------------------------------------')
  59. result = cipherVernam('GRWF', 'MEET' )
  60. showVernamRESULT( result )
  61. print('-----------------------------------------------------------------')
  62. result = cipherVernam('KTLZCPMYGNVED', [15, 1, 24, 25, 11, 22, 4, 11, 2, 12, 31, 23, 0])
  63. showVernamRESULT( result )
  64. print('-----------------------------------------------------------------')
  65. result = cipherVernam('LBMBTC', [31,10,12,12,0,2] )
  66. showVernamRESULT( result )
  67. print('-----------------------------------------------------------------')
  68. result = cipherVernam('0123456789', 'ROCKPYTHON' )
  69. showVernamRESULT( result )
  70. print('-----------------------------------------------------------------')
  71. result = cipherVernam('0123456789', (98, 126, 113, 120, 100, 108, 98, 127, 119, 119) )
  72. showVernamRESULT( result )
  73. print('-----------------------------------------------------------------')
  74. result = cipherVernam('CMGLNYHUJLYGNVEDLFJ', 'OKTLZCPMVLRZUZA' )
  75. showVernamRESULT( result )
  76. print('-----------------------------------------------------------------')
  77. #result = cipherVernam('GOOD', [71,82,87,70] )
  78. result = cipherVernam('GOOD', [ 0b1000111, 0b1010010, 0b1010111, 0b1000110 ] )
  79. showVernamRESULT( result )
  80. print('-----------------------------------------------------------------')
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement