NEKO

Python实现RC4及A5

2017/12/15

RC4:(python3)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import time
s=[]
k=[]
def init(): #初始化密钥流
key = input('请输入密钥:\n')
j=0
for i in range(256):
s.append(i)
k.append(ord(key[i%len(key)]))
for i in range(256):
j = (j + s[i] + k[i]) % 256
tmp = s[i]
s[i] = s[j]
s[j] = tmp

def Encode():
filename=input('请输入要加密的文件的名称:\n')
init()
stime = time.time()
plain=open(filename,'rb').read().decode()
cipher=''
for i in range(len(plain)):
cipher+=chr(s[i%256]^ord(plain[i])) #异或
f=open('RC4_cipher.txt','wb')
f.write(cipher.encode('utf-8'))
f.close()
etime=time.time()
print('密文存储在RC4_cipher.txt中\n')
print('总耗时%s秒\n'%(etime-stime))
def Decode():
filename = input('请输入要解密的文件的名称:\n')
init()
stime = time.time()
cipher = open(filename, 'rb').read().decode()
plain = ''
for i in range(len(cipher)):
plain += chr(s[i%256] ^ ord(cipher[i])) #异或
f = open('RC4_plain.txt', 'wb')
f.write(plain.encode('utf-8'))
f.close()
etime = time.time()
print('明文文存储在RC4_plain.txt中\n')
print('总耗时%s秒\n' % (etime - stime))
if __name__=='__main__':
choice=input("1.加密输1\n2.解密输2\n")
if choice=='1':
Encode()
elif choice=='2':
Decode()
else:
print("请输入1-3!\n")


A5:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import time
X=''
Y=''
Z=''
def LFSRinit(): #用64bit密钥初始3个移位寄存器
global X
global Y
global Z
key=input('请输入8位密钥:\n')
while len(key)!=8:
key = input('请输入8位密钥:\n')
result=''
for i in key:
m=bin(ord(i))[2:].zfill(8)
result+=m
X=result[:19]
Y=result[19:41]
Z=result[41:]

def Move(x1,y1,z1,x,y,z): #钟控移位,占多数者移位
global X
global Y
global Z
if (x1==1 and y1==1 and z1==1) or (x1==0 and y1==0 and z1==0):
X=str(x)+X[:-1]
Y=str(y)+Y[:-1]
Z=str(z)+Z[:-1]
elif (x1==1 and y1==1 and z1==0) or (x1==0 and y1==0 and z1==1):

X = str(x) + X[:-1]
Y = str(y) + Y[:-1]
elif (x1==1 and y1==0 and z1==1) or (x1==0 and y1==1 and z1==0):

X = str(x) + X[:-1]
Z = str(z) + Z[:-1]
elif (x1==1 and y1==0 and z1==0) or (x1==0 and y1==1 and z1==1):

Y = str(y) + Y[:-1]
Z = str(z) + Z[:-1]
def createKey():
global X
global Y
global Z
LFSRinit()
result=''
for i in range(114): #生成114位密钥
result += str(int(X[-1:]) ^ int(Y[-1:]) ^ int(Z[-1:]))
#补位候补
x = int(X[13:14]) ^ int(X[16:17]) ^ int(X[17:18]) ^ int(X[18:19])
y = int(Y[12:13]) ^ int(Y[16:17]) ^ int(Y[20:21]) ^ int(Y[21:22])
z = int(Z[17:18]) ^ int(Z[18:19]) ^ int(Z[21:22]) ^ int(Z[22:23])
#钟控信号
x1=int(X[9:10])
y1=int(Y[11:12])
z1=int(Z[11:12])
Move(x1,y1,z1,x,y,z)
return result
def XOR(str,key):
result=''
for i in range(len(str)):
if str[i]==key[i]:
result+='0'
else:
result+='1'
return result

def Encode():
key=createKey()
filename=input("请输入要加密的文件名:\n")
stime = time.time()
plain = open(filename, 'rb').read().decode()
plainbin=''
cipherbin=''
cipher=''
for i in plain:
plainbin+=bin(ord(i))[2:].zfill(8)

for i in range(0,len(plainbin),114):
cipherbin+=XOR(plainbin[i:i+114],key)

for i in range(0,len(cipherbin),8):
cipher+=chr(int(cipherbin[i:i+8],2))

f = open('A5_cipher.txt', 'wb')
f.write(cipher.encode('utf-8'))
f.close()
etime=time.time()
print('密文存储在A5_cipher.txt中\n')
print('总耗时%s秒'%(etime-stime))
def Decode():
key=createKey()
filename = input("请输入要解密的文件名:\n")
stime = time.time()
cipher = open(filename, 'rb').read().decode()
cipherbin=''
plainbin=''
plain=''
for i in cipher:
cipherbin+=bin(ord(i))[2:].zfill(8)

for i in range(0,len(cipherbin),114):
plainbin+=XOR(cipherbin[i:i+114],key)

for i in range(0,len(plainbin),8):
plain+=chr(int(plainbin[i:i+8],2))

f = open('A5_plain.txt', 'wb')
f.write(plain.encode('utf-8'))
f.close()
etime = time.time()
print('明文存储在A5_plain.txt中\n')
print('总耗时%s秒' % (etime - stime))
if __name__=='__main__':
choice = input("1.加密输1\n2.解密输2\n")
if choice == '1':
Encode()
elif choice == '2':
Decode()
else:
print("请输入1-3!\n")


原文作者: n3k0

发表日期: December 15th 2017, 2:04:57

发出嘶吼: 没有魔夜2玩我要死了

CATALOG