RSA与RSA解密AES密钥偶尔会出现无效的密钥长度错误

发布于 2025-01-20 16:41:58 字数 1981 浏览 3 评论 0 原文

我们通过Java中的RSA方法在客户端(又称Android手机)上对AES密钥进行加密。

class Encryptor {
    private static String RSA_ALGORITHM = "RSA/ECB/PKCS1Padding";

    public Init() {
        this.rsaEncCipher = Cipher.gtInstance(RSA_ALGORITHM);
        this.rsaEncCipher.init(Cipher.ENCRYPT_MODE, public_key)
    }

    public byte[] rsaPublicEncrypt(byte[] content) {
        try {
            return this.rsaEncCipher.doFinal(content);
        } catch(Exception a) {
            //
        }
    }
}


JsonObject json = new JsonObject();

byte[] key = new byte[16];
random.nextBytes(key);

byte[] encryptedContent = encryptor.aesEncrypt(key, content.getBytes())
byte[] encryptedKey = encryptor.rsaPublicEncrypt(key);

json.addProperty("key", Base64.encodeToString(encryptedKey, Base64.DEFAULT));
json.addProperty("content", Base64.encodeToString(encryptedContent, Base64.DEFAULT));

并在服务器端的node.js中使用rsa解密AES密钥,

function  decryptAes(key, cipherContent) {
    var decipher = crypto.createDecipheriv("AES-128-CBC", key, "AndroidAESEncrypt");
    let plainText = decipher.update(cipherContent, 'base64', 'utf8');
    return plainText += decipher.final('utf8');
}

function descrypt(key, content) {
    const contentBuffer = buffer.from(key, 'base64');
    const keyBuffer = Buffer.from(key, 'base64');
    const aesKey = crypto.privateDecrypt({
                    key: privateKey,
                    padding: crypto.constants.RSA_PKCS1_PADDING
                }, keyBuffer);;

    console.log(aesKey.length)

    const rawContent = decryptAes(aesKey, contentBuffer).toString('base64')
}

大多数时候,加密/解密机制效果很好, aeskey 的长度是16。有时,我们遇到了错误无效的密钥长度来自AES解密, aeskey 的长度为32。我们尝试使用 contentbuffer 使用 aeskey的第一个字节解密 contentbuffer 其长度为32,函数 degptaes 效果很好。太奇怪了。

有没有人遇到过同样奇怪的问题?

We encrypt the AES key through RSA method in Java on the client-side, aka android mobile phone.

class Encryptor {
    private static String RSA_ALGORITHM = "RSA/ECB/PKCS1Padding";

    public Init() {
        this.rsaEncCipher = Cipher.gtInstance(RSA_ALGORITHM);
        this.rsaEncCipher.init(Cipher.ENCRYPT_MODE, public_key)
    }

    public byte[] rsaPublicEncrypt(byte[] content) {
        try {
            return this.rsaEncCipher.doFinal(content);
        } catch(Exception a) {
            //
        }
    }
}


JsonObject json = new JsonObject();

byte[] key = new byte[16];
random.nextBytes(key);

byte[] encryptedContent = encryptor.aesEncrypt(key, content.getBytes())
byte[] encryptedKey = encryptor.rsaPublicEncrypt(key);

json.addProperty("key", Base64.encodeToString(encryptedKey, Base64.DEFAULT));
json.addProperty("content", Base64.encodeToString(encryptedContent, Base64.DEFAULT));

And decrypted the AES key with RSA in Node.js on the server side

function  decryptAes(key, cipherContent) {
    var decipher = crypto.createDecipheriv("AES-128-CBC", key, "AndroidAESEncrypt");
    let plainText = decipher.update(cipherContent, 'base64', 'utf8');
    return plainText += decipher.final('utf8');
}

function descrypt(key, content) {
    const contentBuffer = buffer.from(key, 'base64');
    const keyBuffer = Buffer.from(key, 'base64');
    const aesKey = crypto.privateDecrypt({
                    key: privateKey,
                    padding: crypto.constants.RSA_PKCS1_PADDING
                }, keyBuffer);;

    console.log(aesKey.length)

    const rawContent = decryptAes(aesKey, contentBuffer).toString('base64')
}

Most of the time, the encrypt/decrypt mechanism works well and the length of aesKey is 16. Occasionally, we met the error invalid key length from AES decrypt, and the length of aesKey is 32. We try to decrypt the contentBuffer with the first 16 bytes of aesKey whose length is 32, the function decryptAes works well. It is so weird.

Is there anyone who met the same weird issue before?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文