import random, base64 from hashlib import sha1 defcrypt(data, key): """RC4 algorithm""" x = 0 box = range(256) for i in range(256): x = (x + box[i] + ord(key[i % len(key)])) % 256 box[i], box[x] = box[x], box[i] x = y = 0 out = [] for char in data: x = (x + 1) % 256 y = (y + box[x]) % 256 box[x], box[y] = box[y], box[x] out.append(chr(ord(char) ^ box[(box[x] + box[y]) % 256])) return''.join(out) deftencode(data, key, encode=base64.b64encode, salt_length=16): """RC4 encryption with random salt and final encoding""" salt = '' for n in range(salt_length): salt += chr(random.randrange(256)) data = salt + crypt(data, sha1(key + salt).digest()) if encode: data = encode(data) return data deftdecode(data, key, decode=base64.b64decode, salt_length=16): """RC4 decryption of encoded data""" if decode: data = decode(data) print data salt = data[:salt_length] print len(salt) return crypt(data[salt_length:], sha1(key + salt).digest()) if __name__ == '__main__': # 需要解密的数据 data = 'UUyFTj8PCzF6geFn6xgBOYSvVTrbpNU4OF9db9wMcPD1yDbaJw==' # 密钥 key = 'welcometoicqedu' # 解码 decoded_data = tdecode(data=data, key=key) #decoded_data = crypt(data,m) print("明文是:") print decoded_data #bdctf{YOU_CRAKE_THE_RC4}
a = [] for i in range(256): a.append(i) v11 = 0 ch = "Th1sIsTheK3y" for j in range(256): v11 = (v11 + a[j] + ord(ch[j%12]))%256 v3 = a[j] a[j]=a[v11] a[v11] = v3 v8 = 0 v9 = 0 b = [] for k in range(24): v8 = (v8 +1)%256 v9 = (v9+a[v8])%256 b.append(a[(a[v9]+a[v8])%256])
#print(a) #print(b) key = [0x1c,0x61,0x97,0x34,0x28,0x69,0xfa,0x54,0xda,0x3a,0x2b,0xbb,0x05,0x09,0x16,0x38,0xf3,0xcf,0xd8,0xa5,0x12,0x7e,0x67,0x44] print(key) flag="" for i in range(24): flag+=chr(key[i]^b[i])
#coding="utf-8" from Crypto.Cipher import * data = "\x1c\x61\x97\x34\x28\x69\xfa\x54\xda\x3a\x2b\xbb\x05\x09\x16\x38\xf3\xcf\xd8\xa5\x12\x7e\x67\x44" key = "Th1sIsTheK3y" cipher = ARC4.new(key) m = cipher.decrypt(key) key_1 = ARC4.new(m) m = key_1.decrypt(data) print m