[feat] generate random AES128 key and iv during initialization, save them in cache file and load when program starts

This commit is contained in:
dijunkun
2024-11-14 00:49:30 +08:00
parent 5aed8317ca
commit 6eac8380b6
5 changed files with 88 additions and 22 deletions

View File

@@ -15,6 +15,7 @@
class Thumbnail {
public:
Thumbnail();
explicit Thumbnail(unsigned char* aes128_key, unsigned char* aes128_iv);
~Thumbnail();
public:
@@ -29,6 +30,24 @@ class Thumbnail {
int DeleteThumbnail(const std::string& file_name);
int DeleteAllFilesInDirectory();
int GetKey(unsigned char* aes128_key) {
memcpy(aes128_key, aes128_key_, sizeof(aes128_key_));
return sizeof(aes128_key_);
}
int GetIv(unsigned char* aes128_iv) {
memcpy(aes128_iv, aes128_iv_, sizeof(aes128_iv_));
return sizeof(aes128_iv_);
}
int GetKeyAndIv(unsigned char* aes128_key, unsigned char* aes128_iv) {
memcpy(aes128_key, aes128_key_, sizeof(aes128_key_));
memcpy(aes128_iv, aes128_iv_, sizeof(aes128_iv_));
return 0;
}
private:
std::vector<std::filesystem::path> FindThumbnailPath(
const std::filesystem::path& directory);
@@ -46,8 +65,8 @@ class Thumbnail {
std::string image_path_ = "thumbnails/";
std::map<std::time_t, std::filesystem::path> thumbnails_sorted_by_write_time_;
unsigned char* key_ = (unsigned char*)"01234567890123456789012345678901";
unsigned char* iv_ = (unsigned char*)"01234567890123456";
unsigned char aes128_key_[16];
unsigned char aes128_iv_[16];
unsigned char ciphertext_[64];
unsigned char decryptedtext_[64];
};