from Crypto.PublicKey import RSA
from Crypto.Cipher import PKCS1_v1_5
from argparse import ArgumentParser
import time
parser = ArgumentParser(description="这是RSA解密程序.")
parser.add_argument("-c", "--cipher", type=str, default=r"./cipher1.txt", help="密文路径")
parser.add_argument("--private", type=str, default=r"./private.pem", help="解密密钥路径")
parser.add_argument("--cerlen", type=int, default=1024, help="证书key位数")
args = parser.parse_args()
with open(args.cipher, "rb") as f:
Cipher_text = f.read()
"""
单次解密长度
1024bit的证书用128,2048bit证书用256位
"""
rsakey = RSA.importKey(open(args.private).read())
cipher = PKCS1_v1_5.new(rsakey)
Plaintext_ = []
length = int(args.cerlen / 8)
group = len(Cipher_text) // length + 1
process_bar_len = 20
start = time.perf_counter()
for i in range(0, len(Cipher_text), length):
Plaintext_.append(cipher.decrypt(Cipher_text[i:i + length], "解密失败"))
# print("一共",group,"段,第",i//length,"段完成")
dur = time.perf_counter() - start
count = i // length + 2
finished = count / group # 完成的百分比
print("\rdecryption progress:", "{}/{}={:^3.0f}% ◎{:.3f}s".format(count, group, finished * 100, dur),
"★" * (int(process_bar_len * finished)) + "☆" * (int(process_bar_len * (1 - finished))), end="")
Plaintext = b""
for item in Plaintext_:
Plaintext += item
with open("testjiemi.txt", "wb") as f:
f.write(Plaintext)
print("\n" + args.cipher + " ■ Decryption Done...")
这是解密过程,两个文件,一个能解密成功,一个就显示下面这个错误
File "D:/pycharm2020/jiami/RSA_guowenbo/testRSA1jiemi.py", line 38, in
Plaintext += item
TypeError: can't concat str to bytes