-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmessageDigest435.cpp
More file actions
189 lines (111 loc) · 5.43 KB
/
messageDigest435.cpp
File metadata and controls
189 lines (111 loc) · 5.43 KB
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
184
185
186
187
188
189
/*
Project1 part 2
Name - Sai Satya Sruthi Reddy
ID : 4795909
email : sr264@uakron.edu */
#include <string.h>
#include <iostream>
#include <fstream>
#include "sha256.h"
#include "BigIntegerLibrary.hh"
// Function which takes original message as input and set signature using D and N.
BigUnsigned signAndDecrypt(BigUnsigned originalMessage){
// File function used here is fstream : https://stackoverflow.com/questions/16288562/how-is-stdfstream-with-both-in-and-out-supposed-to-work
std::fstream d_n("d_n.txt", std::fstream::in | std::fstream::out);
std::string swapValue;
//Samelike how we swap ito the temporary value in sorting, here also we do the same to swap the lines in d_n.txt file using swwapValue.
d_n >> swapValue;
//convert the swapvalues from string to bigUnsigned, we have an inbuilt function stringToBigUnsigned that is first line(D to bigDecryptD and N to n)
BigUnsigned bigDecryptD = stringToBigUnsigned(swapValue);
swapValue = "";
d_n >> swapValue;
BigUnsigned bigDecryptN = stringToBigUnsigned(swapValue);
//Sign/"decrypt" this hash value using the private key stored in d_n.txt, we use modexp function from th library to get the signature.
BigUnsigned signature = modexp(originalMessage,bigDecryptD,bigDecryptN);
return signature;
}
int main(int argc, char *argv[])
{
if((argc != 4 && argc != 3) || (argv[1][0]!='s' && argv[1][0]!='v'))
std::cout<< "Program executed wrongly\n Collect format should be \"messageDigest435 s filename\"\n";
else {
std::string filename = argv[2];
//read the file
std::streampos begin,end;
std::ifstream myfile (filename.c_str(), std::ios::binary);
begin = myfile.tellg();
myfile.seekg (0, std::ios::end);
end = myfile.tellg();
std::streampos size = end-begin;
//std::cout << "size of the file: " << size << " bytes."; //size of the file
myfile.seekg (0, std::ios::beg);
char * memblock = new char[size];
myfile.read (memblock, size); //read file; it's saved in the char array memblock
myfile.close();
std::string copyOFfile = filename+".Copy";
std::ofstream myfile2 (copyOFfile.c_str(), std::ios::binary);
myfile2.write (memblock, size); //write to a file
myfile2.close();
//std::cout<<memblock;
if (argv[1][0]=='s')
{
std::cout << "Need to sign the doc.\n";
//Step 1 : Generate a SHA‐256 hash of the content of the file to be signed
std::string hashCode = sha256(memblock);
//Step 2 : Convert the hashcode from base 16 to base 10
BigUnsignedInABase hashCodeBase10 = BigUnsignedInABase(hashCode, 16);
// Step 3 : Save the hashCodeBase10 to original message.
BigUnsigned originalMessage = hashCodeBase10;
//---------------------------------------------------------------------------//
//Step 4 : Sign/"decrypt" this hash value using the private key stored in d_n.txt
BigUnsigned signSignature = signAndDecrypt(originalMessage);
//Step 5 : Save the original content and the signature into one document filename.signed (e.g. "file.txt.signed")
std::ofstream sign;
sign.open( filename + ".signed");
sign << signSignature;
sign.close();
std::cout << "Signature is done, use messageDigest435 v file.txt.signed for verification" ;
}
// ..........Verify the signed file...........
else if (argv[1][0] =='v') {
std::cout << "Verification in process.";
//Step 1 : Apply sha256 to memblock
std::string verifySha256Hash = sha256(memblock);
//Step 2 : convert verifySha256Hash from base 16 to base 10.
BigUnsignedInABase VerifyShaBase16 = BigUnsignedInABase(verifySha256Hash, 16);
//Step 3 : Store in orogianl message variable
BigUnsigned originalMessage = VerifyShaBase16;
// Step 4 : Get e and n from e_n.txt file
std::fstream e_n("e_n.txt", std::fstream::in | std::fstream::out);
//Step 5 : Use temp varaible for swapping the values of e and n and convert e and n to bigunsigned value
std::string temp;
e_n >> temp;
BigUnsigned E = stringToBigUnsigned(temp);
temp = "";
e_n >> temp;
BigUnsigned N = stringToBigUnsigned(temp);
e_n.close();
//Step 6 : Separate the signature from the content of the file in the signed document (e.g. "file.txt.signed");
std::fstream sign;
std::string signatureKey;
sign.open(argv[3]);
// Step 7 : Get/read the signature from the file sign
sign >> signatureKey;
sign.close();
// Step 8 : Convert this signature into BigUnsigned
BigUnsigned signature = stringToBigUnsigned(signatureKey);
// Step 9 : Decrypt this signature with modexp function.
BigUnsigned decryptedMessage = modexp(signature,E,N);
if(originalMessage == decryptedMessage)
{
std::cout << "Document is authentic.";
}
else
{
std::cout << "Document is not authentic.\n";
}
}
delete[] memblock;
}
return 0;
}