| Код | #define KEYLENGTH 0x00800000 #define ENCRYPT_ALGORITHM CALG_RC4 #define ENCRYPT_BLOCK_SIZE 8
const LPTSTR pszPassword = L"my_secrey_key";
bool EncryptPassword(const QString &srcPassword, QString &dstPassword) { bool result = false; HCRYPTPROV hCryptProv = NULL; HCRYPTKEY hKey = NULL; HCRYPTHASH hHash = NULL;
DWORD dwBlockLen; DWORD dwBufferLen;
// Get the handle to the default provider. if ( CryptAcquireContext (&hCryptProv, NULL, MS_ENHANCED_PROV, PROV_RSA_FULL, 0) ) { // Create a hash object. if ( CryptCreateHash( hCryptProv, CALG_MD5, 0, 0, &hHash) ) { // Hash the password. if( CryptHashData( hHash, (BYTE *)pszPassword, lstrlen(pszPassword), 0) ) { // Derive a session key from the hash object. if( CryptDeriveKey( hCryptProv, ENCRYPT_ALGORITHM, hHash, KEYLENGTH, &hKey)) { //--------------------------------------------------------------- // Determine the number of bytes to encrypt at a time. // This must be a multiple of ENCRYPT_BLOCK_SIZE. // ENCRYPT_BLOCK_SIZE is set by a #define statement. dwBlockLen = 1000 - 1000 % ENCRYPT_BLOCK_SIZE;
//--------------------------------------------------------------- // Determine the block size. If a block cipher is used, // it must have room for an extra block. if(ENCRYPT_BLOCK_SIZE > 1) dwBufferLen = dwBlockLen + ENCRYPT_BLOCK_SIZE; else dwBufferLen = dwBlockLen;
QByteArray qbdata = srcPassword.toAscii(); DWORD size = (DWORD)qbdata.size(); if(CryptEncrypt( hKey, NULL, TRUE, 0, (BYTE*)qbdata.data(), &size, dwBufferLen)) { dstPassword = qbdata; result = true; } } } } }
// Release the hash object. if(hHash) { if(!(CryptDestroyHash(hHash))) { }
hHash = NULL; }
// Release the session key. if(hKey) { if(!(CryptDestroyKey(hKey))) { } }
// Release the provider handle. if(hCryptProv) { if(!(CryptReleaseContext(hCryptProv, 0))) { } }
return result; }
|
ну вот переделанный пример из мсдн (там файлы шифровались), вырезал отсюда обработку ошибок, чтоб поменьше было ) |