python密码学 rc4
python实现rc4对汉字的加密:import numpy
def init(s,key,len):
i=0
j=0
t=[]
for i in range(256):
s.append(i)
t.append(key)
i=0
for i in range(255):
j=(j+s+t)%256
tmp=s
s=s
s=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)%256
tmp=s
s=s
s=tmp
t=(s+s)%256
if k%2==0:
plaintext^=m*pow(2,8)+s
else:
m=s
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))
for j in range(len(plaintext)):
p.append(ord(plaintext))
s1=init(S,k,len(key))
plaint=crypt(p,s1,len(p))
print('密文:')
for i in range(len(plaint)):
ciphertext.append(chr(plaint))
print(chr(plaint))
for j in range(len(ciphertext)):
c.append(ord(ciphertext))
s2=init(S1,k,len(key))
test=crypt(c,s2,len(c))
print('解密:')
for i in range(len(test)):
print(chr(test))
if __name__ == '__main__':
main()
该算法实现了对汉字的加密,而不是单纯的对二进制流的加密
感谢分享
在这顶一个 很棒 顶帖 {:1_921:}
页:
[1]