Remove PKCS7 padding after decrypting with openssl (C++)

huangapple go评论78阅读模式
英文:

Remove PKCS7 padding after decrypting with openssl (C++)

问题

抱歉,代码部分不提供翻译。以下是您要翻译的文本:

Hello intellectuals,

我正在尝试使用AES 256和OpenSSL解密一个简单的文本文件,文件可以正确解密。但是,当打印出来时,填充仍然存在。库中是否有内置方法可以删除填充,以便只保留原始内容?

我加密了一行“Hello World”现在正在解密它。原始行的部分显示出来,但在末尾有一堆应该在解密后被删除的填充。

英文:

Hello intellectuals,

void aes_256_decrypt(unsigned char *ciphertext, int cipher_len, unsigned char *key, unsigned char *iv, unsigned char *plaintext) {
    AES_KEY aes_key;
    int len;
    EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
    EVP_DecryptInit_ex(ctx, EVP_aes_256_cbc(), NULL, key, iv);
    EVP_DecryptUpdate(ctx, plaintext, &len, ciphertext, cipher_len);
    EVP_DecryptFinal_ex(ctx, plaintext + len, &len);
    EVP_CIPHER_CTX_free(ctx);

}

I am trying to decrypt a simple text file with AES 256 and openssl, and the file decrypts properly. However, when printed out, the padding still remains. Is there any built in way in the library to remove the padding so just the original content remains?

I encrypted a line "Hello World" and am now decrypting it. Parts of the original line show, but towards the end there is a mess of padding that should have been removed after decryption.

答案1

得分: 1

我弄清楚了,我只是简单地忘记在明文的末尾放置空终止符。填充一直都正确地被截断。

英文:

Figured it out; I simply forgot to put a null terminator at the end of the plaintext. The padding was cut off correctly all along.

huangapple
  • 本文由 发表于 2023年6月15日 17:10:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/76480922.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定