@
FanWall 求一份 Python3 的无填充代码 网上找的代码一运行就报错
NotImplementedError: Use module Crypto.Cipher.PKCS1_OAEP instead
我用的这段代码 ,引用位置和引用文件都已经改过了
author__ = 'owen'
__date__ = '2017-11-22'
import base64
from Crypto.PublicKey import RSA
ENCRYPT_SALT = b'12345678901234567890123456789012345679801234' # 44 char
RSA_KEY_PATH = './'
class MyRSACrypto:
@
classmethod def cryptor( cls, plain_text ):
# print("\n================ crypto ========================\n")
if( not isinstance( plain_text, bytes ) ):
plain_text = plain_text.encode()
salt = ENCRYPT_SALT
base_dir = RSA_KEY_PATH
with open(base_dir + 'master-public.pem') as fp:
public_key = fp.read()
if(not public_key):
return None
rsa_cryptor = RSA.importKey( public_key )
plain_text = ( plain_text + salt )
# 无填充方式公钥加密
cipher_text = rsa_cryptor.encrypt( plain_text, 0 )
pad_cnt = 64 - len(cipher_text[0])
cipher_text_rsa = pad_cnt * b'\0' + cipher_text[0]
cipher_text_b64 = base64.b64encode( cipher_text_rsa )
return cipher_text_b64.decode()[:-2]
@
classmethod def decryptor( cls, cipher_text_b64 ):
# print("\n================ decrypto ========================\n")
if( not isinstance( cipher_text_b64, bytes ) ):
cipher_text_b64 = cipher_text_b64.encode()
base_dir = RSA_KEY_PATH
with open( base_dir + 'master-private.pem' ) as fp:
private_key = fp.read()
if(not private_key):
return None
rsa_decryptor = RSA.importKey( private_key )
cipher_text = base64.b64decode( cipher_text_b64 + b"==" )
# 无填充方式私钥解密
plain_text = rsa_decryptor.decrypt( cipher_text )
return plain_text.decode()[:20]
if __name__ == '__main__':
text = '31' * 10
cipher_text = MyRSACrypto.cryptor( text )
print(cipher_text)
plain_text = MyRSACrypto.decryptor( cipher_text )
print( plain_text )