test.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. #encoding:UTF8
  2. from Crypto.Cipher import AES
  3. import base64
  4. password = 'zDXjL5mx5HeUgmsg7HyKLg==' #秘钥
  5. text = '137667617' #需要加密的内容
  6. model = AES.MODE_CTR #定义模式
  7. aes = AES.new(password.encode("UTF8"),model) #创建一个aes对象
  8. en_text = aes.encrypt(text.encode("UTF8")) #加密明文
  9. print(en_text)
  10. en_text1 = base64.b64encode(en_text) #将返回的字节型数据转进行base64编码
  11. print(en_text1)
  12. import base64
  13. from Crypto.Cipher import AES
  14. from Crypto import Random
  15. import pandas as pd
  16. class AESCipher:
  17. def __init__(self):
  18. '''
  19. CBC加密需要一个十六位的key(密钥)和一个十六位iv(偏移量)
  20. '''
  21. self.key = self.check_key(base64.b64decode('zDXjL5mx5HeUgmsg7HyKLg==') )
  22. # 数据块的大小 16位
  23. self.BS = 32
  24. # CBC模式 相对安全 因为有偏移向量 iv 也是16位字节的
  25. self.mode = AES.MODE_ECB
  26. # 填充函数 因为AES加密是一段一段加密的 每段都是BS位字节,不够的话是需要自己填充的
  27. self.pad = lambda s: s + (self.BS - len(s.encode()) % self.BS)*chr(self.BS - len(s.encode()) % self.BS)
  28. # 将填充的数据剔除
  29. self.unpad = lambda s: s[:-ord(s[len(s) - 1:])]
  30. def check_key(self, key):
  31. '''
  32. 检测key的长度是否为16,24或者32bytes的长度
  33. '''
  34. try:
  35. if isinstance(key, bytes):
  36. assert len(key) in [16, 24, 32]
  37. return key
  38. elif isinstance(key, str):
  39. assert len(key.encode()) in [16, 24, 32]
  40. return key.encode()
  41. else:
  42. raise Exception('密钥必须为str或bytes,不能为%s'%type(key))
  43. except AssertionError:
  44. print('输入的长度不正确')
  45. def check_data(self, data):
  46. '''
  47. 检测加密的数据类型
  48. '''
  49. if isinstance(data, int):
  50. data = str(data)
  51. elif isinstance(data, bytes):
  52. data = data.decode()
  53. elif isinstance(data, str):
  54. pass
  55. else:
  56. raise Exception('加密的数据必须为str或bytes,不能为%s'%type(data))
  57. return data
  58. def text2hex(self,_str):
  59. list_hex = []
  60. if isinstance(_str,str):
  61. _str1 = _str.encode()
  62. elif isinstance(_str,bytes):
  63. _str1 = _str
  64. for _hex in map(hex,_str1):
  65. _i = int(_hex[2:],16)
  66. while(True):
  67. a = _i&0xFF
  68. list_hex.append(hex(a)[2:].rjust(2,"0"))
  69. _i = _i >>8
  70. if _i==0:
  71. break
  72. return "".join(list_hex)
  73. def encrypt(self, raw):
  74. raw = self.check_data(raw)
  75. raw = self.pad(raw).encode()
  76. # 随机获取iv
  77. iv = Random.new().read(AES.block_size)
  78. # 定义初始化
  79. cipher = AES.new(self.key, self.mode)
  80. # 此处是将密文和iv一起 base64 解密的时候就可以根据这个iv来解密
  81. return self.text2hex(cipher.encrypt(raw))
  82. return base64.b64encode(cipher.encrypt(raw)).decode()
  83. def decrypt(self, enc):
  84. # 先将密文进行base64解码
  85. enc = base64.b64decode(enc)
  86. # 取出iv值
  87. iv = enc[:self.BS]
  88. # 初始化自定义
  89. cipher = AES.new(self.key, self.mode, iv)
  90. # 返回utf8格式的数据
  91. return self.unpad(cipher.decrypt(enc[self.BS:])).decode()
  92. text = '{"docid":137667617}' # 待加密文本
  93. aes = AESCipher()
  94. print(aes.encrypt(text))
  95. print("42\u4e07\u5143")
  96. print(139106361%500+1)
  97. print(679/37643)