-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathFileManage.cpp
183 lines (157 loc) · 5.17 KB
/
FileManage.cpp
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "D:\preopengpg\IPWorks-OpenPGP-2022-C++-Edition-2\include\qt\qipworksopenpgp.h"
#include "D:\preopengpg\IPWorks-OpenPGP-2022-C++-Edition-2\include\qt\qipworksopenpgpkey.h"
#include "D:\preopengpg\IPWorks-OpenPGP-2022-C++-Edition-2\include\qt\qopenpgp.h"
#pragma comment(lib,"D:\\preopengpg\\IPWorks-OpenPGP-2022-C++-Edition-2\\lib\\ipworksopenpgp22.lib")
#pragma comment(lib,"D:\\preopengpg\\IPWorks-OpenPGP-2022-C++-Edition-2\\lib64\\ipworksopenpgp22.lib")
#include <vector>
#include <Windows.h>
#include <iostream>
using namespace std;
#include "main.h"
#include "global.h"
#include "FileManage.h"
#include "KeyManage.h"
#include "mainwindow.h"
#include <QString>
#include <QMessageBox>
#include <QLabel>
#include <QDebug>
#include <QStack>
#define LINE_LEN 100
FileManage::~FileManage()
{
}
void FileManage::SignAndEncryptString(string pwd, string userID, string keyringDir)
{
OpenPGP pgp;
MyKeyMgr keymgr;
int ret_code = 0;
char command[LINE_LEN]; // user's command
char mykeyringDir[LINE_LEN];
char passphrase[LINE_LEN];
char privateKey[LINE_LEN];
char recipientKey[LINE_LEN];
char signerKey[LINE_LEN];
string mykeyringdir="D:\\OpenPGP_File_Manage_show\\lenovo\\Key\\key-store";
string aid="516";
string apwd="561";
strcpy(mykeyringDir,mykeyringdir.c_str());
strcpy(passphrase,apwd.c_str());
strcpy(privateKey,aid.c_str());
strcpy(recipientKey,aid.c_str());
strcpy(signerKey,aid.c_str());
/*
printf("Keyring Directory: ");
fgets(mykeyringDir, LINE_LEN, stdin);
fgets(mykeyringDir, LINE_LEN, stdin);
mykeyringDir[strlen(mykeyringDir) - 1] = '\0';
keymgr.LoadKeyring(mykeyringDir);
printf("\nPrivate Key User Id (Used to sign outgoing messages and decrypt incoming messages): ");
fgets(privateKey, LINE_LEN, stdin);
privateKey[strlen(privateKey) - 1] = '\0';
printf("Passphrase: ");
fgets(passphrase, LINE_LEN, stdin);
passphrase[strlen(passphrase) - 1] = '\0';
printf("Recipient Key User Id (Used to encrypt outgoing messages): ");
fgets(recipientKey, LINE_LEN, stdin);
recipientKey[strlen(recipientKey) - 1] = '\0';
printf("Signer Key User Id (Used to verify the signature of an incoming message): ");
fgets(signerKey, LINE_LEN, stdin);
signerKey[strlen(signerKey) - 1] = '\0';
*/
while (1)
{
pgp.Reset();
printf("Please enter the message. When finished enter \":q\" on a line by itself:\n");
char message[100];
string msg;
msg="6464848";
strcpy(message,msg.c_str());
/*
message[0] = '\0';
while (fgets(command, LINE_LEN, stdin))
{
command[strlen(command) - 1] = '\0';
if (strcmp(command, ":q") == 0)
break;
strcat(message, command);
strcat(message, "\n");
}
*/
pgp.SetInputMessage(message);
if (strlen(privateKey) <= 0)
{
printf("Please select a private key.\n");
break;
}
if (strlen(recipientKey) <= 0)
{
printf("Please select a recipient key.\n");
break;
}
if (strlen(signerKey) <= 0)
{
printf("Please select a signer key.\n");
break;
}
pgp.SetASCIIArmor(true);
pgp.SetKeyCount(1);
pgp.SetKeyKeyring(0, mykeyringDir);
pgp.SetKeyUserId(0, privateKey);
pgp.SetKeyPassphrase(0, passphrase);
pgp.SetRecipientKeyCount(1);
pgp.SetRecipientKeyKeyring(0, mykeyringDir);
pgp.SetRecipientKeyUserId(0, recipientKey);
pgp.SetOutputFile("D:\\OpenPGP_File_Manage_show\\lenovo\\File\\text4.gpg");
ret_code = pgp.SignAndEncrypt();
if (ret_code) goto done;
//if (strlen(pgp.GetOutputFile()) <= 0)
{
printf("\nSigned & Encrypted Message:\n");
//char* output;
//int len;
pgp.GetOutputMessage();
//printf("%s\n", output);
//output[strlen(output) - 1] = '\0';
//pgp.SetInputMessage(output);
//Output = output;
}
/*
//解密
pgp.SetKeyCount(1);
pgp.SetKeyKeyring(0, mykeyringDir);
pgp.SetKeyUserId(0, privateKey);
pgp.SetKeyPassphrase(0, passphrase);
pgp.SetSignerKeyCount(1);
pgp.SetSignerKeyKeyring(0, mykeyringDir);
pgp.SetSignerKeyUserId(0, signerKey);
ret_code = pgp.DecryptAndVerifySignature();
if (ret_code) goto done;
ret_code = pgp.DecryptAndVerifySignature();
if (ret_code) goto done;
//if (strlen(pgp.GetOutputFile()) <= 0)
{
printf("\nDecrypted & Verified Message:\n");
char* output;
int len;
pgp.GetOutputMessage();
printf("%s\n", output);
}
*/
break;
}
done:
if (ret_code) // Got an error. The user is done.
{
printf("\nError: %d", ret_code);
//if (pgp.GetLastError())
//{
// printf(" \"%s\"\n", pgp.GetLastError());
//}
}
}