-
Notifications
You must be signed in to change notification settings - Fork 5
Open
Description
from Crypto.Cipher import AES
from Crypto.Util.Padding import pad
import binascii
import base64
def encrypt_data(userid):
# 密钥和初始化向量设置
secret_passphrase = '01aaa85c-c60d-43f6-bf6a-cdaa344d'
key = secret_passphrase.encode('utf-8')
iv = "1234567890000000".encode('utf-8')
# 确保密钥长度为32位(AES-256)
if len(key) < 32:
key = key.ljust(32, b'\0')
elif len(key) > 32:
key = key[:32]
# 确保IV长度为16位(AES块大小)
if len(iv) != 16:
iv = iv[:16] if len(iv) > 16 else iv.ljust(16, b'\0')
# 加密用户ID
cipher = AES.new(key, AES.MODE_CBC, iv)
encrypted_userid = cipher.encrypt(pad(userid.encode('utf-8'), AES.block_size))
# 返回加密后的十六进制字符串
return binascii.hexlify(encrypted_userid).decode('utf-8')
使用示例
if name == "main":
userid = "11111111111"
encrypted_userid = encrypt_data(userid)
print(f"Encrypted User ID: {encrypted_userid}")
Metadata
Metadata
Assignees
Labels
No labels