#coding:utf-8 import re defRC4_INIT(key): key=list(key) for i in range(len(key)): key[i]=ord(key[i]) k=[0for i in range(256)] s=[0for i in range(256)] j=0 length=len(key) for i in range(256): s[i]=i k[i]=key[i % length] for i in range(256): j=(j + s[i] + k[i])%256 s[i],s[j]=s[j],s[i] return s defRC4_DECRYPTE(Data,key): Data=list(Data) for i in range(len(Data)): Data[i]=ord(Data[i]) s=RC4_INIT(key) i=j=t=0 length=len(Data) for k in range(length): i = (i+1)%256 j=(j + s[i])%256 s[i],s[j]=s[j],s[i] t=(s[i]+s[j]+24)%256#不同于正常RC4 Data[k]=Data[k]^s[t] return Data defbase64_encode(s, dictionary): r = "" p = "" c = len(s) % 3
if (c > 0): for i in range(c, 3): p += '=' s += "\0"
for c in range(0, len(s), 3): n = (ord(s[c]) << 16) + (ord(s[c+1]) << 8) + (ord(s[c+2])) n = [(n >> 18) & 0x3F, (n >> 12) & 0x3F, (n >> 6) & 0x3F, n & 0x3F] r += dictionary[n[0]] + dictionary[n[1]] + dictionary[n[2]] + dictionary[n[3]] return r[0:len(r) - len(p)] + p
defbase64_decode(s, dictionary): base64inv = {} for i in range(len(dictionary)): base64inv[dictionary[i]] = i
if len(s) == 0: return"" p = ""if (s[-1] != "=") else"AA"if (len(s) > 1and s[-2] == "=") else"A" r = "" s = s[0:len(s) - len(p)] + p for c in range(0, len(s), 4): n = (base64inv[s[c]] << 18) + (base64inv[s[c+1]] << 12) + (base64inv[s[c+2]] << 6) + base64inv[s[c+3]] r += chr((n >> 16) & 255) + chr((n >> 8) & 255) + chr(n & 255) return r[0:len(r) - len(p)] deftest_base64(): import base64 import string import random dictionary = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" defrandom_string(length): return''.join(random.choice(string.ascii_letters) for m in range(length)) for i in range(100): s = random_string(i) encoded = base64_encode(s, dictionary) assert(encoded == base64.b64encode(s)) assert(s == base64_decode(encoded, dictionary))
if __name__ == "__main__": dictionary = 'Mq/J0tTI1RkSimKFwnczo2VXpPshL4_UgjH6DEG39yr+aOYWCfBeN5lb8v7QdxZuA'#向左平移后的码表 Data=base64_decode("B4QrGVzkpZVeHssap5HEgWfSQQ0zmMAA", dictionary) key='Please input the flag:\n' flag=RC4_DECRYPTE(Data,key) for i in flag: print(chr(i),end='') print()