1

我需要将 AES 加密功能添加到我的 C++ 项目中。到目前为止,我正在尝试为代码项目编写一个包装类:AES++ 的 C++ 实现,它具有以下功能:

  • char* Encrypt(string& InputData); //takes plain text and returns encrypted data
  • char* Decrypt(string& InputData); //takes encrypted data and returns plain text

但是当我测试我的代码时,只有前 32 个字节的数据被加密和解密。

输出的最后一行是:-

这是加密测试我的名字

是什么让它错过了字符串的其余部分?我错过了什么?

#include "Rijndael.h"
#include <iostream>
#include <string>

using namespace std;

string LenMod(string Input);

char* Encrypt(string& InputData)
{
    try
    {
        InputData = LenMod(InputData);
        char* OutputData = (char*)malloc(InputData.size() + 1); 
        memset(OutputData, 0, sizeof(OutputData));

        CRijndael AESEncrypter;

        AESEncrypter.MakeKey("HiLCoE School of Computer Science and Technology",CRijndael::sm_chain0, 16 , 16);

        AESEncrypter.Encrypt(InputData.c_str(), OutputData, sizeof(InputData), CRijndael::ECB);

        return OutputData;
    }
    catch(exception e)
    {
        cout<<e.what();
        return NULL;
    }
}

char* Decrypt(string& Input)
{
    try
    {
        Input = LenMod(Input);
        char* Output = (char*)malloc(Input.size() + 1); 
        memset(Output, 0, sizeof(Output));

        CRijndael AESDecrypter;

        AESDecrypter.MakeKey("HiLCoE School of Computer Science and Technology",CRijndael::sm_chain0, 16, 16);

        AESDecrypter.Decrypt(Input.c_str(), Output, sizeof(Input), CRijndael::ECB); 

        return Output;
    }
    catch(exception e)
    {
        cout<<e.what();
        return NULL;
    }
}

string LenMod(string Input)
{
    while(Input.length() % 16 != 0)
        Input += '\0';

    return Input;
}

int main()
{

    string s = "This is Encryption Test My name is yohannes tamru i am a computer science student";

    //cout<<LengthMod(s)<<endl;
    string temp1(Encrypt(s));
    string temp2(Decrypt(temp1));

    cout<<temp1<<endl<<endl<<temp2<<endl;

    system("pause");
return 0;
}
4

0 回答 0