首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >AES128与CryptoAPI

AES128与CryptoAPI
EN

Stack Overflow用户
提问于 2018-07-11 20:03:15
回答 2查看 176关注 0票数 2

我试图使用AES128使用CryptoAPI加密字符串,但由于某些原因,我没有得到它。当我编译时,它只会使应用程序崩溃。我认为问题在于CryptDecryptCryptEncrypt函数的调用,因为我是根据我在互联网上找到的另一个函数编写这个函数的。我对它一无所知,也不知道如何使用它。

这是我的代码:

代码语言:javascript
复制
#include <Windows.h>
#include <wincrypt.h>
#include <stdio.h>
#include <cmath>
#include <string>

#pragma comment(lib, "crypt32.lib")

#define BLOCK_LEN 16

std::string AES128(std::string key, std::string data, bool enc)
{
    bool Result = false;
    size_t blocks = ceil((float)data.length() / BLOCK_LEN) + 1;
    BYTE* chunk = new BYTE[blocks * BLOCK_LEN];
    memset(chunk, 0, blocks * BLOCK_LEN);
    memcpy(chunk, data.c_str(), data.length());

    HCRYPTPROV hProv;
    if (!CryptAcquireContextA(&hProv, NULL, NULL, PROV_RSA_AES, CRYPT_VERIFYCONTEXT))
        goto finally;

    HCRYPTHASH hHash;
    if (!CryptCreateHash(hProv, CALG_SHA_256, 0, 0, &hHash))
        goto finally;

    if (!CryptHashData(hHash, (BYTE*)key.c_str(), key.length(), 0))
        goto finally;

    HCRYPTKEY hKey;
    if (!CryptDeriveKey(hProv, CALG_AES_128, hHash, 0, &hKey))
        goto finally;

    for (int i = 0; i < blocks; i++)
        switch (enc)
        {
        case true:
        {
            DWORD out_len = BLOCK_LEN;
            if (!CryptEncrypt(hKey, NULL, i + 1 == blocks, NULL, &chunk[i * BLOCK_LEN], &out_len, blocks * BLOCK_LEN))
                goto finally;
            break;
        }

        case false:
        {
            DWORD out_len = BLOCK_LEN;
            if (!CryptDecrypt(hKey, NULL, i + 1 == blocks, NULL, &chunk[i * BLOCK_LEN], &out_len))
                goto finally;
            break;
        }
        }

    Result = true;
    goto finally;

    finally:
    {
        if (hProv)
            CryptReleaseContext(hProv, 0);
        if (hHash)
            CryptDestroyHash(hHash);
        if (hKey)
            CryptDestroyKey(hKey);

        if (Result)
            return std::string(reinterpret_cast<char*>(chunk));
        else
            return "";
    }
}

int main()
{
    std::string key = "12345";
    std::string data = "aaaaaabbbbbb";
    std::string encdata = AES128(key, data, true);
    std::string decdata = AES128(key, encdata, false);

    printf("%s => %s => %s", data.c_str(), encdata.c_str(), decdata.c_str());
    system("pause");
}

对不起英语不好,我是巴西人。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-07-11 20:56:09

您的计算已关闭:ceil((float)data.length() / BLOCK_LEN) + 112-byte输入的2

但是你不需要用块加密,加密API可以帮你处理块。只需为整个输入调用一次。

下面是一个有效的修改版本:

代码语言:javascript
复制
#include <Windows.h>
#include <wincrypt.h>
#include <stdio.h>
#include <cmath>
#include <string>

#pragma comment(lib, "crypt32.lib")

#define BLOCK_LEN 16

std::string AES128(const std::string& key, const std::string& data, bool enc)
{
    std::string result = data;
    bool Result = false;
    HCRYPTPROV hProv = NULL;
    HCRYPTHASH hHash = NULL;
    HCRYPTKEY hKey = NULL;
    result.resize((data.length() + BLOCK_LEN - 1) & ~(BLOCK_LEN - 1));
    DWORD out_len = data.length();

    do {
        if (!CryptAcquireContextA(&hProv, NULL, NULL, PROV_RSA_AES, CRYPT_VERIFYCONTEXT))
            break;

        if (!CryptCreateHash(hProv, CALG_SHA_256, 0, 0, &hHash))
            break;

        if (!CryptHashData(hHash, (const BYTE*)key.c_str(), key.length(), 0))
            break;

        if (!CryptDeriveKey(hProv, CALG_AES_128, hHash, 0, &hKey))
            break;

        if (enc)
        {
            if (!CryptEncrypt(hKey, NULL, TRUE, 0, (BYTE*)result.data(), &out_len, result.length()))
                break;
        }
        else
        {
            if (!CryptDecrypt(hKey, NULL, TRUE, 0, (BYTE*)result.data(), &out_len))
                break;
        }

        result.resize(out_len);
        Result = true;
    } while (false);

    if (hKey)
        CryptDestroyKey(hKey);
    if (hHash)
        CryptDestroyHash(hHash);
    if (hProv)
        CryptReleaseContext(hProv, 0);

    if (!Result)
        result = "";
    return result;
}

int main()
{
    std::string key = "12345";
    std::string data = "aaaaaabbbbbb";
    std::string encdata = AES128(key, data, true);
    std::string decdata = AES128(key, encdata, false);

    printf("%s => %s => %s\n", data.c_str(), encdata.c_str(), decdata.c_str());
}
票数 2
EN

Stack Overflow用户

发布于 2018-07-11 20:52:25

我怀疑你的坠机是来这里的:

代码语言:javascript
复制
return std::string(reinterpret_cast<char*>(chunk));

chunk是一个完全随机的字节序列。它可能嵌入了空白处。它几乎肯定不会以null结尾。此构造函数需要一个以空结尾的字符序列.我怀疑它在继续读取字节,寻找空值,直到它到达无效的地址并崩溃。

AES加密的数据不是字符串.它是一个公正的字节。你需要这样对待它。您可以将其作为vector<BYTE>返回,也可以使用Base64 64或十六进制编码函数将其转换为人类可读的字符串(如果需要的话)。在试图解密该字符串之前,一定要对其进行解码。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51293419

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档