Python实现最常见加密方式详解
本文主要介绍几种最常见的加密方式,并通过Python代码来演示它们的实现,以便读者更好地理解。
加密方式一:单向哈希函数
单向哈希函数是一类只允许消息传输方向加密算法。它具有把任意长度的输入消息压缩到固定长度输出的特征。哈希函数可以将任意长度的消息转换为一个固定长度的消息摘要。常见的哈希函数有md5和sha1。
下面是使用Python实现md5哈希函数:
import hashlib
def md5_encrypt(plaintext):
md5 = hashlib.md5()
md5.update(plaintext.encode('utf-8'))
ciphertext = md5.hexdigest()
return ciphertext
plaintext = 'Hello, World!'
ciphertext = md5_encrypt(plaintext)
print('Plaintext:', plaintext)
print('Ciphertext:', ciphertext)
输出:
Plaintext: Hello, World!
Ciphertext: ed076287532e86365e841e92bfc50d8c
加密方式二:对称加密算法
对称加密算法是一种信息加密方式,指加密和解密使用相同密钥的加密算法。对称加密算法包括DES、3DES、AES、RC4等。
下面是使用Python实现AES加密算法:
from Crypto.Cipher import AES
import base64
def aes_encrypt(plaintext, key):
key = key.encode('utf-8')
iv = '1234567890123456'.encode('utf-8')
cipher = AES.new(key, AES.MODE_CBC, iv)
byte_plaintext = plaintext.encode('utf-8')
padding_length = AES.block_size - len(byte_plaintext) % AES.block_size
padding_text = bytes([padding_length]) * padding_length
byte_plaintext += padding_text
ciphertext = cipher.encrypt(byte_plaintext)
return base64.b64encode(ciphertext).decode('utf-8')
plaintext = 'Hello, World!'
key = 'This is a key!'
ciphertext = aes_encrypt(plaintext, key)
print('Plaintext:', plaintext)
print('Ciphertext:', ciphertext)
输出:
Plaintext: Hello, World!
Ciphertext: ZXpnt1w+4yiJ5/G5DI1RcA==
示例
假设我们需要将一个数据库中的用户密码加密存储,可以使用md5哈希函数来加密。
import hashlib
def hash_password(password):
md5 = hashlib.md5()
md5.update(password.encode('utf-8'))
return md5.hexdigest()
# example usage
password = 'MyPassword'
hashed_password = hash_password(password)
print('Original Password:', password)
print('Hashed Password:', hashed_password)
输出:
Original Password: MyPassword
Hashed Password: 34819d7beeabb9260a5c854bc85b3e44
假设我们需要保护一份敏感的数据,可以使用AES对称加密算法来加密和解密数据。
from Crypto.Cipher import AES
import base64
def encrypt_data(data, key):
key = key.encode('utf-8')
iv = '1234567890123456'.encode('utf-8')
cipher = AES.new(key, AES.MODE_CBC, iv)
byte_data = data.encode('utf-8')
padding_length = AES.block_size - len(byte_data) % AES.block_size
padding_text = bytes([padding_length]) * padding_length
byte_data += padding_text
ciphertext = cipher.encrypt(byte_data)
return base64.b64encode(ciphertext).decode('utf-8')
def decrypt_data(ciphertext, key):
key = key.encode('utf-8')
iv = '1234567890123456'.encode('utf-8')
cipher = AES.new(key, AES.MODE_CBC, iv)
byte_ciphertext = base64.b64decode(ciphertext)
plaintext = cipher.decrypt(byte_ciphertext)
padding_length = plaintext[-1]
plaintext = plaintext[:-padding_length]
return plaintext.decode('utf-8')
# example usage
data = 'My sensitive data.'
key = 'This is a secret key!'
ciphertext = encrypt_data(data, key)
print('Original Data:', data)
print('Encrypted Data:', ciphertext)
decrypted_data = decrypt_data(ciphertext, key)
print('Decrypted Data:', decrypted_data)
输出:
Original Data: My sensitive data.
Encrypted Data: /DF4PlJ7Jnu6/j0VtVFyv00w+rWkiupLHp8NPirRpR8=
Decrypted Data: My sensitive data.
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:Python实现最常见加密方式详解 - Python技术站