from Crypto.Cipher import AES from binascii import b2a_hex, a2b_hex import wmi # pip install pycryptodome # pip install wmi class Aes(object): # 如果text不足16位的倍数就用空格补足为16位 def add_to_16(self, text): if len(text.encode('utf-8')) % 16: add = 16 - (len(text.encode('utf-8')) % 16) else: add = 0 text = text + ('\0' * add) return text.encode('utf-8') # 加密函数 def encrypt(self, text, key, iv): mode = AES.MODE_CBC text = self.add_to_16(text) cryptos = AES.new(key=key.encode('utf-8'), mode=mode, iv=iv.encode('utf-8')) cipher_text = cryptos.encrypt(text) # 因为AES加密后的字符串不一定是ascii字符集的,输出保存可能存在问题,所以这里转为16进制字符串 return b2a_hex(cipher_text) # 解密后,去掉补足的空格用strip() 去掉 def decrypt(self, text, key, iv): mode = AES.MODE_CBC cryptos = AES.new(key=key.encode('utf-8'), mode=mode, iv=iv.encode('utf-8')) plain_text = cryptos.decrypt(a2b_hex(text)) return bytes.decode(plain_text).rstrip('\0') def get_computer_key(self): c = wmi.WMI() system_info = c.Win32_ComputerSystem()[0] mac_address = c.Win32_NetworkAdapterConfiguration(IPEnabled=True)[0].MACAddress cpu_id = c.Win32_Processor()[0].ProcessorId machine_code = system_info.Manufacturer + '' + system_info.Model + '' + mac_address + '' + cpu_id machine_code = machine_code.replace(" ", "").replace(":", "") return machine_code def get_authorization(self, key_text, key, iv): machine_code = self.get_computer_key() try: text = self.decrypt(key_text, key, iv) # 解密 except: return "false" try: _machine_code, authorization = text.split("_") if _machine_code != machine_code: return "false" except: return "false" return authorization def get_key(self, authorization, key, iv): machine_code = self.get_computer_key() key = self.encrypt("{}_{}".format(machine_code, authorization), key, iv) # 加密 return key.decode() if __name__ == '__main__': print(Aes().get_computer_key()) # e = Aes().encrypt("hellosadsasdasdas1111+ world", "8E5EC2AC2191167DF9B753BA93A1E7B1", "0112030405060701") # 加密 # d = Aes().decrypt(e, "8E5EC2AC2191167DF9B753BA93A1E7B1", "0112030405060701") # 解密 c = Aes().get_key("heldasdasdsdsadsadsadsadsadsadsdsadsadasdasds", "8E5EC2AC2191167DF9B753BA93A1E7B1", "0112030405060701") print(c) print( Aes().get_authorization(c, "8E5EC2AC2191167DF9B753BA93A1E7B1", "0112030405060701")) # print("加密:", e) # print("解密:", d)