欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Python PyCryptodome庫介紹與實例教程

 更新時間:2024年07月06日 15:43:48   作者:engchina  
PyCryptodome提供了豐富的加密功能,可以滿足多種安全需求,本文介紹了幾個常見的使用場景,包括對稱加密、非對稱加密、哈希函數(shù)和消息認(rèn)證碼,感興趣的朋友跟隨小編一起看看吧

PyCryptodome是一個強(qiáng)大的Python加密庫,提供了各種加密算法和工具。本文將介紹PyCryptodome的基本概念和常見使用場景,并通過示例代碼展示如何使用該庫。

1. 安裝

首先,我們需要安裝PyCryptodome庫:

pip install pycryptodome

2. 基本概念

PyCryptodome提供了以下幾種主要類型的加密算法:

  • 對稱加密: AES, DES, 3DES等
  • 非對稱加密: RSA, DSA, ECC等
  • 哈希函數(shù): SHA256, MD5等
  • 消息認(rèn)證碼(MAC): HMAC
  • 數(shù)字簽名

3. 使用場景和示例代碼

3.1 對稱加密 - AES

AES (Advanced Encryption Standard) 是一種廣泛使用的對稱加密算法。以下是使用AES加密和解密的示例:

from Crypto.Cipher import AES
from Crypto.Random import get_random_bytes
from Crypto.Util.Padding import pad, unpad
def encrypt_aes(plaintext, key):
    cipher = AES.new(key, AES.MODE_ECB)
    padded_data = pad(plaintext.encode(), AES.block_size)
    return cipher.encrypt(padded_data)
def decrypt_aes(ciphertext, key):
    cipher = AES.new(key, AES.MODE_ECB)
    padded_data = cipher.decrypt(ciphertext)
    return unpad(padded_data, AES.block_size).decode()
# 生成一個16字節(jié)的隨機(jī)密鑰
key = get_random_bytes(16)
# 加密
message = "Hello, PyCryptodome!"
encrypted = encrypt_aes(message, key)
print(f"加密后: {encrypted.hex()}")
# 解密
decrypted = decrypt_aes(encrypted, key)
print(f"解密后: {decrypted}")

3.2 非對稱加密 - RSA

RSA是一種常用的非對稱加密算法,適用于數(shù)據(jù)加密和數(shù)字簽名。以下是RSA加密和解密的示例:

from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_OAEP
import binascii
# 生成RSA密鑰對
key = RSA.generate(2048)
private_key = key.export_key()
public_key = key.publickey().export_key()
# 加密
message = b"Hello, RSA!"
rsa_public_key = RSA.import_key(public_key)
rsa_public_key = PKCS1_OAEP.new(rsa_public_key)
encrypted = rsa_public_key.encrypt(message)
print(f"加密后: {binascii.hexlify(encrypted)}")
# 解密
rsa_private_key = RSA.import_key(private_key)
rsa_private_key = PKCS1_OAEP.new(rsa_private_key)
decrypted = rsa_private_key.decrypt(encrypted)
print(f"解密后: {decrypted.decode()}")

3.3 哈希函數(shù) - SHA256

哈希函數(shù)用于生成消息摘要,常用于數(shù)據(jù)完整性檢查和密碼存儲。以下是使用SHA256哈希的示例:

from Crypto.Hash import SHA256
def hash_sha256(data):
    hash_object = SHA256.new(data=data.encode())
    return hash_object.hexdigest()
message = "Hello, SHA256!"
hashed = hash_sha256(message)
print(f"SHA256哈希值: {hashed}")

3.4 消息認(rèn)證碼 - HMAC

HMAC (Hash-based Message Authentication Code) 用于驗證消息的完整性和真實性。以下是HMAC的使用示例:

from Crypto.Hash import HMAC, SHA256
def create_hmac(key, message):
    hmac = HMAC.new(key, msg=message.encode(), digestmod=SHA256)
    return hmac.hexdigest()
def verify_hmac(key, message, received_mac):
    hmac = HMAC.new(key, msg=message.encode(), digestmod=SHA256)
    try:
        hmac.hexverify(received_mac)
        return True
    except ValueError:
        return False
# 創(chuàng)建HMAC
secret_key = b"my_secret_key"
message = "Hello, HMAC!"
mac = create_hmac(secret_key, message)
print(f"HMAC: {mac}")
# 驗證HMAC
is_valid = verify_hmac(secret_key, message, mac)
print(f"HMAC驗證結(jié)果: {is_valid}")

4. 總結(jié)

PyCryptodome提供了豐富的加密功能,可以滿足多種安全需求。本文介紹了幾個常見的使用場景,包括對稱加密、非對稱加密、哈希函數(shù)和消息認(rèn)證碼。在實際應(yīng)用中,請根據(jù)具體需求選擇合適的加密算法和參數(shù),并確保正確管理密鑰。

到此這篇關(guān)于Python PyCryptodome庫介紹與實例的文章就介紹到這了,更多相關(guān)Python PyCryptodome庫內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python的rllib庫你了解嗎

    python的rllib庫你了解嗎

    這篇文章主要介紹了python urllib庫的使用,幫助大家更好的利用python學(xué)習(xí)爬蟲,感興趣的朋友可以了解下,希望能夠給你帶來幫助
    2021-11-11
  • Python構(gòu)建XML樹結(jié)構(gòu)的方法示例

    Python構(gòu)建XML樹結(jié)構(gòu)的方法示例

    這篇文章主要介紹了Python構(gòu)建XML樹結(jié)構(gòu)的方法,結(jié)合實例形式分析了Python創(chuàng)建與打印xml數(shù)結(jié)構(gòu)的實現(xiàn)步驟與相關(guān)操作技巧,需要的朋友可以參考下
    2017-06-06
  • Python用sndhdr模塊識別音頻格式詳解

    Python用sndhdr模塊識別音頻格式詳解

    這篇文章主要介紹了Python用sndhdr模塊識別音頻格式詳解,具有一定借鑒價值,需要的朋友可以參考下
    2018-01-01
  • 最新評論