Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- from Crypto.Cipher import AES
- from Crypto.Util.Padding import pad, unpad
- from Crypto.Protocol.KDF import PBKDF2
- from Crypto.Random import get_random_bytes
- # 固定的 salt
- salt = get_random_bytes(32)
- # 密碼
- password = 'MY SERCRET PASSWORD'
- # 根據密碼與 salt 產生金鑰
- key = PBKDF2(password, salt, dkLen=32)
- # 要加密的資料(必須為 bytes)
- data = b'My secret data.'
- # 以金鑰搭配 CBC 模式建立 cipher 物件
- cipher = AES.new(key, AES.MODE_CBC)
- # 將輸入資料加上 padding 後進行加密
- cipheredData = cipher.encrypt(pad(data, AES.block_size))
- print(f'salt = {salt}')
- print(f'iv = {cipher.iv}')
- print(f'enc = {cipheredData}')
- print(f'''
- salt = {salt}
- key = PBKDF2(password, salt, dkLen=32)
- iv = {cipher.iv}
- cipher = AES.new(key, AES.MODE_CBC, iv=iv)
- originalData = unpad(cipher.decrypt(cipheredData), AES.block_size)
- ''')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement