windows自带的加密库实现加密

    //************************************
    // 函数名:CEncryptionDlg::CalculateHash
    // 返回类型:BOOL
    // 功能:    计算hash值
    // 参数1:BYTE *pData 需要计算哈希值的数据
    // 参数2:DWORD dwDataLength 需要计算哈希值的数据长度
    // 参数3:ALG_ID algHashType 需要计算哈希值的数据的类型
    // 参数4:BYTE **ppHashData 计算出来的哈希数据
    // 参数5:DWORD *pdwHashDataLength 计算出来的哈希数据长度
    //************************************
    BOOL CalculateHash(BYTE *pData, DWORD dwDataLength, ALG_ID algHashType, BYTE **ppHashData, DWORD *pdwHashDataLength);

    //************************************
    // 函数名:CEncryptionDlg::AesEncrypt
    // 返回类型:BOOL
    // 功能:    AES加密
    // 参数1:BYTE *pPassword 密钥
    // 参数2:DWORD dwPasswordLength 密钥长度
    // 参数3:BYTE *pData 需要AES加密的数据
    // 参数4:DWORD &dwDataLength 需要AES加密的数据长度
    // 参数5:DWORD dwBufferLength  缓冲区长度
    //************************************
    BOOL AesEncrypt(BYTE *pPassword, DWORD dwPasswordLength, BYTE *pData, DWORD &dwDataLength, DWORD dwBufferLength);

    //************************************
    // 函数名:CEncryptionDlg::AesDecrypt
    // 返回类型:BOOL
    // 功能:    AES解密
    // 参数1:BYTE *pPassword 密钥
    // 参数2:DWORD dwPasswordLength 密钥长度
    // 参数3:BYTE *pData 需要AES解密的数据
    // 参数4:DWORD &dwDataLength 需要AES解密的数据长度
    //************************************
    BOOL AesDecrypt(BYTE *pPassword, DWORD dwPasswordLength, BYTE *pData, DWORD &dwDataLength);

    //************************************
    // 函数名:CEncryptionDlg::GenerateKey
    // 返回类型:BOOL
    // 功能:    生成公钥和私钥
    // 参数1:BYTE **ppPublicKey 公钥
    // 参数2:DWORD *pdwPublicKeyLength 公钥长度
    // 参数3:BYTE **ppPrivateKey 私钥
    // 参数4:DWORD *pdwPrivateKeyLength 私钥长度
    //************************************
    BOOL GenerateKey(BYTE **ppPublicKey, DWORD *pdwPublicKeyLength, BYTE **ppPrivateKey, DWORD *pdwPrivateKeyLength);

    //************************************
    // 函数名:CEncryptionDlg::RsaEncrypt
    // 返回类型:BOOL
    // 功能:    RAS加密
    // 参数1:BYTE *pPublicKey    公钥
    // 参数2:DWORD dwPublicKeyLength    公钥长度
    // 参数3:BYTE *pData    需要加密的数据
    // 参数4:DWORD &dwDataLength    需要加密的数据长度
    // 参数5:DWORD dwBufferLength    缓冲区长度
    //************************************
    BOOL RsaEncrypt(BYTE *pPublicKey, DWORD dwPublicKeyLength, BYTE *pData, DWORD &dwDataLength, DWORD dwBufferLength);

    //************************************
    // 函数名:CEncryptionDlg::RsaDecrypt
    // 返回类型:BOOL
    // 功能:     RAS解密
    // 参数1:BYTE **ppPrivateKey 私钥
    // 参数2:DWORD *pdwPrivateKeyLength 私钥长度
    // 参数3:BYTE *pData    需要解密的数据
    // 参数4:DWORD &dwDataLength    需要解密的数据长度
    //************************************
    BOOL RsaDecrypt(BYTE *pPrivateKey, DWORD dwProvateKeyLength, BYTE *pData, DWORD &dwDataLength);
