python实现rc4对汉字的加密:
[Python] 纯文本查看 复制代码 import numpy
def init(s,key,len):
i=0
j=0
t=[]
for i in range(256):
s.append(i)
t.append(key[i%len])
i=0
for i in range(255):
j=(j+s[i]+t[i])%256
tmp=s[i]
s[i]=s[j]
s[j]=tmp
return s
def crypt(plaintext,s,len):
i=0
j=0
s1=[]
s2=[]
k=1
for i in range(len*2):
n=i
i=(i+1)%256
j=(j+s[i])%256
tmp=s[i]
s[i]=s[j]
s[j]=tmp
t=(s[i]+s[j])%256
if k%2==0:
plaintext[int((n-1)/2)]^=m*pow(2,8)+s[t]
else:
m=s[t]
k=k+1
return plaintext
def main():
key=input('key:')
plaintext=input('plaintext:')
ciphertext=[]
p=[]
i=j=0
k=[]
S=[]
S1=[]
s=[]
c=[]
for i in range(len(key)):
k.append(ord(key[i]))
for j in range(len(plaintext)):
p.append(ord(plaintext[j]))
s1=init(S,k,len(key))
plaint=crypt(p,s1,len(p))
print('密文:')
for i in range(len(plaint)):
ciphertext.append(chr(plaint[i]))
print(chr(plaint[i]))
for j in range(len(ciphertext)):
c.append(ord(ciphertext[j]))
s2=init(S1,k,len(key))
test=crypt(c,s2,len(c))
print('解密:')
for i in range(len(test)):
print(chr(test[i]))
if __name__ == '__main__':
main()
该算法实现了对汉字的加密,而不是单纯的对二进制流的加密
|