python – AES会话密钥的RSA解密失败,出现’AttributeError:’bytes’对象没有属性’n’
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了python – AES会话密钥的RSA解密失败,出现’AttributeError:’bytes’对象没有属性’n’,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2597字,纯文字阅读大概需要4分钟。
内容图文
![python – AES会话密钥的RSA解密失败,出现’AttributeError:’bytes’对象没有属性’n’](/upload/InfoBanner/zyjiaocheng/820/7ff60884cbce40759ce0aecfd94e012b.jpg)
我正在使用Python 3.6上的PyCryptodome实现公钥加密.当我尝试创建对称加密密钥并加密/解密变量时,一切正常.但是,在我介绍RSA(和PKCS1_OAEP)的那一刻,这一切都在下Go – session_key加密很好但是当我尝试解密时,我收到以下错误:
Traceback (most recent call last):
File "enctest.py", line 109, in <module>
deckey = decrypt_val(enckey)
File "enctest.py", line 77, in decrypt_val
session_key = cipher.decrypt(ciphertext)
File "/usr/lib/python3.6/site-packages/Crypto/Cipher/PKCS1_OAEP.py", line 187, in decrypt
modBits = Crypto.Util.number.size(self._key.n)
AttributeError: 'bytes' object has no attribute 'n'
我的代码如下.任何人都可以看看,告诉我我做错了什么?
from Crypto.Cipher import AES, PKCS1_OAEP
from Crypto.PublicKey import RSA
from Crypto import Random
from Crypto.Random import get_random_bytes
random_generator = Random.new().read
keys = RSA.generate(1024, random_generator)
pubkey = keys.publickey()
privkey = keys.exportKey()
pubcipher = PKCS1_OAEP.new(pubkey) # ciphertext = cipher.encrypt(message)
privcipher = PKCS1_OAEP.new(privkey) # message = cipher.decrypt(ciphertext)
privkeystr = keys.exportKey(format='PEM', passphrase=None, pkcs=1)
pubkeystr = keys.publickey().exportKey(format='PEM', passphrase=None, pkcs=1)
def encrypt_val(session_key, cipher = pubcipher):
try:
session_key = session_key.encode('utf8')
except:
pass
ciphertext = cipher.encrypt(session_key)
print("encrypted key : %s \n" % ciphertext)
return ciphertext
def decrypt_val(ciphertext, cipher = privcipher):
session_key = cipher.decrypt(ciphertext)
try:
session_key = session_key.decode('utf8')
except:
pass
return session_key
def aesenc(data):
try:
data = data.encode('utf8')
except:
pass
key = get_random_bytes(16)
cipher = AES.new(key, AES.MODE_EAX)
ciphertext, tag = cipher.encrypt_and_digest(data)
aesencdict = {'aesdict' : {'ciphertext' : ciphertext, 'tag' : tag, 'nonce' : cipher.nonce} , 'key' : key}
return(aesencdict)
def aesdec(aesdict, key):
cipher = AES.new(key, AES.MODE_EAX, aesdict['nonce'])
data = cipher.decrypt_and_verify(aesdict['ciphertext'], aesdict['tag'])
try:
data = data.decode('utf8')
except:
pass
return data
val = "hello"
encval = aesenc(val)
enckey = encrypt_val(encval['key'])
print(enckey)
deckey = decrypt_val(enckey)
print(deckey)
if deckey == encval['key']:
outval = aesdec(encval['aesdict'], encval['key'])
print(val, outval)
else:
print("oops\n")
解决方法:
看起来你做了一个虚假的导出,它将一个键转换为一个键的编码:
privkey = keys.exportKey()
....
privcipher = PKCS1_OAEP.new(privkey) # message = cipher.decrypt(ciphertext)
之后,它尝试从编码密钥中找到模数n,而不是从包含成员n的对象实例中找到模数n.
尝试:
privcipher = PKCS1_OAEP.new(keys)
代替.
内容总结
以上是互联网集市为您收集整理的python – AES会话密钥的RSA解密失败,出现’AttributeError:’bytes’对象没有属性’n’全部内容,希望文章能够帮你解决python – AES会话密钥的RSA解密失败,出现’AttributeError:’bytes’对象没有属性’n’所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。