Einzelnen Beitrag anzeigen
Alt 09.07.11, 23:20   #53 (permalink)
CDW
Moderator
 
Benutzerbild von CDW
 
Registriert seit: 20.07.05
CDW Leistung: OpteronCDW Leistung: OpteronCDW Leistung: OpteronCDW Leistung: OpteronCDW Leistung: OpteronCDW Leistung: Opteron
Likes: 200
Standard

RC4 in 26 Python-Zeilen. 1:1 aus Wikipedia umgesetzt und kann Dateien ver/entschlüsseleln. Muss also nicht immer Caeser oder Vigenère sein

Py   

Code:
#! /usr/bin/env python
# -*- coding: utf-8 -*-
import sys

def init_sbox(key):
    s = range(256)
    j = 0
    for i in range(256):
        j = (j + s[i] + ord(key[i % len(key)])) % 256
        s[i], s[j] = s[j], s[i]
    return s

def crypt(text, key):
    buf = list(text)
    i = j = 0
    s = init_sbox(key)
    for n in xrange(len(text)):
        i = (i + 1) % 256
        j = (j + s[i]) % 256
        s[i], s[j] = s[j], s[i]
        rand = s[(s[i] + s[j]) % 256]
        buf[n] = chr(rand ^ ord(buf[n]))

    return "".join(buf)

try:
    with open(sys.argv[1], "r+b") as data:
        new_content = crypt(data.read(), sys.argv[2])
        data.seek(0)
        data.write(new_content)
except IndexError:
    print "usage: rc4.py file key"
__________________
Noch mal, für alle Pseudo-Geeks: 1+1=0. -> 10 wäre Überlauf!
Selig, wer nichts zu sagen hat und trotzdem schweigt.
CDW ist offline   Mit Zitat antworten
 

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61