-
Notifications
You must be signed in to change notification settings - Fork 0
/
Encoder_CryptoAPI.h
100 lines (76 loc) · 2.14 KB
/
Encoder_CryptoAPI.h
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
#pragma once
#define MY_ENCODING_TYPE (PKCS_7_ASN_ENCODING | X509_ASN_ENCODING)
#include "ConsoleHelper.h"
#include <iostream>
#include <windows.h>
#include <wincrypt.h>
#include <stdio.h>
#include <string>
#include <fstream>
using namespace std;
#pragma comment(lib, "crypt32.lib")
enum EStateKey {
Non,
Gen,
Import,
Export
};
/// <summary>
/// Based On CryptoAPI
/// - debug v0.1
/// </summary>
class Encoder_CryptoAPI
{
private:
EStateKey stateKey;
bool isHandleMessage;
HCRYPTPROV hProv;
HCRYPTKEY hSessionKey;
bool ExportKey();
bool ImportKey();
void MyHandleMessage(string);
void MyHandleError(string);
public:
Encoder_CryptoAPI(EStateKey stateKey, bool isHandleMessage = false);
~Encoder_CryptoAPI();
bool SaveKey();
bool LoadKey();
void SetKey(HCRYPTKEY hSessionKey);
HCRYPTKEY GetKey() { return hSessionKey; }
string Encrypt(string source_str, bool isDebugPrint = false);
string Decrypt(string source_str, bool isDebugPrint = false);
bool EncryptContentOfFile(string path_to_source_file, string path_to_result_file, bool isHasKeyInFile = false);
bool DecryptContentOfFile(string path_to_source_file, string path_to_result_file, bool isHasKeyInFile = false);
};
/*
* === Usage Example: ===
#include <Windows.h>
#include "Encoder.h"
void main() {
SetConsoleCP(1251);
SetConsoleOutputCP(1251);
// string encoder
{
Encoder_CryptoAPI encoder_en(EStateKey::Gen, true);
Encoder_CryptoAPI encoder_de(EStateKey::Non, true);
string coder_str = encoder_en.Encrypt("УниверПервый", true);
encoder_en.SaveKey();
system("pause");
cout << endl;
//encoder_de.SetKey(encoder_en.GetKey());
encoder_de.LoadKey();
encoder_de.Decrypt(coder_str, true);
system("pause");
}
// File encoder
{
Encoder_CryptoAPI encoder_en(EStateKey::Gen, true);
Encoder_CryptoAPI encoder_de(EStateKey::Non, true);
encoder_en.EncryptContentOfFile("DbStudents.bin", "EncryptStudents.txt", true);
system("pause");
encoder_de.DecryptContentOfFile("EncryptStudents.txt", "DecryptStudents.txt", true);
system("pause");
}
return;
}
*/