RSA加密的字符串过长导致报错:Traceback (most recent call last):
File "D:\xx\xx\xx\main.py", line 96, in <module>
encrypted_message = message_p.encrypt_file(message)
File "D:\xx\xx\xx\\decrypt_class.py", line 30, in encrypt_file
encrypted_file = self.public_key.encrypt(
File "C:\Users\xx\AppData\Local\Programs\Python\Python39\lib\site-packages\cryptography\hazmat\backends\openssl\rsa.py", line 537, in encrypt
return _enc_dec_rsa(self._backend, self, plaintext, padding)
File "C:\Users\xx\AppData\Local\Programs\Python\Python39\lib\site-packages\cryptography\hazmat\backends\openssl\rsa.py", line 87, in _enc_dec_rsa
return _enc_dec_rsa_pkey_ctx(backend, key, data, padding_enum, padding)
File "C:\Users\xx\AppData\Local\Programs\Python\Python39\lib\site-packages\cryptography\hazmat\backends\openssl\rsa.py", line 151, in _enc_dec_rsa_pkey_ctx
raise ValueError("Encryption/decryption failed.")
ValueError: Encryption/decryption failed.
解决方法:1、更换加密方式;2、减少加密字符长度。
在使用RSA加密时遇到“Encryption/decryption failed.”这样的错误,通常是因为尝试加密的数据块超过了RSA密钥的最大加密长度。RSA加密通常不直接用于加密大量数据,因为加密效率较低且数据大小有限制。RSA通常用于加密小量的关键数据(如对称加密的密钥),而大量数据的加密则通过对称加密算法(如AES)进行。