Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Windows.h>
- #include <wincrypt.h>
- #include <iostream>
- using namespace std;
- void ErrorHandling(){
- DWORD dw = GetLastError();
- cout << "Error: " << hex << dw << endl;
- }
- int hybridED(){
- string data = "Hello, World!"; // входная строка
- string encryptedData = data;
- DWORD dataLength = static_cast<DWORD>(data.length()); // её длина
- HCRYPTPROV hProv;
- LPCWSTR hName = L"Container"; // имя контейнера
- HCRYPTKEY hSessionKey, hExportKey; // ключ сессии и публичный
- HCRYPTKEY hImportKey; // ключ для импорта
- DWORD pbDataLen = 0; // длина массива для экспорта ключа
- if (!CryptAcquireContext(&hProv,hName,NULL,PROV_RSA_FULL,CRYPT_NEWKEYSET))
- {
- ErrorHandling();
- return -1;
- }
- // генерируем ключ сессии
- cout << "Generating session key\n";
- if (!CryptGenKey(hProv,CALG_RC4,
- CRYPT_EXPORTABLE | CRYPT_ENCRYPT | CRYPT_DECRYPT,
- &hSessionKey))
- {
- ErrorHandling();
- return -1;
- }
- cout << "Session key has been generated\n";
- // генерируем ключ для экспорта в данном контексте
- cout << "Generating export key\n";
- if (!CryptGenKey(hProv,AT_KEYEXCHANGE,0,&hExportKey))
- {
- ErrorHandling();
- return -1;
- }
- if (!CryptGetUserKey(hProv,AT_KEYEXCHANGE,&hExportKey))
- {
- ErrorHandling();
- return -1;
- }
- cout << "Export key has been generated\n";
- // рассчитываем длину для экспорта ключа
- if (!CryptExportKey(hSessionKey,hExportKey,SIMPLEBLOB,0,NULL,&pbDataLen))
- {
- ErrorHandling();
- return -1;
- }
- PBYTE exportKey = static_cast<PBYTE>(malloc(pbDataLen));
- ZeroMemory(exportKey,pbDataLen);
- // Экспортируем ключ шифрования
- if (!CryptExportKey(hSessionKey,hExportKey,SIMPLEBLOB,0, exportKey,&pbDataLen))
- {
- ErrorHandling();
- return -1;
- }
- cout << "Key has been exported\n";
- cout << "Data encryption\n";
- cout << "\t\tInput data\n";
- cout << "Data: " << data << endl;
- cout << "Data length: " << data.length() << endl;
- if (!CryptEncrypt(hSessionKey,0,TRUE,0,
- (PBYTE) encryptedData.c_str(),&dataLength,data.length()))
- {
- ErrorHandling();
- return -1;
- }
- else
- {
- cout << "Encrypted successfully\n";
- cout << "Encrypted string: " << encryptedData << endl;
- cout << "Encrypted data length:" << static_cast<int>(dataLength) << endl;
- }
- cout << "Data decryption\n";
- cout << "Importing key\n";
- if (!CryptImportKey(hProv,exportKey,pbDataLen,hExportKey,0,&hImportKey))
- {
- ErrorHandling();
- return -1;
- }
- cout << "Key has been imported successfully\n";
- string decryptedData = encryptedData;
- if (CryptDecrypt(hImportKey,0,TRUE,0,(BYTE *) decryptedData.c_str(),&dataLength))
- {
- cout << "Decrypted successfully\n";
- cout << "Decrypted string: " << decryptedData << endl;
- cout << "Decrypted data length: " << static_cast<int>(dataLength) << endl;
- }
- else
- {
- ErrorHandling();
- return -1;
- }
- // освобождаем память
- if (hProv)
- {
- CryptAcquireContext(&hProv,hName,NULL,PROV_RSA_FULL,CRYPT_DELETEKEYSET);
- CryptReleaseContext(hProv,0);
- }
- if (hSessionKey)
- CryptDestroyKey(hSessionKey);
- if (hExportKey)
- CryptDestroyKey(hExportKey);
- return 0; // succeded
- }
- int main(){
- cout << "\t\tHYDRID Encryption\n";
- hybridED();
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement