Skip to content
This repository has been archived by the owner on Feb 5, 2024. It is now read-only.

Commit

Permalink
Add files via upload
Browse files Browse the repository at this point in the history
add sign and check sign
  • Loading branch information
SkyDream01 authored Jan 25, 2024
1 parent 39a1cd7 commit 5dd53e3
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 18 deletions.
4 changes: 3 additions & 1 deletion RSA.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,12 @@ def sign_message(message, private_key_bytes):


# 使用公钥验证签名
def verify_signature(message, signature, public_key_bytes):
def verify_signature(message, signature_bytes, public_key_bytes):
public_key = serialization.load_pem_public_key(
public_key_bytes,
backend=default_backend()
)
signature = eval(signature_bytes)
try:
public_key.verify(
signature,
Expand All @@ -67,6 +68,7 @@ def verify_signature(message, signature, public_key_bytes):
),
hashes.SHA256()
)
print("Signature is valid.")
return True
except Exception as e:
print("Signature is invalid:", str(e))
Expand Down
20 changes: 10 additions & 10 deletions main.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,37 @@
from config import *


def encode_message2img(private_key,public_key):
def encode_message2img(private_key, public_key):
image_name = input("请输入图片名称:")
encrypted_image = ImageProcessor("encrypted_image",image_name)
message = input("请输入要加密的信息:")
encrypted_message = encrypt_with_public_key(message, public_key)
sign = sign_message(message, private_key)
#print("加密后的信息:", encrypted_message)
zero_count, one_count = encrypted_image.image_decode()
#print(f"0的数量:{zero_count}")
#print(f"1的数量:{one_count}")
encrypted_image.image_decode()
temp_file = encrypted_image.name + "_PBE.tem"
with open(temp_file, 'r') as file:
binary_ends = file.read() # 读取文件
binary_message = encode_massage(encrypted_message)
binary_ends = overwrite_binary(binary_ends, binary_message)
binary_sign = encode_massage(sign)
binary_ends = overwrite_binary(binary_ends, binary_message, binary_sign)
with open(temp_file, 'w') as file:
file.write(binary_ends)
encrypted_image.image_encode()


def decode_img2message(private_key,public_key):
def decode_img2message(private_key, public_key):
image_name = input("请输入图片名称:")
decrypted_image = ImageProcessor("decrypted_image",image_name)
zero_count, one_count = decrypted_image.image_decode()
#print(f"0的数量:{zero_count}")
#print(f"1的数量:{one_count}")
decrypted_image.image_decode()
temp_file = decrypted_image.name + "_PBE.tem"
with open(temp_file, 'r') as file:
binary_ends = file.read() # 读取文件
binary_message =read_binary_message(binary_ends)
binary_message ,binary_sign =read_binary_message(binary_ends)
encrypted_message = decode_massage(binary_message)
sign = decode_massage(binary_sign)
decrypted_message = decrypt_with_private_key(encrypted_message, private_key)
verify_signature(decrypted_message, sign, public_key)
print(decrypted_message)


Expand Down
23 changes: 16 additions & 7 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,22 +125,27 @@ def delete_temp_files():
pass


def overwrite_binary(binary_end, binary_message):
if len(binary_end) >= (128 + len(binary_message)):
def overwrite_binary(binary_end, binary_message, binary_sign):
if len(binary_end) >= (128 + len(binary_message) +128 + len(binary_sign)):
#print(len(binary_end))
# 计算 binary_message 的长度,并将二进制数据
message_length = len(binary_message)
binary_length = bin(message_length)[2:].zfill(128)
sign_length = len(binary_sign)
binary_message_length = bin(message_length)[2:].zfill(128)
binary_sign_length = bin(sign_length)[2:].zfill(128)

binary_message_list = list(binary_message)
binary_end_list = list(binary_end)
binary_length_list = list(binary_length)
binary_message_length_list = list(binary_message_length)
binary_sign_list = list(binary_sign)
binary_sign_length_list = list(binary_sign_length)

# 在 binary_end 的前 128 字节写入长度标记
binary_end_list[:128] = binary_length_list
binary_end_list[:128] = binary_message_length_list
# 将 binary_message 覆写到 binary_end,保留原来的部分
binary_end_list[128:128+len(binary_message)] = binary_message_list
#print(len(binary_end_list))
binary_end_list[128+len(binary_message):128+len(binary_message)+128] = binary_sign_length_list
binary_end_list[128+len(binary_message)+128:128+len(binary_message)+128+len(binary_sign)] = binary_sign_list
return ''.join(binary_end_list)
else:
# 如果binary_end较小,可以选择抛出异常或执行其他操作
Expand All @@ -157,4 +162,8 @@ def read_binary_message(binary_end):
# 从binary_end的128到128+message_length字节之间读取binary_message
binary_message = binary_end[128:128 + message_length]

return binary_message
sign_length_bytes = binary_end[128 + message_length:128 + message_length+128]
sign_length = int(sign_length_bytes, 2)
sign = binary_end[128+message_length+128: 128 + message_length + 128 + sign_length]
return binary_message, sign

0 comments on commit 5dd53e3

Please sign in to comment.