//计算hash值
BOOL CEncryptionDlg::CalculateHash(BYTE *pData, DWORD dwDataLength, ALG_ID algHashType, BYTE **ppHashData, DWORD *pdwHashDataLength)
{
    BOOL bRet = FALSE;
    HCRYPTPROV hCryptProv = NULL;
    HCRYPTHASH hCryptHash = NULL;
    DWORD dwTemp = 0;
    DWORD dwHashDataLength = 0;
    BYTE* pHashData = NULL;

    // 获得指定CSP的密钥容器的句柄
    bRet = ::CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_AES, CRYPT_VERIFYCONTEXT);
    if (FALSE == bRet)
    {
        MessageBox(_T("CryptAcquireContext Error\r\n"));
        return FALSE;
    }

    // 创建一个HASH对象, 指定HASH算法
    bRet = ::CryptCreateHash(hCryptProv, algHashType, NULL, NULL, &hCryptHash);
    if (FALSE == bRet)
    {
        MessageBox(_T("CryptCreateHash Error\r\n"));
        CryptReleaseContext(hCryptProv, 0);
        return FALSE;
    }

    // 计算HASH数据
    bRet = ::CryptHashData(hCryptHash, pData, dwDataLength, 0);
    if (FALSE == bRet)
    {
        MessageBox(_T("CryptHashData Error\r\n"));
        CryptDestroyHash(hCryptHash);
        CryptReleaseContext(hCryptProv, 0);
        return FALSE;
    }

    // 获取HASH结果的大小
    dwTemp = sizeof(dwHashDataLength);
    bRet = ::CryptGetHashParam(hCryptHash, HP_HASHSIZE, (BYTE *)(&dwHashDataLength), &dwTemp, 0);
    if (FALSE == bRet)
    {
        MessageBox(_T("CryptGetHashParam Error\r\n"));
        CryptDestroyHash(hCryptHash);
        CryptReleaseContext(hCryptProv, 0);
        return FALSE;
    }

    // 申请内存
    pHashData = new BYTE[dwHashDataLength]{ 0 };
    if (NULL == pHashData)
    {
        MessageBox(_T("new Error\r\n"));
        CryptDestroyHash(hCryptHash);
        CryptReleaseContext(hCryptProv, 0);
        return FALSE;
    }

    // 获取HASH结果数据
    bRet = ::CryptGetHashParam(hCryptHash, HP_HASHVAL, pHashData, &dwHashDataLength, 0);
    if (FALSE == bRet)
    {
        MessageBox(_T("CryptGetHashParam Error\r\n"));
        delete[] pHashData;
        pHashData = NULL;
        CryptDestroyHash(hCryptHash);
        CryptReleaseContext(hCryptProv, 0);
        return FALSE;
    }

    // 返回数据
    *ppHashData = pHashData;
    *pdwHashDataLength = dwHashDataLength;

    // 释放关闭
    CryptDestroyHash(hCryptHash);
    CryptReleaseContext(hCryptProv, 0);

    return TRUE;
}

// AES加密
BOOL CEncryptionDlg::AesEncrypt(BYTE *pPassword, DWORD dwPasswordLength, BYTE *pData, DWORD &dwDataLength, DWORD dwBufferLength)
{
    BOOL bRet = TRUE;
    HCRYPTPROV hCryptProv = NULL;
    HCRYPTHASH hCryptHash = NULL;
    HCRYPTKEY hCryptKey = NULL;

    do {
        // 获取CSP句柄
        bRet = ::CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_AES, CRYPT_VERIFYCONTEXT);
        if (FALSE == bRet)
        {
            MessageBox(_T("CryptAcquireContext Error\r\n"));
            break;
        }

        // 创建HASH对象
        bRet = ::CryptCreateHash(hCryptProv, CALG_MD5, NULL, 0, &hCryptHash);
        if (FALSE == bRet)
        {
            MessageBox(_T("CryptCreateHash Error\r\n"));
            break;
        }

        // 对密钥进行HASH计算 计算出密钥的MD5值
        bRet = ::CryptHashData(hCryptHash, pPassword, dwPasswordLength, 0);
        if (FALSE == bRet)
        {
            MessageBox(_T("CryptHashData Error\r\n"));
            break;
        }

        // 使用HASH来生成密钥
        bRet = ::CryptDeriveKey(hCryptProv, CALG_AES_128, hCryptHash, CRYPT_EXPORTABLE, &hCryptKey);
        if (FALSE == bRet)
        {
            MessageBox(_T("CryptDeriveKey Error\r\n"));
            break;
        }
        // 加密数据
        bRet = ::CryptEncrypt(hCryptKey, NULL, TRUE, 0, pData, &dwDataLength, dwBufferLength);
        if (FALSE == bRet)
        {
            MessageBox(_T("CryptEncrypt Error\r\n"));
            break;
        }
    } while (FALSE);

    // 关闭释放
    if (hCryptKey)
    {
        CryptDestroyKey(hCryptKey);
    }
    if (hCryptHash)
    {
        CryptDestroyHash(hCryptHash);
    }
    if (hCryptProv)
    {
        CryptReleaseContext(hCryptProv, 0);
    }

    return bRet;
}

