zoukankan      html  css  js  c++  java
  • windows crpyt API 实现DES 3DES 3DES_112 加解密

    string Test3DES()
        {
            string key = "12656b2e4ba2f22e";
            HCRYPTPROV hCryptProv = NULL;
            HCRYPTHASH hHash = NULL;
            HCRYPTKEY hCryptKey = NULL;
            char pIV[] = "d566gdbc";  //simple test IV for 3DES
            CryptAcquireContext(&hCryptProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT);
            CryptCreateHash( hCryptProv, CALG_MD5, NULL, 0, &hHash );
            CryptHashData( hHash, (LPBYTE)key.c_str(), (DWORD)key.size(), 0 ); 
            DWORD dwMode = CRYPT_MODE_CBC;
            CryptDeriveKey(hCryptProv, CALG_3DES, hHash, 0, &hCryptKey);
            CryptSetKeyParam(hCryptKey, KP_MODE, (BYTE*)&dwMode, 0);
            CryptSetKeyParam(hCryptKey, KP_IV,(const BYTE*) pIV, 0) ; 
            DWORD dwFilled = 0;
            BOOL ret = CryptEncrypt( hCryptKey, NULL, TRUE, 0, (LPBYTE)cipherText.c_str(), &dwFilled, (DWORD)str.size());
            cipherText.resize(dwFilled);
            if( hCryptKey ) CryptDestroyKey( hCryptKey );
            if( hHash ) CryptDestroyHash( hHash );
            if( hCryptProv ) CryptReleaseContext( hCryptProv, 0 );
            return cipherText;
        }

    3DES_112
         string key = "12656b2e4ba2f22e";
            unsigned char pIV[] = "d566gdbc";  //simple test IV for 3DES
            HCRYPTPROV hCryptProv = NULL;
            HCRYPTHASH hHash = NULL;
            HCRYPTKEY hCryptKey = NULL;
            DWORD ret = CryptAcquireContext(&hCryptProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL,CRYPT_VERIFYCONTEXT);
            if( ret == 0 ) std::wcout << LastError(GetLastError()) << std::endl;
    
            PlainTextKeyBlob keyBlob ={0};
            keyBlob.hdr.bType = PLAINTEXTKEYBLOB;
            keyBlob.hdr.bVersion = CUR_BLOB_VERSION;
            keyBlob.hdr.reserved = 0;
            keyBlob.hdr.aiKeyAlg = CALG_3DES_112;
            keyBlob.cbKeySize =  key.size();
            memcpy(keyBlob.key, key.c_str(), key.size());
    
            DWORD dwSizeBlob = sizeof(BLOBHEADER)+sizeof(DWORD)+key.size();
            ret = CryptImportKey( hCryptProv, (const BYTE*)&keyBlob, dwSizeBlob, 0, CRYPT_EXPORTABLE, &hCryptKey );
            if( ret == 0 ) std::wcout << LastError(GetLastError()) << std::endl;
    
            DWORD dwMode = CRYPT_MODE_CBC;
            CryptSetKeyParam(hCryptKey, KP_MODE, (BYTE*)&dwMode, 0);
            CryptSetKeyParam(hCryptKey, KP_IV,(const BYTE*) pIV, 0) ; 
    
            std::vector< BYTE > buffer( 1024 );
            memcpy( &buffer[0], passwd.c_str(), passwd.size() );
            DWORD dwFilled = passwd.size();
            ret = CryptEncrypt( hCryptKey, NULL, TRUE, 0, (LPBYTE)&buffer[0], &dwFilled, (DWORD)buffer.size());
            if( ret == 0 ) std::wcout << LastError(GetLastError()) << std::endl;
            buffer.resize(dwFilled);
            if( hCryptKey ) CryptDestroyKey( hCryptKey );
            if( hHash ) CryptDestroyHash( hHash );
            if( hCryptProv ) CryptReleaseContext( hCryptProv, 0 );
            return buffer;

     
  • 相关阅读:
    车载OS盘点及特点分析一:车载OS几大系统介绍
    CTF常用软件/工具
    汽车软件产业研究报告(2020年)
    高级加密标准(AES)分析
    工具 | CTP、SimNow、NSight、快期
    CTF之图片隐写术解题思路
    V2X和车路协同研究:5G V2X将成为数字座舱标配
    腾讯安全正式发布《IoT安全能力图谱》
    Microsoft Remote Desktop Beta 下载地址
    密码学初探|加密模式
  • 原文地址:https://www.cnblogs.com/M4ster/p/winCryptAPI_DES.html
Copyright © 2011-2022 走看看