PGPY解锁私钥报错NotImplementedError: <SymmetricKeyAlgorithm.Plaintext: 0>求助
解决PGPY解锁私钥时的NotImplementedError(SymmetricKeyAlgorithm.Plaintext:0)
问题重现
尝试用PGPY加载私钥并解密消息,私钥和加密消息均能正常加载,但调用unlock()方法时触发错误:
加载私钥代码:
Private_key_path = r"/content/Private_KEY.sub" privkey, _ = pgpy.PGPKey.from_file(Private_key_path)
加载加密消息代码:
file_path=r"/content/we.csv" message_from_file = pgpy.PGPMessage.from_file(file_path) print(message_from_file)
解锁私钥并解密时的代码:
with privkey.unlock('correctpassphrase') as key: raw_message = key.decrypt(message_from_file).message print(raw_message)
触发的错误栈:
NotImplementedError Traceback (most recent call last) <ipython-input-139-f188768ef141> in <module> ----> 1 with privkey.unlock(PASSPHRASE) as key: 2 raw_message = key.decrypt(message_from_file).message 3 print(raw_message) 6 frames /usr/lib/python3.8/contextlib.py in __enter__(self) 111 del self.args, self.kwds, self.func 112 try: --> 113 return next(self.gen) 114 except StopIteration: 115 raise RuntimeError("generator didn't yield") from None /usr/local/lib/python3.8/dist-packages/pgpy/pgp.py in unlock(self, passphrase) 1809 try: 1810 for sk in itertools.chain([self], self.subkeys.values()): --> 1811 sk._key.unprotect(passphrase) 1812 del passphrase 1813 yield self /usr/local/lib/python3.8/dist-packages/pgpy/packet/packets.py in unprotect(self, passphrase) 939 940 def unprotect(self, passphrase): --> 941 self.keymaterial.decrypt_keyblob(passphrase) 942 del passphrase 943 /usr/local/lib/python3.8/dist-packages/pgpy/packet/fields.py in decrypt_keyblob(self, passphrase) 1351 1352 def decrypt_keyblob(self, passphrase): --> 1353 kb = super(RSAPriv, self).decrypt_keyblob(passphrase) 1354 del passphrase 1355 /usr/local/lib/python3.8/dist-packages/pgpy/packet/fields.py in decrypt_keyblob(self, passphrase) 1252 1253 # derive the session key from our passphrase, and then unreference passphrase --> 1254 sessionkey = self.s2k.derive_key(passphrase) 1255 del passphrase 1256 /usr/local/lib/python3.8/dist-packages/pgpy/packet/fields.py in derive_key(self, passphrase) 1017 def derive_key(self, passphrase): 1018 ##TODO: raise an exception if self.usage is not 254 or 255 --> 1019 keylen = self.encalg.key_size 1020 hashlen = self.halg.digest_size * 8 1021 /usr/local/lib/python3.8/dist-packages/pgpy/constants.py in key_size(self) 237 return ks[self] 238 --> 239 raise NotImplementedError(repr(self)) 240 241 def gen_iv(self): NotImplementedError: <SymmetricKeyAlgorithm.Plaintext: 0>
错误原因
这个错误的核心是:你的私钥是未加密的明文私钥,但PGPY的unlock()方法是为加密私钥设计的。当私钥未加密时,其密钥算法标记为Plaintext(0),PGPY尝试推导解密密钥时,该算法没有对应的密钥长度实现,从而抛出NotImplementedError。
可以通过查看私钥文件内容验证:如果私钥块中没有Proc-Type: 4,ENCRYPTED这一行,说明私钥确实是明文存储的。
解决方案
方案1:直接使用明文私钥解密(无需unlock)
既然私钥未加密,跳过unlock()步骤,直接调用解密方法即可:
Private_key_path = r"/content/Private_KEY.sub" privkey, _ = pgpy.PGPKey.from_file(Private_key_path) file_path=r"/content/we.csv" message_from_file = pgpy.PGPMessage.from_file(file_path) # 明文私钥直接解密,不需要unlock raw_message = privkey.decrypt(message_from_file).message print(raw_message)
方案2:将明文私钥加密后再使用(安全推荐)
出于安全考虑,建议给私钥设置密码,步骤如下:
- 用GPG导出明文私钥:
gpg --export-secret-keys YOUR_KEY_ID > plain_privkey.asc
- 导入明文私钥并设置密码:
gpg --import plain_privkey.asc gpg --edit-key YOUR_KEY_ID passwd
按照提示输入新密码完成加密。
3. 导出加密后的私钥:
gpg --export-secret-keys YOUR_KEY_ID > encrypted_privkey.asc
之后用这个加密后的私钥,就可以正常使用privkey.unlock('your_passphrase')进行解密了。
验证
运行修改后的代码,确认加密消息能被正常解密输出。
内容的提问来源于stack exchange,提问作者9879ypxkj
相关产品推荐
相关产品推荐

