加密逻辑 解密逻辑 可以查看 上一篇 的java版本
ebc安全加密逻辑 if方法去掉,中间key为默认测试参数
提供一个 easok的测试 代码
if __name__ == '__main__':
print('1234567890123456','java encrypt:','b772a50be83a7c14fe882fc8548cd5d847b7f7a1bfcd0403244635ec3ed05406')
pwd = "7985c4cc0cf22caccddb0b864f79e5dd";
iv = "e3205234a872012af4f3ac5dbd9c810c";
pwd = hexToByte(pwd)
iv = hexToByte(iv)
pc = prpcrypt(pwd, iv)
import sys
text=sys.argv[1]
text='各位辛苦了'
text='hell:各位辛苦了&*()123QWE'
print("源串Hex:" , byteToHex(text.encode('utf-8')))
print("密码Hex:" + byteToHex(pwd));
print("偏移Hex:" + byteToHex(iv));
#开始加密
e = pc.encrypt(text) #加密
d = pc.decrypt(e) #解密
print('encrypt e type:',type(d),',d type:',type(d))
print ("加密串:",e )
print ("解密串:",d )
def byteToHex( _bytes ):
#return ''.join( [ "%02x" % x for x in _bytes ] ).strip()
return "".join(map(lambda b: format(b, "02x"), _bytes))
def hexToByte( hexStr ):
return bytes.fromhex(hexStr)
class prpcrypt():
def __init__(self,key,iv):
self.key = key
self.iv = iv
self.mode = AES.MODE_CBC
self.BS = AES.block_size
#补位
#self.pad = lambda s: s + (self.BS - len(s) % self.BS) * chr(self.BS - len(s) % self.BS)
self.pad = lambda s: s + (self.BS - len(s.encode('utf-8')) % self.BS) * chr(self.BS - len(s.encode('utf-8')) % self.BS)
self.unpad = lambda s : s[0:-ord(s[-1])]
def encrypt(self,text):
print ("pad text ...",len(text) ,'->',len(text.encode('utf-8')))
text = self.pad(text)
print ("pad text ok.",len(text),'->',byteToHex(text.encode('utf-8')))
cryptor = AES.new(self.key,self.mode,self.iv)
#目前AES-128 足够目前使用
ciphertext = cryptor.encrypt(text)
print ("ciphertext type:",type(ciphertext),' :',ciphertext)
#把加密后的字符串转化为16进制字符串
return byteToHex(ciphertext)
#return b2a_hex(ciphertext)
#return b2a_hex(ciphertext).decode("utf-8")
#解密后,去掉补足的空格用strip() 去掉
def decrypt(self,text):
cryptor = AES.new(self.key,self.mode, iv)
plain_text = cryptor.decrypt(a2b_hex(text))
pad_plain_text = plain_text.decode('utf-8').rstrip('\0')
return self.unpad(pad_plain_text)
其引用的 AES文件,开头需要引起注意的,是不同版本引用引用包的逻辑
print("os:", os.name);
if os.name == "nt":
from Cryptodome.Cipher import AES
else:
from Crypto.Cipher import AES
def byteToHex(_bytes):
print 'byteToHex...'
print type(_bytes)
return ''.join( [ "%02x" % x for x in _bytes ] ).strip()
#return "".join(map(lambda b: format(b, "02x"), _bytes))
def hexToByte(hexStr):
return bytes.fromhex(hexStr)
def hexToByte(s):
base='0123456789ABCDEF'
i=0
s = s.upper()
s1=''
while i < len(s):
c1=s[i]
c2=s[i+1]
i+=2
b1=base.find(c1)
b2=base.find(c2)
if b1 == -1 or b2 == -1:
return None
s1+=chr((b1 << 4)+b2)
return s1
class Prpcrypt():
def reset(self, key, iv):
key = hexToByte(key)
iv = hexToByte(iv)
self.key = key
self.iv = iv
def __init__(self):
_key = "51492ad8fe708880d7d4cebb808ee025"
_iv = "61ec9e40971e944470f4a0d2fc3025f7"
key = hexToByte(_key)
iv = hexToByte(_iv)
self.key = key
self.iv = iv
self.mode = AES.MODE_CBC
self.BS = AES.block_size
# 补位
# self.pad = lambda s: s + (self.BS - len(s) % self.BS) * chr(self.BS - len(s) % self.BS)
self.pad = lambda s: s + (self.BS - len(s.encode('utf-8')) % self.BS) * chr(
self.BS - len(s.encode('utf-8')) % self.BS)
self.unpad = lambda s: s[0:-ord(s[-1])]
# EAS加密
# 入参 :stringOrbytes 要进行加密的字符串或二进制数组
# 返回值:bytes AES加密字节数组
def encrypt(self, text):
if not type(str(''))==type(text):
text=str(text)
print"原始串:", type(text), text
text = self.pad(text)
cryptor = AES.new(self.key, self.mode, self.iv)
cipherbytes = cryptor.encrypt(text)
return cipherbytes
# EAS解密
# 入参:encrypt_value 类型为bytes 或 16进制串
# 返回值:bytes
def decrypt(self, encrypt_value):
cryptor = AES.new(self.key, self.mode, self.iv)
plain_bytes = cryptor.decrypt(encrypt_value)
pad_plain_text = plain_bytes.decode('utf-8').rstrip('\0')
plain_text=self.unpad(pad_plain_text)
return plain_text
# 先EAS加密再进行base64编码
# 入参:string 要进行加密的字符串或二进制数组
# 返回值:base64编码bytes
def encrypt_base64(self, string):
print("Encrypt_base64 starting... :")
temp = self.encrypt(string)
e = base64.b64encode(temp)
print"加密串:", type(e), e, "加密hex串:",b2a_hex(e)
return e
# base64解码再进行EAS解密
def decrypt_base64(self, byte):
print("Decrypt_base64 starting... :")
temp = base64.b64decode(byte)
return self.decrypt(temp)