为什么只接受我键中的前2个字符? C ++

时间:2013-01-16 12:19:38

标签: c++ visual-studio-2010 visual-c++

在Visual Studio 2010专业版的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

任何建议或想法,谢谢

1 个答案:

答案 0 :(得分:1)

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

//****************** 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);
//******************************************************