因为实在看不懂python程序,逻辑跟不上,不想现学
下面有案例。
有高手来试试吧
# This is anexample program using a 64 bits key
# you may usingPython 3.x to run it
# you areencouraged to write your own program for RC4 since the algorithm is simple
def rc4crypt(data,key):
x = 0
box = list(range(256))
for i in range(256):
x = (x + box[i] + key[i % len(key)]) %256
box[i], box[x] = box[x], box[i]
x = 0
y = 0
out = []
for char in data:
x = (x + 1) % 256
y = (y + box[x]) % 256
box[x], box[y] = box[y], box[x]
out.append(ord(char) ^ box[(box[x] +box[y]) % 256])
return out
defrc4decrypt(data, key):
x = 0
box = list(range(256))
for i in range(256):
x = (x + box[i] + key[i % len(key)]) %256
box[i], box[x] = box[x], box[i]
x = 0
y = 0
out = []
for char in data:
x = (x + 1) % 256
y = (y + box[x]) % 256
box[x], box[y] = box[y], box[x]
out.append(chr(char ^ box[(box[x] +box[y]) % 256]))
return ''.join(out)
data = "thisis a plaintext"
key = [0x12, 0x34,0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0]
ciphertext =rc4crypt(data, key)
plaintext =rc4decrypt(ciphertext, key)
print('data :',data)
print('key :',key)
print('ciphertext:', ciphertext)
print('plaintext:', plaintext)
*******************theresults for above program is******************
data : this is aplaintext
key : [18, 52, 86,120, 154, 188, 222, 240]
ciphertext : [111,213, 40, 159, 67, 144, 7, 72, 240, 137, 19, 133, 81, 112, 144, 150, 226, 178,176]
plaintext : thisis a plaintext
下面有案例。
有高手来试试吧
# This is anexample program using a 64 bits key
# you may usingPython 3.x to run it
# you areencouraged to write your own program for RC4 since the algorithm is simple
def rc4crypt(data,key):
x = 0
box = list(range(256))
for i in range(256):
x = (x + box[i] + key[i % len(key)]) %256
box[i], box[x] = box[x], box[i]
x = 0
y = 0
out = []
for char in data:
x = (x + 1) % 256
y = (y + box[x]) % 256
box[x], box[y] = box[y], box[x]
out.append(ord(char) ^ box[(box[x] +box[y]) % 256])
return out
defrc4decrypt(data, key):
x = 0
box = list(range(256))
for i in range(256):
x = (x + box[i] + key[i % len(key)]) %256
box[i], box[x] = box[x], box[i]
x = 0
y = 0
out = []
for char in data:
x = (x + 1) % 256
y = (y + box[x]) % 256
box[x], box[y] = box[y], box[x]
out.append(chr(char ^ box[(box[x] +box[y]) % 256]))
return ''.join(out)
data = "thisis a plaintext"
key = [0x12, 0x34,0x56, 0x78, 0x9A, 0xBC, 0xDE, 0xF0]
ciphertext =rc4crypt(data, key)
plaintext =rc4decrypt(ciphertext, key)
print('data :',data)
print('key :',key)
print('ciphertext:', ciphertext)
print('plaintext:', plaintext)
*******************theresults for above program is******************
data : this is aplaintext
key : [18, 52, 86,120, 154, 188, 222, 240]
ciphertext : [111,213, 40, 159, 67, 144, 7, 72, 240, 137, 19, 133, 81, 112, 144, 150, 226, 178,176]
plaintext : thisis a plaintext