python3 - RC4 算法
max.bai
2019.07
工作中遇到的rc4 算法,记录一下
RC4,加密和解密是一样的再加密一次就等于解密了
代码实现:
# coding: utf-8
# import base64
import binascii
def rc4_crypt(PlainBytes:bytes, KeyBytes:bytes) -> str:
'''[summary]
rc4 crypt
Arguments:
PlainBytes {[bytes]} -- [plain bytes]
KeyBytes {[bytes]} -- [key bytes]
Returns:
[string] -- [hex string]
'''
keystreamList = []
cipherList = []
keyLen = len(KeyBytes)
plainLen = len(PlainBytes)
S = list(range(256))
j = 0
for i in range(256):
j = (j + S[i] + KeyBytes[i % keyLen]) % 256
S[i], S[j] = S[j], S[i]
i = 0
j = 0
for m in range(plainLen):
i = (i + 1) % 256
j = (j + S[i]) % 256
S[i], S[j] = S[j], S[i]
k