与RSA的密码AES密钥

发布于 2025-02-01 10:46:59 字数 1074 浏览 2 评论 0原文

我有2个文件,一个文件具有RSA公钥,另一个是AES密钥。我需要用RSA将AES密钥密码。

publicKey.pem

-----开始公共密钥-----键的其余部分-----结束公共密钥------

键。PEM(包含AES密钥)

“ llave”: “ b'\ xaa \ xbd [\ x91 \ x82 | \ xc6r \ x05 \ xa1 \ xc2 \ xc2 \ x13 \ xca \ xca \ xb14u', “ iv”:“ pvgattfskrwwfkorzdpzxw ==”

我得到了包含AES密钥(llave)的文件的一部分,然后用RSA对其进行了密封。唯一的问题是密钥长度为256个字节。

import json
from Crypto.Cipher import PKCS1_OAEP
from Crypto.PublicKey import RSA
from Crypto.Cipher import AES
def cipher_keyaes():
    with open('publickey.pem', 'rb') as f:
        with open('key.pem', 'rb') as f1:
            data = f.read().decode('utf-8')

            file = json.load(f1)
            print(file)
            key = file['llave']
            key_rsa = RSA.import_key(data)
            cipher = PKCS1_OAEP.new(key_rsa)

            cipher_key = cipher.encrypt(key.encode('utf8'))
            print(cipher_key)
            key = AES.new(cipher_key, AES.MODE_CBC)

            f.close()

有没有办法仅获得16个字节长度的cipher_key?

I have 2 files, one has the RSA public key, and the other one is the AES key. I need to cipher the AES key with RSA.

publickey.pem

-----BEGIN PUBLIC KEY----- rest of key -----END PUBLIC KEY-----

key.pem (contains the AES key)

"llave":
"b'\xaa\xbd[\x91\x82|\xc6r\x05\xa1\xc2\x13\xca\xb14U'",
"iv": "pvGatTFSKrWwfKOrZdPzXw=="

I got the part of the file that contains the AES key (llave) and then I ciphered it with RSA. The only problem is that the ciphered key length is 256 bytes.

import json
from Crypto.Cipher import PKCS1_OAEP
from Crypto.PublicKey import RSA
from Crypto.Cipher import AES
def cipher_keyaes():
    with open('publickey.pem', 'rb') as f:
        with open('key.pem', 'rb') as f1:
            data = f.read().decode('utf-8')

            file = json.load(f1)
            print(file)
            key = file['llave']
            key_rsa = RSA.import_key(data)
            cipher = PKCS1_OAEP.new(key_rsa)

            cipher_key = cipher.encrypt(key.encode('utf8'))
            print(cipher_key)
            key = AES.new(cipher_key, AES.MODE_CBC)

            f.close()

Is there a way to just get the cipher_key of 16 bytes length?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏花。依旧 2025-02-08 10:46:59

不,使用不对称加密术几乎是不可能的。

关于最好的选择是ECC公钥的32个字节,然后您可以将其用于IES加密,这基本上是DH密钥协议,然后是一个键推导机制来推导AES密钥。

对于AES,模块化凸起的结果大约是RSA内使用的模量的大小(并且,对于标准化模式,通常是模量的大小,直至下一个字节边界) 。因此,我可以从包装的AES密钥大小为256个字节中看到,您使用的RSA模量为8 x 256 = 2048位(并且,由于模量定义了密钥大小,密钥大小也为2048)。


我们使用混合密码学的原因之一,即不对称的加密(例如RSA +对称加密),不对称的密码学添加了重要的数据开销。对于较小的消息或单个AES键,这个开销通常不是一个问题,但是对于较大的消息,它很快就会成为一个问题。

No, a ciphertext size of 16 bytes is next to impossible using asymmetric cryptography.

About the best you can do is about 32 bytes for an ECC public key, which you can then use for IES encryption, which is basically DH key agreement, followed by a key derivation mechanism to derive an AES key.

For AES, the result of the modular exponentiation is about the size of the modulus used within RSA (and, for standardized modes, it is usually exactly the size of the modulus, up to the next byte boundary). Hence I can see from the wrapped AES key size of 256 bytes that you are using an RSA modulus of 8 x 256 = 2048 bits (and, as the modulus defines the key size, a key size of 2048 as well).


One of the reasons why we use hybrid cryptography, i.e. asymmetric cryptography such as RSA + symmetric cryptography is that asymmetric cryptography adds significant data overhead. This overhead is usually not such a problem for smaller messages or indeed single AES keys, but for larger messages it quickly does become an issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文