1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
|
#include <stdio.h>
#include <stdlib.h>
#include "../ransom/encryption/decrypt.hpp"
#include "../mylibs/arrtypes.h"
#include <iostream>
#ifndef __MGRTOOLS
#define __MGRTOOLS
#define _PWDMGR_MAXSIZE 1000
using namespace std;
struct Credential {
const char *service{nullptr};
const char *username{nullptr};
uint8_t *password{nullptr};
Credential() = default;
Credential(const char *s, const char *u, uint8_t* p)
{
uint8_t *tmp = new uint8_t[strlen((const char *) p) + 1]{};
strcpy((char *) tmp, (const char *) p);
password = tmp;
char *tmp2 = new char[strlen((const char *) u) + 1]{};
strcpy((char *) tmp2, (const char *) u);
username = tmp2;
char *tmp3 = new char[strlen((const char *) s) + 1]{};
strcpy((char *) tmp3, (const char *) s);
service = tmp3;
}
};
auto _AES = [](Credential *cred, uint8_t* key, void (*algortm)(uint8_t*, uint8_t*))-> void
{
size_t aessize{strlen((const char*) cred->password)};
while(aessize % _AES_BLOCKSIZE != 0)
aessize++;
uint8_t *_buffer = new uint8_t[aessize]{};
strcpy((char *) _buffer, (const char *) cred->password);
algortm(_buffer, key);
delete[] cred->password;
cred->password = _buffer;
};
size_t write_file(const char *file, ContainerList<Credential> data, uint8_t* key)
{
size_t written;
FILE *fp = fopen(file, "wb");
if (fp == nullptr)
return numeric_limits<size_t>::max(); // error
data.insert(0, ContainerList<Credential>{{"TESTSERVICE", "TESTUSERNAME", (uint8_t *) "TESTPASS"}});
for (auto &item : data)
_AES(&item, key, AES_encrypt);
written = fwrite((const void *) data.begin(), sizeof(*data.begin()), data.size(), fp);
fclose(fp);
return written - 1;
}
ContainerList<Credential> read_file(const char *file, uint8_t* key)
{
size_t read;
FILE *fp = fopen(file, "rb");
if (fp == nullptr)
return {};
Credential *_in = new Credential[_PWDMGR_MAXSIZE];
read = fread(_in, sizeof(*_in), _PWDMGR_MAXSIZE, fp);
ContainerList<Credential> result{_in, _in + read};
for (auto &item : result)
_AES(&item, key, AES_decrypt);
fclose(fp);
delete[] _in;
return result;
}
#endif
| |