Исправь ошибки в коде error C2065: infile: необъявленный идентификатор error C2664:…
- User
Исправь ошибки в коде error C2065: infile: необъявленный идентификатор error C2664: “void EncryptDecryptFile(std::string,char)”: невозможно преобразовать аргумент 2 из “const char [2]” в “char” error C2101: “&” для константы
#include #include #include #include #include<windows.h> #include<direct.h>
using namespace std;
// Функция шифрования файла void EncryptDecryptFile(string file_name, char key) { ifstream input_file(file_name, ios::binary); ofstream output_file((file_name + “.enc”), ios::binary);
char buffer = 0; if (input_file.is_open() && output_file.is_open()) { while (input_file.get(buffer)) { output_file << (buffer ^ key); } input_file.close(); output_file.close(); remove(file_name.c_str()); // удаляем исходный файл } else { cout << “File not found” << endl; } }
// Функция парольной защиты файла bool PasswordProtectFile(string file_name, string password) { string key = “KEY” + password; const char* key_c = key.c_str(); unsigned char digest[20];
HCRYPTPROV hProv = NULL; HCRYPTHASH hHash = NULL; DWORD cbHash = 0; DWORD dwCount = sizeof(digest); bool success = CryptAcquireContext(&hProv, NULL, MS_ENH_RSA_AES_PROV, PROV_RSA_AES, CRYPT_VERIFYCONTEXT); if (!success) { return false; } success = CryptCreateHash(hProv, CALG_SHA1, 0, 0, &hHash); if (!success) { CryptReleaseContext(hProv, 0); return false; } success = CryptHashData(hHash, (const BYTE*)key_c, strlen(key_c), 0); if (!success) { CryptDestroyHash(hHash); CryptReleaseContext(hProv, 0); return false; } success = CryptGetHashParam(hHash, HP_HASHSIZE, (BYTE*)&cbHash, &(DWORD){ sizeof(DWORD) }, 0); if (!success) { CryptDestroyHash(hHash); CryptReleaseContext(hProv, 0); return false; } success = CryptGetHashParam(hHash, HP_HASHVAL, (BYTE*)digest, &dwCount, 0); if (!success) { CryptDestroyHash(hHash); CryptReleaseContext(hProv, 0); return false; } CryptDestroyHash(hHash); CryptReleaseContext(hProv, 0);
string encrypted_name = file_name + “.bin”; ofstream outfile(encrypted_name, ios::binary | ios::out);
if (!outfile.is_open()) { return false; }
outfile.write((char*)digest, 20);
infile = ifstream(file_name, ios::binary); if (!infile.is_open()) { return false; }
unsigned char byte; success = true; while (infile >> std::noskipws >> byte) { byte ^= digest[success % 20]; if (!outfile.put(byte)) { success = false; break; } ++success; }
outfile.close(); infile.close();
if (remove(file_name.c_str()) == 0) { return true; } else { return false; } }
// Главная функция программы int main() { // Функция шифрования файла EncryptDecryptFile(“example.txt”, “k”);
// Функция парольной защиты файла bool result = PasswordProtectFile(“example.txt”, “password”); if (result == true) { cout << “File protected with password” << endl; } else { cout << “Failed protecting file with password” << endl; }
return 0; }
- Assistant