• 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;

     
  • 相关阅读:
    ACM: HDU 2544 最短路-Dijkstra算法
    ACM: HDU 1874 畅通工程续-Dijkstra算法
    ACM: SGU 101 Domino- 欧拉回路-并查集
    ACM: HDU 1285 确定比赛名次
    ACM: hdu 2647 Reward -拓扑排序
    ACM: hdu 1811 Rank of Tetris
    ACM: poj 1094 Sorting It All Out
    ACM: hihicoder #1174 : 拓扑排序·一 STL- queue
    ACM: CodeForces 140A New Year Table-数学几何
    POJ 3122 Pie 二分枚举
  • 原文地址:https://www.cnblogs.com/M4ster/p/winCryptAPI_DES.html
Copyright © 2020-2023  润新知