#!/usr/bin/env python # # /* zx8 (ps) - Stream Cipher Algorithm/Source Code */ # /* # Copyright (c) 2012, Karl-Uwe Frank # All rights reserved. # # Redistribution and use in source and binary forms, with or without # modification, are permitted provided that the following conditions are # met: # # 1. Redistributions of source code must retain the above copyright # notice, this list of conditions and the following disclaimer. # # 2. Redistributions in binary form must reproduce the above copyright # notice, this list of conditions and the following disclaimer in the # documentation and/or other materials provided with the distribution. # # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS # IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A # PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT # HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED # TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR # PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING # NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS # SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # # #** zx8 (ps) algorithm developed by Karl-Uwe Frank # */ import sys, os #----------------------------------------- # Simulate missing ANSI-C "printf" # def printf(*args): sys.stdout.write(*args) sys.stdout.flush() #----------------------------------------- # # echo -en "Protected Privacy" | python ./zx8_ps__STDIN.py "SecretKeyWord" # # Keyword = SecretKeyWord # (hex) = 53:65:63:72:65:74:4B:65:79:57:6F:72:64 # # Plaintext = 50:72:6F:74:65:63:74:65:64:20:50:72:69:76:61:63:79 # Keystream = 62:F9:D4:31:2F:63:CC:08:E1:6F:22:C6:0F:68:EB:F2:1B # Ciphertext = 32:8B:BB:45:4A:00:B8:6D:85:4F:72:B4:66:1E:8A:91:62 # import zx8_ps_algorithm as zx8 # zx8_ps_algorithm.py #----------------------------------------- # def ShowUsage(ThisName): printf ("\nUsage: echo \"Plaintext\" \| %s keyword\n\n" % ThisName) # ----------------------------------------- # Main # def main(): # # Check if a Prarmeter is passed through PrgName = sys.argv[0] if (len(sys.argv) < 2): ShowUsage(PrgName) sys.exit() # ----------------------------------------- # Read the Encryption Keyword # if (len(sys.argv) == 2): for i in range(len(sys.argv[1])): zx8.KeyWord[i] = ord(sys.argv[1][i]) zx8.KeyLen = len(sys.argv[1]) if (zx8.KeyLen < 8): printf ("\nKeyword must be at least 8 characters long\n") sys.exit() # Show only if Keyword isn't too long if (zx8.KeyLen < 32): printf ("\nKeyword = %s" % zx8.KeyWord) printf ("\n (hex) = ") for i in range(zx8.KeyLen-1): printf ('%02X:' % zx8.KeyWord[i]) printf ('%02X\n\n' % zx8.KeyWord[i+1]) # ----------------------------------------- # Initialisation of zx8 (ps) # # perform the Key Schedule zx8.KSA() inB = outB = int(0) BLOCKSIZE = 1 # Byte Plaintext = Keystream = Ciphertext = '' # Encrypt from STDIN while (True): inB = sys.stdin.read(BLOCKSIZE) # read 1 Byte if (inB == ''): break # EOF outB = zx8.PRGA() Keystream += ('%02X:' % outB) Plaintext += ('%02X:' % ord(inB)) Ciphertext += ('%02X:' % (ord(inB) ^ outB)) printf ('Plaintext = %s\n' % Plaintext[0:-1]) printf ('Keystream = %s\n' % Keystream[0:-1]) printf ('Ciphertext = %s\n\n'% Ciphertext[0:-1]) if __name__ == "__main__": main() sys.exit()