#!/usr/bin/env python # # /* RC4 - Stream Cipher Algorithm/Source Code */ import sys, os, struct #----------------------------------------- # Simulate missing ANSI-C "printf" # def printf(*args): sys.stdout.write(*args) sys.stdout.flush() #----------------------------------------- # # echo "Protected Privacy" | python ./rc4__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:0A # Keystream = ED:FD:04:87:9D:65:25:3C:9F:45:51:7F:93:2C:77:A9:3C:26 # Ciphertext = BD:8F:6B:F3:F8:06:51:59:FB:65:01:0D:FA:5A:16:CA:45:2C # import rc4_algorithm as rc4 # rc4_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 # n = c = int(0) if (len(sys.argv) == 2): for i in range(len(sys.argv[1])): rc4.KeyWord[i] = ord(sys.argv[1][i]) rc4.KeyLen = len(sys.argv[1]) # ----------------------------------------- # Initialisation of rc4 # # perform the Key Schedule rc4.KSA() inB = outB = int(0) BLOCKSIZE = 1 # Byte Keystream = '' # Encrypt/Decrypt from STDIN while (True): inB = sys.stdin.read(BLOCKSIZE) # read 1 Byte if (inB == ''): break # EOF outB = rc4.PRGA() Keystream += ('%02X:' % outB) printf ('%s' % Keystream[0:-1]) if __name__ == "__main__": main() sys.exit()