繁体   English   中英

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.')

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

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

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

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

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

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

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)

请注意,这里还有另一个错误:在rsa.verify()中, message必须替换为message.encode('ascii')

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM