AES_crypt.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. """
  2. @File : AES_new.py
  3. @Author: lee
  4. @Date : 2022/3/8/0008 9:41:14
  5. @Desc :
  6. """
  7. import base64
  8. import wmi
  9. from Crypto.Cipher import AES
  10. passwd = "123456781234567"
  11. iv = '1234567812345678'
  12. class AESCrypt:
  13. def __init__(self, key, model, iv):
  14. self.key = self.add_16(key)
  15. self.model = model
  16. self.iv = self.add_16(iv)
  17. def add_16(self, par):
  18. if type(par) == str:
  19. par = par.encode()
  20. while len(par) % 16 != 0:
  21. par += b'\x00'
  22. return par
  23. def aesencrypt(self, text):
  24. text = self.add_16(text)
  25. if self.model == AES.MODE_CBC:
  26. aes = AES.new(self.key, self.model, self.iv)
  27. elif self.model == AES.MODE_ECB:
  28. aes = AES.new(self.key, self.model)
  29. encrypt_text = aes.encrypt(text)
  30. return base64.encodebytes(encrypt_text).decode('utf8').rstrip("\n")
  31. def aesdecrypt(self, text):
  32. if self.model == AES.MODE_CBC:
  33. aes = AES.new(self.key, self.model, self.iv)
  34. elif self.model == AES.MODE_ECB:
  35. aes = AES.new(self.key, self.model)
  36. try:
  37. decrypt_text = text.encode('utf8')
  38. decrypt_text = base64.decodebytes(decrypt_text)
  39. decrypt_text = aes.decrypt(decrypt_text)
  40. decrypt_text = decrypt_text.strip(b"\x00")
  41. return decrypt_text.decode('utf8')
  42. except Exception as e:
  43. print(e)
  44. return None
  45. def get_cpu_code():
  46. try:
  47. c = wmi.WMI()
  48. for cpu in c.Win32_Processor():
  49. cpu_code = cpu.ProcessorId.strip()
  50. return cpu_code
  51. except Exception as e:
  52. return None
  53. def decrypt(text):
  54. aes_cryptor = AESCrypt(passwd, AES.MODE_CBC, iv) # CBC模式
  55. return aes_cryptor.aesdecrypt(text)
  56. def encrypt(text):
  57. aes_cryptor = AESCrypt(passwd, AES.MODE_CBC, iv)
  58. return aes_cryptor.aesencrypt(text)
  59. if __name__ == '__main__':
  60. passwd = "123456781234567"
  61. iv = '1234567812345678'
  62. aescryptor = AESCrypt(passwd, AES.MODE_CBC, iv) # CBC模式
  63. # aescryptor = Aescrypt(passwd,AES.MODE_ECB,"") # ECB模式
  64. text = "root"
  65. en_text = aescryptor.aesencrypt(text)
  66. print("密文:", en_text)
  67. text = aescryptor.aesdecrypt(en_text)
  68. print("明文:", text)