apppublic/appPublic/rc4.py

185 lines
4.6 KiB
Python
Raw Normal View History

2019-07-16 16:33:07 +08:00
# -*- coding: utf-8 -*-
2022-02-24 13:24:49 +08:00
import time
import datetime
2019-07-16 16:33:07 +08:00
import random, base64
from hashlib import sha1
class RC4:
2019-08-12 11:55:18 +08:00
def __init__(self,data_coding='utf8'):
2019-07-16 16:33:07 +08:00
self.bcoding = 'iso-8859-1'
self.dcoding = data_coding
self.salt = b'AFUqx9WZuI32lnHk'
def _crypt(self,data,key):
"""RC4 algorithm return bytes"""
x = 0
box = [i for i in 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 = 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).encode(self.bcoding)
2021-07-13 16:13:59 +08:00
def encode_bytes(self, bdata, key):
a = sha1(key + self.salt)
k = a.digest()
data = self.salt + self._crypt(bdata, k)
return data
2019-08-12 11:55:18 +08:00
def encode(self,data, key,encode=base64.b64encode, salt_length=16):
2019-07-16 16:33:07 +08:00
"""RC4 encryption with random salt and final encoding"""
2021-07-13 16:13:59 +08:00
if type(data)==type(''):
data = data.encode(self.dcoding)
key = key.encode(self.bcoding)
self.encode_bytes(data, key)
2019-07-16 16:33:07 +08:00
if encode:
data = encode(data)
return data.decode(self.dcoding)
2021-07-13 16:13:59 +08:00
def decode_bytes(self, data, key):
salt_length = 16
salt = data[:salt_length]
a = sha1(key + self.salt)
k = a.digest() #.decode('iso-8859-1')
r = self._crypt(data[salt_length:], k)
return r
2019-08-12 11:55:18 +08:00
def decode(self,data, key,decode=base64.b64decode, salt_length=16):
2019-07-16 16:33:07 +08:00
"""RC4 decryption of encoded data"""
2021-07-13 16:13:59 +08:00
if type(data)==type(''):
data = data.encode(self.dcoding)
key = key.encode(self.bcoding)
2019-07-16 16:33:07 +08:00
if decode:
data = decode(data)
2021-07-13 16:13:59 +08:00
r = self.decode_bytes(data, key)
2019-07-16 16:33:07 +08:00
return r.decode(self.dcoding)
2022-02-24 13:24:49 +08:00
class KeyChain(object):
2022-03-18 12:45:57 +08:00
def __init__(self, seed_str, crypter=None, keylen=23, period=600, threshold=60, time_delta=0):
2022-02-24 13:24:49 +08:00
self.seed_str = seed_str
2022-03-18 23:08:14 +08:00
if isinstance(self.seed_str, str):
self.seed_str = self.seed_str.encode('utf-8')
2022-02-28 14:01:18 +08:00
self.period = int(period)
self.threshold = int(threshold)
2022-02-24 13:24:49 +08:00
self.crypter = crypter
2022-03-18 12:45:57 +08:00
self.time_delta = time_delta
2022-02-28 14:01:18 +08:00
if crypter is None:
self.crypter = RC4()
2022-02-24 13:24:49 +08:00
self.keylen = keylen
self.keypool = {
}
2022-02-28 12:06:25 +08:00
delta = datetime.timedelta(0)
self.timezone = datetime.timezone(delta, name='gmt')
2022-02-24 13:24:49 +08:00
2022-03-18 12:45:57 +08:00
def get_timestamp(self):
ts = int(time.time()) - self.time_delta
return ts
2022-02-28 14:01:18 +08:00
def is_near_bottom(self, indicator=None):
2022-03-18 12:45:57 +08:00
ts = self.get_timestamp()
2022-02-28 14:01:18 +08:00
i = indicator
if i is None:
i = self.get_indicator(ts)
if i + self.threshold > ts:
return True
return FalseTrue
def is_near_top(self, indicator=None):
2022-03-18 12:45:57 +08:00
ts = self.get_timestamp()
2022-02-28 14:01:18 +08:00
i = indicator
if i is None:
i = self.get_indicator(ts)
if i + self.period - self.threshold < ts:
return True
return False
def get_indicator(self, ts=None):
if ts is None:
2022-03-18 12:45:57 +08:00
ts = self.get_timestamp()
2022-02-28 14:01:18 +08:00
return int(ts / self.period) * self.period
def genKey(self, indicator):
vv = indicator
2022-02-24 13:24:49 +08:00
if self.keypool.get(vv):
return self.keypool[vv]
v = vv
k1 = 0
k = ''
m = len(self.seed_str)
while k1 < self.keylen:
j = v % self.keylen
v = v - (j + k1) * m + self.keylen
2022-03-18 23:08:14 +08:00
k = k + chr(self.seed_str[j])
2022-02-28 14:01:18 +08:00
k1 += self.threshold / 2
2022-02-24 13:24:49 +08:00
key = k.encode('utf-8')
self.keypool[vv] = key
dates = [ d for d in self.keypool.keys() ]
2022-02-28 14:01:18 +08:00
for d in dates:
if d < indicator - self.period:
del self.keypool[d]
2022-02-24 13:24:49 +08:00
return key
def encode(self, text):
bdata = text.encode('utf-8')
return self.encode_bytes(bdata)
def encode_bytes(self, bdata):
2022-02-28 14:01:18 +08:00
indicator = self.get_indicator()
key = self.genKey(indicator)
2022-02-24 13:24:49 +08:00
data = key + bdata
return self.crypter.encode_bytes(data, key)
def _decode(self, data, key):
d = self.crypter.decode_bytes(data, key)
if d[:len(key)] == key:
return d[len(key):]
return None
def decode_bytes(self, data):
2022-02-28 14:01:18 +08:00
indicator = self.get_indicator()
key = self.genKey(indicator)
2022-02-24 13:24:49 +08:00
d = self._decode(data, key)
if d is not None:
return d
2022-02-28 14:01:18 +08:00
if self.is_near_bottom(indicator):
indicator -= self.period
key = self.genKey(indicator)
2022-02-24 13:24:49 +08:00
return self._decode(data, key)
2022-02-28 14:01:18 +08:00
if self.is_near_top(indicator):
indicator += self.period
key = self.genKey(indicator)
2022-02-24 13:24:49 +08:00
return self._decode(data, key)
return None
2019-07-16 16:33:07 +08:00
2022-02-24 13:24:49 +08:00
def decode(self, data):
d = self.decode_bytes(data)
if d is None:
return None
return d.decode('utf-8')
2019-07-16 16:33:07 +08:00
if __name__=='__main__':
2019-07-16 17:00:48 +08:00
# 需要加密的数据长度没有限制
2019-07-16 16:33:07 +08:00
# 密钥
2022-02-24 13:24:49 +08:00
2021-07-13 16:13:59 +08:00
data=b"231r3 feregrenerjk gkht324g8924gnfw k;ejkvwkjerv"
key = b'123456'
2019-08-12 11:55:18 +08:00
rc4 = RC4()
2022-02-24 13:24:49 +08:00
kc = KeyChain('in the heaven, we are equal', rc4)
2019-07-16 16:33:07 +08:00
print(data)
# 加码
2022-02-24 13:24:49 +08:00
encoded_data = kc.encode_bytes(data)
2019-07-16 16:33:07 +08:00
print(encoded_data,len(encoded_data) )
# 解码
2022-02-24 13:24:49 +08:00
decoded_data = kc.decode_bytes(encoded_data)
2021-07-13 16:13:59 +08:00
print(data, decoded_data, decoded_data==data)