【问题标题】:Why just accept me the first 2 characters in the key? C ++ [closed]为什么只接受我密钥中的前 2 个字符? C++ [关闭]
【发布时间】:2013-01-16 12:19:37
【问题描述】:

我在 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

任何建议或想法,谢谢

【问题讨论】:

  • 为什么i &lt; argc 是条件的一部分?
  • 我不知道它对我有帮助,以前 argv [2] 有 argv [i] 但我改变了,因为我被冻结了
  • 您的代码看起来像 C。使用 C++ 的原因之一是不必考虑这类问题。
  • @aleksanderhaugas 您缺少基础知识。先从good book 开始怎么样?或者至少告诉我们(用简单的话)你想要达到的目标。
  • 但是如果我把 `for(register int i = 0; i!=size;++i) ` 我感觉一样

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


【解决方案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);
//******************************************************

【讨论】:

  • 我用的是literatecode aes256的类,就是用hex key解密文件
  • 我也有同感,不知道怎么回事
  • @aleksanderhaugas:它解决了你的问题吗?
  • 不,和以前一样。但感谢您的帮助
猜你喜欢
  • 2016-03-10
  • 1970-01-01
  • 1970-01-01
  • 2013-12-01
  • 1970-01-01
  • 2014-02-18
  • 1970-01-01
  • 2017-01-02
  • 1970-01-01
相关资源
最近更新 更多