Advertisement
Guest User

Untitled

a guest
Dec 26th, 2021
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.90 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. from Crypto.Cipher import AES
  3. from Crypto.Util.Padding import pad, unpad
  4. from Crypto.Protocol.KDF import PBKDF2
  5. from Crypto.Random import get_random_bytes
  6.  
  7. # 固定的 salt
  8. salt = get_random_bytes(32)
  9.  
  10. # 密碼
  11. password = 'MY SERCRET PASSWORD'
  12.  
  13. # 根據密碼與 salt 產生金鑰
  14. key = PBKDF2(password, salt, dkLen=32)
  15.  
  16. # 要加密的資料(必須為 bytes)
  17. data = b'My secret data.'
  18.  
  19. # 以金鑰搭配 CBC 模式建立 cipher 物件
  20. cipher = AES.new(key, AES.MODE_CBC)
  21.  
  22. # 將輸入資料加上 padding 後進行加密
  23. cipheredData = cipher.encrypt(pad(data, AES.block_size))
  24.  
  25. print(f'salt = {salt}')
  26. print(f'iv  = {cipher.iv}')
  27. print(f'enc = {cipheredData}')
  28.  
  29. print(f'''
  30. salt = {salt}
  31. key = PBKDF2(password, salt, dkLen=32)
  32. iv = {cipher.iv}
  33. cipher = AES.new(key, AES.MODE_CBC, iv=iv)
  34. originalData = unpad(cipher.decrypt(cipheredData), AES.block_size)
  35. ''')
  36.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement