【发布时间】:2015-05-16 01:58:53
【问题描述】:
我有一个包含电子邮件、用户名和密码的文本,我使用 AES 算法对其进行加密。
我在加密的时候好像没问题,但是到了解密的时候,只有一部分(拳头 48bytes = 3x 16byte 块)还可以,其余的只是一些垃圾。
我曾尝试使用 unsigned char 代替字符串,但遇到了同样的问题。
我不知道问题出在我的算法还是字符集中,但算法对我来说似乎没问题。
如果字符串包含空终止字符,会导致 string.size 出现问题吗?我不知道来自 AES 的密码是否可以包含空终止字符。
代码如下:
int aes_set_key( aes_context *ctx, uint8 *key, int nbits ); //Prototype
void aes_encrypt( aes_context *ctx, uint8 input[16], uint8 output[16] ); //Prototype
void aes_decrypt( aes_context *ctx, uint8 input[16], uint8 output[16] ); //Prototype
char Buffer[255];
memset(Buffer, 0, sizeof(Buffer));
sprintf_s(Buffer, "%s\r\n%s\r\n%s", RegisterEdits[0], RegisterEdits[1], /*PasswordHash.c_str()*/RegisterEdits[2]); // Save email, username and password to buffer
MSGBOX(Buffer);
aes_context ctx;
unsigned char key[] = "0123456789ABCDEF"; // Encrypting/Decrypting key
unsigned char outputEnc[20];
unsigned char outputDec[20];
string CipherData; // Hold whole encrypted text
string Decrypted; // Hold whole decrypted text
memset(outputEnc, 0, sizeof(outputEnc));
memset(outputDec, 0, sizeof(outputDec));
aes_set_key(&ctx, key, 256);
int Step = 0;
char Temp[18];
do
{
memset(Temp, 0, sizeof(Temp));
_snprintf(Temp, 16, &Buffer[Step]);//Take first 16 bytes from Buffer at address 0 and copy them into Temp
Step += strlen(Temp); // Append the Temp size to stepper
memset(outputEnc, 0, sizeof(outputEnc));
aes_encrypt(&ctx, reinterpret_cast<unsigned char*>(Temp), outputEnc); // encrypt 16 bytes
CipherData += reinterpret_cast<char*>(outputEnc); //append the 16 encrypted bytes to string
}
while (Step < strlen(Buffer));
MSGBOX((LPSTR)CipherData.c_str()); //Let me see the cipher (seems to be ok)
//Trying little different algorithm than "do while" for decrypting
MSGBOX("Entering");
Step = 0;
for(int i = CipherData.size(); i >= 0; i-=16) //At the start we have cipher size
{
if(i < 16) // If we have less than 16 bytes in string left...*
{
Beep(1000, 100);
memset(Temp, 0, sizeof(Temp));
memcpy(Temp, &CipherData.c_str()[Step], CipherData.size()); // *...copy only the bytes that left.
MSGBOX(Temp);
aes_decrypt(&ctx, reinterpret_cast<unsigned char*>(Temp), outputDec);
Decrypted += reinterpret_cast<char*>(outputDec);
MSGBOX((LPSTR)Decrypted.c_str());
break;
}
else
{
//if we do have more than 16 bytes left in the string...
memset(Temp, 0, sizeof(Temp));
memcpy(Temp, &CipherData.c_str()[Step], 16); // ...Copy 16 bytes again (#)
MSGBOX(Temp);
aes_decrypt(&ctx, reinterpret_cast<unsigned char*>(Temp), outputDec); //decrypt 16 bytes
Decrypted += reinterpret_cast<char*>(outputDec); //append 16 decrypted bytes
CipherData = SubstractLastn(CipherData, 16); // IMPORTANT! Remove 16 bytes from the end of the string
Step += 16; // Append decrypted size to stepper
MSGBOX((LPSTR)Decrypted.c_str());
//FIX ME! - in 3rd iteration of this loop the CipherData seems to be corrupted in the part marked with (#)
}
}
我会感谢你们的任何帮助!
【问题讨论】:
-
请在您的问题中包含您正在使用的加密库。仅供参考,调试器是针对此类问题制作的。
-
我没有使用任何库,我刚刚包含了由 Christophe Devine 用 C 语言编写的 AES 算法的源代码。如果我知道问题出在哪里,我不会发布问题。我什至不认为包括图书馆会帮助任何人解决这类问题。
-
调试器几乎肯定会有所帮助。只是为了节省一些时间,this is the algorithm implementation 您正在使用,对吗?您的问题很可能是您将 加密 数据视为字符串可行。它不是。 AES 加密的数据可以是完整 0..255 范围内的字节,第一个问题是您构建 CipherData 的方式的主要问题。我会写一个答案,但想确保我使用的是与你相同的实现。
-
抱歉回复晚了,我一直在睡觉 :P 是的,这是我正在使用的算法...我想,重新解释的演员会正确地投射它,一切都会好起来的,我是使用相同的方法通过 TCPIP 发送二进制数据。如果你能告诉我如何正确地做,你会非常感激,因为我仍然被困在这里。
标签: c++ algorithm encryption cryptography aes