rsa加密解密文件分开

Separate files for rsa encryption and decryption

所以我正在尝试制作两个程序,一个使用 rsa 加密对消息进行编码,另一个对消息进行解码。在我的加密文件中:

import rsa


def generateKeys():
    (publicKey, privateKey) = rsa.newkeys(1024)
    with open('keys/publicKey.pem', 'wb') as p:
        p.write(publicKey.save_pkcs1('PEM'))
    with open('keys/privateKey.pem', 'wb') as p:
        p.write(privateKey.save_pkcs1('PEM'))


def loadKeys():
    with open('keys/publicKey.pem', 'rb') as p:
        publicKey = rsa.PublicKey.load_pkcs1(p.read())
    with open('keys/privateKey.pem', 'rb') as p:
        privateKey = rsa.PrivateKey.load_pkcs1(p.read())
    return privateKey, publicKey


def encrypt(message, key):
    return rsa.encrypt(message.encode('ascii'), key)


def sign(message, key):
    return rsa.sign(message.encode('ascii'), key, 'SHA-1')


#generateKeys()
privateKey, publicKey = loadKeys()
print(f"public key: {publicKey}, private key: {privateKey}")
encryptme = input('Write your message here:')
ciphertext = encrypt(encryptme, publicKey)
signature = sign(encryptme, privateKey)

print(str(ciphertext))
#print(signature)

在解密文件中我有:

import rsa


def loadKeys():
    with open('keys/publicKey.pem', 'rb') as p:
        publicKey = rsa.PublicKey.load_pkcs1(p.read())
    with open('keys/privateKey.pem', 'rb') as p:
        privateKey = rsa.PrivateKey.load_pkcs1(p.read())
    return privateKey, publicKey


def decrypt(ciphertext, key):
    try:
        print(ciphertext)
        return rsa.decrypt(ciphertext, key).decode('ascii')
    except:
        return False


def verify(message, signature, key):
    try:
        return rsa.verify(message, signature, key, ) == 'SHA-1'
    except:
        return False


privateKey, publicKey = loadKeys()

ciphertext = input("message to decipher: ")
print(ciphertext)
text = decrypt(ciphertext, privateKey)

if text:
    print(f'Message text: {text}')
else:
    print(f'Unable to decrypt the message.')

每当我对文本进行编码然后将其粘贴到解密程序的输入中时,它returns 无法解码消息。如果有人知道这是为什么,我会喜欢一些帮助。谢谢!

字节串ciphertextstr()或最迟input()转换为字符串。

示例:2 字节字符串 b'\x11\xed'str()input() 转换为 11 字节字符串,如 UTF8 编码显示:b"b'\x11\xed'".

这个字符串在解密之前必须被转换回原始字节串,这在发布的实现中不会发生(或不正确地发生)(也就是评论)。

如果密文是Base64编码,问题就很容易解决。然后可以通过 copy/paste 将此字符串传递给 input(),并在解密前对其进行 Base64 解码。如果需要存储密文,由于Base64开销,建议存储原始密文。

以下代码使用Base64实现加解密encoding/decoding:

import rsa
import base64

def generateKeys():
    return rsa.newkeys(1024)

def encrypt(message, key):
    return rsa.encrypt(message.encode('ascii'), key)

def decrypt(ciphertext, key):
    try:
        return rsa.decrypt(ciphertext, key).decode('ascii')
    except:
        return False

(publicKey, privateKey) = generateKeys()
encryptme = 'The quick brown fox jumps over the lazy dog'

# Encryption
ciphertext = encrypt(encryptme, publicKey) # store raw ciphertext in file system 
ciphertextB64 = base64.b64encode(ciphertext).decode('utf8')
print(ciphertextB64)
ciphertextB64Input = input("message to decipher: ") # Enter the ciphertext with copy/paste
decrypted = decrypt(base64.b64decode(ciphertextB64Input), privateKey)
print(decrypted)

签名也一样:

def sign(message, key):
    return rsa.sign(message.encode('ascii'), key, 'SHA-1')

def verify(message, signature, key):
    try:
        return rsa.verify(message.encode('ascii'), signature, key) == 'SHA-1'
    except:
        return False

# Signing
signme = 'The quick brown fox jumps over the lazy dog'
signature = sign(signme, privateKey) # store raw signature in file system
signatureB64 = base64.b64encode(signature).decode('utf8')
print(signatureB64)
signatureB64Input = input("signature to verify: ")
verified = verify(signme, base64.b64decode(signatureB64Input), publicKey)
print(verified)

注意这里还有一个bug:在rsa.verify()message必须换成message.encode('ascii')