// AES解密
BOOL CEncryptionDlg::AesDecrypt(BYTE *pPassword, DWORD dwPasswordLength, BYTE *pData, DWORD &dwDataLength)
{
    // 变量
    BOOL bRet = TRUE;
    HCRYPTPROV hCryptProv = NULL;
    HCRYPTHASH hCryptHash = NULL;
    HCRYPTKEY hCryptKey = NULL;

    do
    {
        // 获取CSP句柄
        bRet = ::CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_AES, CRYPT_VERIFYCONTEXT);
        if (FALSE == bRet)
        {
            MessageBox(_T("CryptAcquireContext Error\r\n"));
            break;
        }

        // 创建HASH对象
        bRet = ::CryptCreateHash(hCryptProv, CALG_MD5, NULL, 0, &hCryptHash);
        if (FALSE == bRet)
        {
            MessageBox(_T("CryptCreateHash Error\r\n"));
            break;
        }

        // 对密钥进行HASH计算
        bRet = ::CryptHashData(hCryptHash, pPassword, dwPasswordLength, 0);
        if (FALSE == bRet)
        {
            MessageBox(_T("CryptCreateHash Error\r\n"));
            break;
        }

        // 使用HASH来生成密钥
        bRet = ::CryptDeriveKey(hCryptProv, CALG_AES_128, hCryptHash, CRYPT_EXPORTABLE, &hCryptKey);
        if (FALSE == bRet)
        {
            MessageBox(_T("CryptDeriveKey Error\r\n"));
            break;
        }

        // 解密数据
        bRet = ::CryptDecrypt(hCryptKey, NULL, TRUE, 0, pData, &dwDataLength);
        if (FALSE == bRet)
        {
            MessageBox(_T("CryptDecrypt Error\r\n"));
            break;
        }
    } while (FALSE);

    // 关闭释放 
    if (hCryptKey)
    {
        CryptDestroyKey(hCryptKey);
    }
    if (hCryptHash)
    {
        CryptDestroyHash(hCryptHash);
    }
    if (hCryptProv)
    {

        CryptReleaseContext(hCryptProv, 0);
    }
    return bRet;
}

// 生成公钥和私钥
BOOL CEncryptionDlg::GenerateKey(BYTE **ppPublicKey, DWORD *pdwPublicKeyLength, BYTE **ppPrivateKey, DWORD *pdwPrivateKeyLength)
{
    // 变量
    BOOL bRet = TRUE;
    HCRYPTPROV hCryptProv = NULL;
    HCRYPTKEY hCryptKey = NULL;
    DWORD dwPublicKeyLength = 0;
    BYTE* pPublicKey = NULL;
    DWORD dwPrivateKeyLength = 0;
    BYTE* pPrivateKey = NULL;

    do
    {
        // 获取CSP句柄
        bRet = ::CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, 0);
        if (FALSE == bRet)
        {
            MessageBox(_T("CryptAcquireContext Error\r\n"));
            break;
        }

        // 生成公/私密钥对
        bRet = ::CryptGenKey(hCryptProv, AT_KEYEXCHANGE, CRYPT_EXPORTABLE, &hCryptKey);
        if (FALSE == bRet)
        {
            MessageBox(_T("CryptGenKey Error\r\n"));
            break;
        }

        // 获取公钥密钥的长度和内容
        bRet = ::CryptExportKey(hCryptKey, NULL, PUBLICKEYBLOB, 0, NULL, &dwPublicKeyLength);
        if (FALSE == bRet)
        {
            MessageBox(_T("CryptExportKey Error\r\n"));
            break;
        }
        pPublicKey = new BYTE[dwPublicKeyLength]{0};
        bRet = ::CryptExportKey(hCryptKey, NULL, PUBLICKEYBLOB, 0, pPublicKey, &dwPublicKeyLength);
        if (FALSE == bRet)
        {
            MessageBox(_T("CryptExportKey Error\r\n"));
            break;
        }

        // 获取私钥密钥的长度和内容
        bRet = ::CryptExportKey(hCryptKey, NULL, PRIVATEKEYBLOB, 0, NULL, &dwPrivateKeyLength);
        if (FALSE == bRet)
        {
            MessageBox(_T("CryptExportKey Error\r\n"));
            break;
        }
        pPrivateKey = new BYTE[dwPrivateKeyLength]{0};
        bRet = ::CryptExportKey(hCryptKey, NULL, PRIVATEKEYBLOB, 0, pPrivateKey, &dwPrivateKeyLength);
        if (FALSE == bRet)
        {
            MessageBox(_T("CryptExportKey Error\r\n"));
            break;
        }

        // 返回数据
        *ppPublicKey = pPublicKey;
        *pdwPublicKeyLength = dwPublicKeyLength;
        *ppPrivateKey = pPrivateKey;
        *pdwPrivateKeyLength = dwPrivateKeyLength;

    } while (FALSE);

    // 释放关闭
    if (hCryptKey)
    {
        CryptDestroyKey(hCryptKey);
    }
    if (hCryptProv)
    {
        CryptReleaseContext(hCryptProv, 0);
    }
    return bRet;
}

