1
nongmei 2018-09-29 10:05:27 +08:00 1
感觉你俩的明文补全方式不一样,php 是手动补的"\0",python 是 pkcs1 padding
|
2
334862132 OP 这个是我的初始加密方式 我用\0 在加密对象左侧填充到 245 长度加密出来的结果第三方也解密不了 加密到 256 位的话 RSA 长度不行 至于 RSA 切割加密感觉更不行了, 长度估计都跟对面给出来的长度不一样,RSA 公钥加密最恶心的是每次加密出来的结果都不一样, 除了私钥根本没法验证 更恶心的是我还没有私钥
|
3
FanWall 2018-09-29 10:32:29 +08:00 1
搜索: python rsa no padding
还有, no padding 每次的结果应当是一样的 |
4
334862132 OP @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 ) |