def fpe(frame_phone_input,root): phone_input = frame_phone_input.get() def fpe_encrypt(plaintext, key, radix=10, rounds=4): # 对明文进行哈希 hash_val = hmac.new(key.encode(), plaintext.encode(), hashlib.sha256).digest() # 将哈希值转换为整数 num_val = int.from_bytes(hash_val, byteorder='big') # 将整数转换为指定进制的字符串 str_val = '' while num_val > 0: num_val, rem = divmod(num_val, radix) str_val = str(rem) + str_val # 对字符串进行 FPE 加密 for i in range(rounds): str_val = ''.join([chr((ord(c) - ord('0') + i) % (radix - 1) + ord('0')) for c in str_val]) # 返回加密后的字符串 return str_val key = 'my-secret-key' encrypted = fpe_encrypt(phone_input, key) phone_label = tk.Label(root, text=("加密后的手机号:", encrypted), font=("微软雅黑 -20")) phone_label.place(x=500, y=560)怎么把加密后的手机号分三行显示
时间: 2024-01-14 08:04:46 浏览: 122
你可以使用字符串的切片功能将加密后的手机号分成三段,然后分别显示在三个 Label 中。下面是一个修改过的代码示例:
```
def fpe(frame_phone_input, root):
phone_input = frame_phone_input.get()
def fpe_encrypt(plaintext, key, radix=10, rounds=4):
# 对明文进行哈希
hash_val = hmac.new(key.encode(), plaintext.encode(), hashlib.sha256).digest()
# 将哈希值转换为整数
num_val = int.from_bytes(hash_val, byteorder='big')
# 将整数转换为指定进制的字符串
str_val = ''
while num_val > 0:
num_val, rem = divmod(num_val, radix)
str_val = str(rem) + str_val
# 对字符串进行 FPE 加密
for i in range(rounds):
str_val = ''.join([chr((ord(c) - ord('0') + i) % (radix - 1) + ord('0')) for c in str_val])
# 返回加密后的字符串
return str_val
key = 'my-secret-key'
encrypted = fpe_encrypt(phone_input, key)
# 将加密后的手机号分成三段
chunk_size = len(encrypted) // 3
chunk1 = encrypted[:chunk_size]
chunk2 = encrypted[chunk_size:2*chunk_size]
chunk3 = encrypted[2*chunk_size:]
# 在三个 Label 中分别显示加密后的手机号的三段
phone_label1 = tk.Label(root, text=("加密后的手机号:", chunk1), font=("微软雅黑 -20"))
phone_label1.place(x=500, y=560)
phone_label2 = tk.Label(root, text=chunk2, font=("微软雅黑 -20"))
phone_label2.place(x=500, y=590)
phone_label3 = tk.Label(root, text=chunk3, font=("微软雅黑 -20"))
phone_label3.place(x=500, y=620)
```
这样,加密后的手机号就会分成三行显示。
阅读全文