Skip to main content

Advanced Encryption Standard

Project description

簡介

  • 此repository沒有使用任何外部crypt library實現AES加密算法,金鑰長度可接受128、192、256bits。
  • 通過solar qube源碼掃描,security報警數=0。
  • 明明有library,為甚麼要從頭開始寫呢?因為閒閒沒事做(X別人的library在做源碼掃描時總是很多問題跑出來,改別人300個問題不如從頭開始QQ。

注意事項

  • 開發版本:python 3.9.1
  • 外部引用:__future__、random、hashlib、base64

如何使用

1. 生成公鑰、私鑰對

  載入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())

2. 資料加密

  加密可接受的資料型態為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)

3. 資料解密

  解密方法回傳的資料型態為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)

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

advancedencryptionstandard-1.0.0.tar.gz (8.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

AdvancedEncryptionStandard-1.0.0-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file advancedencryptionstandard-1.0.0.tar.gz.

File metadata

File hashes

Hashes for advancedencryptionstandard-1.0.0.tar.gz
Algorithm Hash digest
SHA256 6310b78ad6a9a4af31a252dfe11d692e57c094863c118dc6ac449be8c527234c
MD5 192575db0aa5b17dc2f9d83ed51947e7
BLAKE2b-256 4c5800651e00f192c201d5a0d91a925aca559040c88ada5cd758a870092759a1

See more details on using hashes here.

File details

Details for the file AdvancedEncryptionStandard-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for AdvancedEncryptionStandard-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 9a790280d5152f813f3bf0f8012e761e2c41a7746ae684ed349282906a42b6a8
MD5 976db210d1e7a24c8448b9d93ae99c9d
BLAKE2b-256 2a769203cf6345e6aa333e8d9cfe166d19d47c112fcd7ae0ec985677acda1586

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page