为什么只接受我密钥的前两个字符?c++

Why just accept me the first 2 characters in the key? C ++

本文关键字:两个 字符 c++ 密钥 为什么      更新时间:2023-10-16

我在visual studio 2010 professional的c++项目中插入密钥时遇到了一个小问题。

当我放入密钥时,只接受前两个密钥,当您放入以前两个字符开头的类似密钥时,这可能会出现问题。

但是,当我直接将密钥放入十六进制字符中时,将验证所有。

我事先明确我所知甚少,我正在学习c++这就是我现在所做的。

    //****************** AES decryption ********************
const int size = 32;
unsigned char aesKey[size];
char* p;
for (int i = 1; i < argc || i < size; ++i)
{
    aesKey[i] = (unsigned char)strtol(argv[2], &p, 16);
} 
unsigned char *buf;
aes256_context ctx;
aes256_init(&ctx, aesKey);
for (unsigned long i = 0; i < lSize/16; i++) {
    buf = text + (i * 16);
    aes256_decrypt_ecb(&ctx, buf);
}
aes256_done(&ctx);
//******************************************************

这里有参数argv[2]因为我必须使用参数2

有任何建议或想法,谢谢

这段代码可以有很多修复,但这是基本的我可以看到

//****************** AES decryption ********************
const int size = 32;
unsigned char aesKey[size];
char* p;
//check you have argv[2]
if (argc < 3)
{
    //TODO: return or handle the error as you wish...
}
//i need to start from 0 (it's a zero base index)
//argc = argument count. and this should not be here
//you have 3 arguments and this is why it read 2 chars...
for (int i = 0;i < size; ++i)
{
    aesKey[i] = (unsigned char)strtol(argv[2], &p, 16);
} 
unsigned char *buf;
aes256_context ctx;
aes256_init(&ctx, aesKey);
//I don't know where lsize is coming from but I would calculate the division out side:
unsigned long myMax = lSize/16;
for (unsigned long i = 0; i < myMax; i++) {
    buf = text + (i * 16);
    aes256_decrypt_ecb(&ctx, buf);
}
aes256_done(&ctx);
//******************************************************