# -*- coding: utf-8 -*-
plain=b'UUyFTj8PCzF6geFn6xgBOYSvVTrbpNU4OF9db9wMcPD1yDbaJw=='
key=b'welcometoicqedu'
def rc4(plain,key):
x=0
l=[]
# init box
Sbox=[s for s in range(256)]
Tbox=[key[i%len(key)] for i in range(256)]
# swap box
for i in range(256):
x=(x+Sbox[i]+Tbox[i])%256
Sbox[i],Sbox[x]=Sbox[x],Sbox[i]
x=y=0
out=[]
# Pseudo-Random Generation Algorithm,PRGA
for char in plain:
x=(x+1)%256 #linear
y=(y+Sbox[x])%256 #non-linear
Sbox[y],Sbox[x]=Sbox[x],Sbox[y]
cipher_byte=Sbox[(Sbox[x]+Sbox[y])%256]
# xor
out.append(chr(char^cipher_byte))
cipher_stream=''.join(out)
print(cipher_stream.encode())
return cipher_stream
import base64
import hashlib
# encode=rc4(base64.b64decode(plain).decode()[len(key):],key)
salt,crypt=base64.b64decode(plain)[:16],base64.b64decode(plain)[16:]
print(salt)
realkey=hashlib.sha1(key+salt).digest()
rc4(crypt,realkey)
由于密文前面有salt,需要先提取salt然后计算realkey,然后解密。由于rc4的本质采用异或伪随机数流,而其由密钥生成,所以加解密采用一套算法即可。