Current collected state Current
Observed since 20 Oct, 19:57 UTC
No superseded metadata states collected. Unchanged observations do not create new revisions.
Advanced Encryption Standard
載入Crypto module,此module包含加解密過程中所需的函式。
from AdvancedEncryptionStandard.Cipher import Crypto
使用AESkey的generate_key()方法產生金鑰、長度限定128、192、256。並且可以使用extract_key()將金鑰檔(.pem)儲存下來。
AES_key = Crypto.AESKey.generate_key(256)
with open('k1.pem', 'wb') as f:
f.write(AES_key.extract_key())
加密可接受的資料型態為bytes,若不是bytes可使用package中的converter針對不同資料型態來源進行轉換。
from DualRegev.IO import Converter
以下是加密的詳細流程:首先建立加密物件,接著使用import_key方法把金鑰讀取進來後,使用encrypt方法加密即可。
crypto_obj = Crypto.AESCrypto()
# 載入金鑰
with open('k1.pem', 'rb') as f:
key = f.read()
crypto_obj.import_key(key)
# 加密訊息
data = 'sddas'
data = data.encode()
cipher_text = crypto_obj.encrypt(data)
# 儲存密文
with open('cipher_text.bin', 'wb') as f:
f.write(cipher_text)
解密方法回傳的資料型態為bytes,使用AESCrypto中的方法decrypt即可進行解密。
# 創建加密工具物件
crypto_obj = Crypto.AESCrypto()
# 載入金鑰
with open('k1.pem', 'rb') as f:
key = f.read()
crypto_obj.import_key(key)
# 載入密文
with open('cipher_text.bin', 'rb') as f:
cipher_text = f.read()
# 解密訊息
data = crypto_obj.decrypt(cipher_text).decode()
print(data)
Download counts and popularity will appear here as the collector receives them.
| Version | Published | Direct dependencies | Open version |
|---|---|---|---|
| 1.0.0 | 30 Jul 2024 | 0 |
This describes our collected data; it is not a security assessment.
Shared repository data · GitHub
Observed since 20 Oct, 19:57 UTC
No superseded metadata states collected. Unchanged observations do not create new revisions.
package:d13cdd120429cf5852464d8b3e6597711fb79528f62b940ff5422964faa929ba