#!/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, struct #----------------------------------------- # Simulate missing ANSI-C "printf" # def printf(*args): sys.stdout.write(*args) sys.stdout.flush() #----------------------------------------- # # # python ./zx8_ps.py -e Plainfile Cipherfile # # python ./zx8_ps.py -d Cipherfile Plainfile # import zx8_ps_algorithm as zx8 # zx8_ps_algorithm.py #----------------------------------------- # def ShowUsage(ThisName): printf ("\nUsage: %s -e InFile OutFile for encryption\n" % ThisName) printf ("Usage: %s -d InFile OutFile for decryption\n\n" % ThisName) # ----------------------------------------- # Invisible Keyboard Input # # http://www.python-forum.org/pythonforum/viewtopic.php?f=1&t=10998#p50502 # def getch(): if (os.name == 'posix'): import termios TERMIOS = termios fd = sys.stdin.fileno() old = termios.tcgetattr(fd) new = termios.tcgetattr(fd) new[3] = new[3] & ~TERMIOS.ICANON & ~TERMIOS.ECHO new[6][TERMIOS.VMIN] = 1 new[6][TERMIOS.VTIME] = 0 termios.tcsetattr(fd, TERMIOS.TCSANOW, new) ch = None try: ch = os.read(fd, 1) finally: termios.tcsetattr(fd, TERMIOS.TCSAFLUSH, old) return ch else: import msvcrt return msvcrt.getch() # ----------------------------------------- # Main # def main(): # # Check if a Parameter are passed through PrgName = InFile = OutFile = Direction = str('') PrgName = sys.argv[0] if (len(sys.argv) < 4): ShowUsage(PrgName) sys.exit() else: KeyComp = bytearray(256) Direction = sys.argv[1] if ( (Direction != "-e") and (Direction != "-d") ): ShowUsage(PrgName) sys.exit() # Set InFile and OutFile Name InFile = sys.argv[2] OutFile = sys.argv[3] # ----------------------------------------- # Read the Encryption Keyword # n = c = int(0) if (len(sys.argv) == 5): # If passed as Parameter for i in range(len(sys.argv[4])): zx8.KeyWord[i] = ord(sys.argv[4][i]) zx8.KeyLen = len(sys.argv[4]) else: printf("Enter Keyword: ") n = c = 0 while(c!='\n'):c=getch();zx8.KeyWord[n]=ord(c);n+=1 zx8.KeyWord[n-1]='\0' if (Direction == "-e"): printf("\nEnter Keyword again: ") n = c = 0 while(c!='\n'):c=getch();KeyComp[n]=ord(c);n+=1 KeyComp[n-1]='\0' if ( zx8.KeyWord != KeyComp): print("\nEntered Keywords did not match\n") sys.exit() zx8.KeyLen = n-1 if (zx8.KeyLen < 8): print("\nKeyword must be at least 8 unsigned characters long\n") sys.exit() printf("\nKeyword is: %s\n" % zx8.KeyWord) # ----------------------------------------- # Initialisation of zx8 (ps) # # perform the Key Schedule zx8.KSA() FileLen = inB = outB = i = int(0) BLOCKSIZE = 1 # Byte if (Direction == "-e"): printf("\n===================================================\n") printf("Encrypting: %s\ninto file : %s\n" % (InFile, OutFile)) printf("===================================================\n") else: printf("\n===================================================\n") printf("Decrypting: %s\ninto file : %s\n" % (InFile, OutFile)) printf("===================================================\n") try: fIn = open(InFile, "rb") except IOError: print("error opening file: %s\n" % InFile) sys.exit() fIn.seek(0,2) # EOF FileLen = fIn.tell() fIn.seek(0,0) # rewind try: fOut = open(OutFile, "wb"); except IOError: print("error creating file: %s\n" % OutFile) sys.exit() print("%10u byte file size\n" % FileLen) # Encrypt/Decrypt Binary File (unbuffered Read/Write) while (True): inB = fIn.read(BLOCKSIZE) if (inB == ''): break # EOF outB = struct.pack('B', ord(inB) ^ zx8.PRGA()) fOut.write(outB) fIn.close() fOut.close() if __name__ == "__main__": main() sys.exit()