AES解密仅适用于前16个字符

时间:2015-04-30 00:31:09

标签: c++ encryption openssl aes

我正在尝试使用c ++为我的计算机科学课程进行简单的AES加密/解密。它几乎完美地工作,但解密只适用于字符数组的前16个字符。

这是我的代码:

#include <iostream>
#include "openssl/aes.h"

using namespace std;

int main() {
    unsigned char teext[] = "The quick brown fox jumped over the lazy dog";

    /*created keys for en- and de-cryption*/
    unsigned char key[] = "abcdefghijklmnop";
    unsigned char iv[] = "abcdefghijklmnop";

    /*char array for encryption and decryption output*/
    unsigned char enc_out[80];
    unsigned char dec_out[80];

    /*creating key variables*/
    AES_KEY enc_key, dec_key;

    /*encryption process*/
    AES_set_encrypt_key(key, 128, &enc_key);
    AES_encrypt(teext, enc_out, &enc_key);

    /*decryption process*/
    AES_set_decrypt_key(iv, 128, &dec_key);
    AES_decrypt(enc_out, dec_out, &dec_key);

    cout << "AES encryption"
         << "Encrypted: " << enc_out << endl 
         << "Decrypted: " << dec_out << endl;

    return 0;
}

我正在编译:

 g++ Crypto.cc -L/usr/lib -lssl -lcrypto -o crypto

程序将加密“快速的棕色狐狸跳过懒狗”就好了。但解密时会输出“快速褐色”和t 母鸡随机字符。知道我做错了吗?

1 个答案:

答案 0 :(得分:3)

AES_decrypt只解密一个16字节的AES数据块。我相信这是一个比你想要的更低级别的功能。

您最好使用EVP_*级功能。你可以找到一个非常完整的例子来说明如何在openssl wiki上使用它们 - 它甚至使用AES ecryption:https://wiki.openssl.org/index.php/EVP_Symmetric_Encryption_and_Decryption

相关问题