├── README.md └── rc4.py /README.md: -------------------------------------------------------------------------------- 1 | Copyright (C) 2012 Bo Zhu http://about.bozhu.me 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining a 4 | copy of this software and associated documentation files (the "Software"), 5 | to deal in the Software without restriction, including without limitation 6 | the rights to use, copy, modify, merge, publish, distribute, sublicense, 7 | and/or sell copies of the Software, and to permit persons to whom the 8 | Software is furnished to do so, subject to the following conditions: 9 | 10 | The above copyright notice and this permission notice shall be included in 11 | all copies or substantial portions of the Software. 12 | 13 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 16 | THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 18 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 19 | DEALINGS IN THE SOFTWARE. 20 | -------------------------------------------------------------------------------- /rc4.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python 2 | 3 | """ 4 | Copyright (C) 2012 Bo Zhu http://about.bozhu.me 5 | 6 | Permission is hereby granted, free of charge, to any person obtaining a 7 | copy of this software and associated documentation files (the "Software"), 8 | to deal in the Software without restriction, including without limitation 9 | the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 | and/or sell copies of the Software, and to permit persons to whom the 11 | Software is furnished to do so, subject to the following conditions: 12 | 13 | The above copyright notice and this permission notice shall be included in 14 | all copies or substantial portions of the Software. 15 | 16 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 | THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 20 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 21 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 22 | DEALINGS IN THE SOFTWARE. 23 | """ 24 | 25 | 26 | def KSA(key): 27 | keylength = len(key) 28 | 29 | S = range(256) 30 | 31 | j = 0 32 | for i in range(256): 33 | j = (j + S[i] + key[i % keylength]) % 256 34 | S[i], S[j] = S[j], S[i] # swap 35 | 36 | return S 37 | 38 | 39 | def PRGA(S): 40 | i = 0 41 | j = 0 42 | while True: 43 | i = (i + 1) % 256 44 | j = (j + S[i]) % 256 45 | S[i], S[j] = S[j], S[i] # swap 46 | 47 | K = S[(S[i] + S[j]) % 256] 48 | yield K 49 | 50 | 51 | def RC4(key): 52 | S = KSA(key) 53 | return PRGA(S) 54 | 55 | 56 | if __name__ == '__main__': 57 | # test vectors are from http://en.wikipedia.org/wiki/RC4 58 | 59 | # ciphertext should be BBF316E8D940AF0AD3 60 | key = 'Key' 61 | plaintext = 'Plaintext' 62 | 63 | # ciphertext should be 1021BF0420 64 | #key = 'Wiki' 65 | #plaintext = 'pedia' 66 | 67 | # ciphertext should be 45A01F645FC35B383552544B9BF5 68 | #key = 'Secret' 69 | #plaintext = 'Attack at dawn' 70 | 71 | def convert_key(s): 72 | return [ord(c) for c in s] 73 | key = convert_key(key) 74 | 75 | keystream = RC4(key) 76 | 77 | import sys 78 | for c in plaintext: 79 | sys.stdout.write("%02X" % (ord(c) ^ keystream.next())) 80 | print 81 | --------------------------------------------------------------------------------