// 公钥加密数据
BOOL CEncryptionDlg::RsaEncrypt(BYTE *pPublicKey, DWORD dwPublicKeyLength, BYTE *pData, DWORD &dwDataLength, DWORD dwBufferLength)
{
    // 变量
    BOOL bRet = TRUE;
    HCRYPTPROV hCryptProv = NULL;
    HCRYPTKEY hCryptKey = NULL;

    do
    {
        // 获取CSP句柄
        bRet = ::CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, 0);
        if (FALSE == bRet)
        {
            MessageBox(_T("CryptAcquireContext Error\r\n"));
            break;
        }

        // 导入公钥
        bRet = ::CryptImportKey(hCryptProv, pPublicKey, dwPublicKeyLength, NULL, 0, &hCryptKey);
        if (FALSE == bRet)
        {
            MessageBox(_T("CryptImportKey Error\r\n"));
            break;
        }

        // 加密数据
        bRet = ::CryptEncrypt(hCryptKey, NULL, TRUE, 0, pData, &dwDataLength, dwBufferLength);
        if (FALSE == bRet)
        {
            MessageBox(_T("CryptEncrypt Error\r\n"));
            break;
        }

    } while (FALSE);

    // 关闭句柄
    if (hCryptKey)
    {
        CryptDestroyKey(hCryptKey);
    }
    if (hCryptProv)
    {
        CryptReleaseContext(hCryptProv, 0);
    }
    return bRet;
}

// 私钥解密数据
BOOL CEncryptionDlg::RsaDecrypt(BYTE *pPrivateKey, DWORD dwProvateKeyLength, BYTE *pData, DWORD &dwDataLength)
{
    // 变量
    BOOL bRet = TRUE;
    HCRYPTPROV hCryptProv = NULL;
    HCRYPTKEY hCryptKey = NULL;

    do
    {
        // 获取CSP句柄
        bRet = ::CryptAcquireContext(&hCryptProv, NULL, NULL, PROV_RSA_FULL, 0);
        if (FALSE == bRet)
        {
            MessageBox(_T("CryptAcquireContext Error\r\n"));
            break;
        }

        // 导入私钥
        bRet = ::CryptImportKey(hCryptProv, pPrivateKey, dwProvateKeyLength, NULL, 0, &hCryptKey);
        if (FALSE == bRet)
        {
            MessageBox(_T("CryptImportKey Error\r\n"));
            break;
        }

        // 解密数据
        bRet = ::CryptDecrypt(hCryptKey, NULL, TRUE, 0, pData, &dwDataLength);
        if (FALSE == bRet)
        {
            MessageBox(_T("CryptDecrypt Error\r\n"));
            break;
        }

    } while (FALSE);

    // 关闭句柄
    if (hCryptKey)
    {
        CryptDestroyKey(hCryptKey);
    }
    if (hCryptProv)
    {
        CryptReleaseContext(hCryptProv, 0);
    }

    return bRet;
}

 

posted @ 2020-06-22 18:10  自己的小白  阅读(1949)  评论(0编辑  收藏  举报