@ -0,0 +1,61 @@
|
||||
import binascii
|
||||
import hashlib
|
||||
import base64
|
||||
|
||||
from Crypto.Cipher import DES
|
||||
from Crypto.Random import get_random_bytes
|
||||
|
||||
import rsa
|
||||
|
||||
|
||||
def des_key_encrypt_with_rsa():
|
||||
# 读取 DES 密钥
|
||||
with open('sandbox_sender/desPassword.txt', 'r') as des_password_gotcha:
|
||||
des_pwd = des_password_gotcha.readline().strip() # 移除换行符
|
||||
|
||||
# 读取公钥
|
||||
with open('public_key_receiver.txt', 'r') as pub_key_radio:
|
||||
public_key_pair_string = pub_key_radio.readline().strip() # 移除换行符
|
||||
|
||||
# 假设公钥是以 `(n, e)` 格式存储
|
||||
try:
|
||||
public_key_pair_string2pair_step_1 = public_key_pair_string.split(', ')
|
||||
|
||||
public_key_pair_string2pair_step_2_a = public_key_pair_string2pair_step_1[0]
|
||||
if public_key_pair_string2pair_step_2_a.startswith('('):
|
||||
public_key_pair_string2pair_step_2_a = public_key_pair_string2pair_step_2_a[1:]
|
||||
|
||||
public_key_pair_string2pair_step_2_b = public_key_pair_string2pair_step_1[1]
|
||||
if public_key_pair_string2pair_step_2_b.endswith(')'):
|
||||
public_key_pair_string2pair_step_2_b = public_key_pair_string2pair_step_2_b[:-1]
|
||||
|
||||
# 转换成整数
|
||||
n = int(public_key_pair_string2pair_step_2_a)
|
||||
e = int(public_key_pair_string2pair_step_2_b)
|
||||
|
||||
# 打印输出以验证解析是否正确
|
||||
#print(f"Parsed RSA public key: n = {n}, e = {e}")
|
||||
except Exception as ex:
|
||||
print(f"Error parsing the RSA public key: {ex}")
|
||||
return
|
||||
|
||||
# 创建 RSA 公钥对象
|
||||
try:
|
||||
rsa_public_key = RSA.construct((n, e)) # 使用解析出的 (n, e) 创建 RSA 公钥
|
||||
rsa_cipher = PKCS1_OAEP.new(rsa_public_key)
|
||||
|
||||
# 将 DES 密钥字符串编码为字节
|
||||
des_pwd_bytes = des_pwd.encode()
|
||||
|
||||
# 使用 RSA 加密 DES 密钥
|
||||
encrypted_des_pwd = rsa_cipher.encrypt(des_pwd_bytes)
|
||||
|
||||
# 将加密后的 DES 密钥转换为十六进制字符串
|
||||
encrypted_des_pwd_hex = encrypted_des_pwd.hex()
|
||||
|
||||
# 将加密后的 DES 密钥保存到文件
|
||||
with open('outbox/encrypted_pwd_des.txt', 'w') as encrypted_des_pwd_container:
|
||||
encrypted_des_pwd_container.write(encrypted_des_pwd_hex)
|
||||
print("DES password has been encrypted.")
|
||||
except ValueError as ve:
|
||||
print(f"Error while constructing RSA public key: {ve}")
|
||||
Loading…
Reference in new issue