-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple.cpp~
135 lines (110 loc) · 4.2 KB
/
simple.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
#include <iostream>
#include <fstream>
#include <openssl/ssl.h>
#include <openssl/bio.h>
using namespace std;
int main(int argc, char *argv[])
{
//This section uses BIOs to write a copy of infile.txt to outfile.txt
// and to send the hash of infile.txt to the command window.
// It is a barebones implementation with little to no error checking.
//The SHA1 hash BIO is chained to the input BIO, though it could just
// as easily be chained to the output BIO instead.
char infilename[] = "griffin.txt";
char outfilename[] = "outfile.txt";
char rsaprivatekey[]="rsaprivatekey.pem";
char* buffer[1024];
char *bufferin[1024];
char *bufferout[1024];
BIO *binfile, *boutfile, *hash, *rsaprivate;
binfile = BIO_new_file(infilename, "r");
boutfile = BIO_new_file(outfilename, "w") ;
hash = BIO_new(BIO_f_md());
BIO_set_md(hash, EVP_sha1());
BIO *f= BIO_new_file(rsaprivatekey,"r");
RSA *rsa=PEM_read_bio_RSAPrivateKey(f, NULL, NULL, NULL );
int a=BIO_read(binfile,buffer, 1024);
BIO_push(hash, binfile);
int rsa_encryt= RSA_private_encrypt(1024,(unsigned char*)buffer,(unsigned char *)bufferin,rsa,RSA_PKCS1_PADDING);
BIO *pub= BIO_new_file("rsapublickey.pem","r");
RSA *rsa2=PEM_read_bio_RSA_PUBKEY(pub, NULL, NULL, NULL );
int rsa_decryt = RSA_public_decrypt(1024,(unsigned char *) bufferin, (unsigned char*)bufferout,rsa,RSA_PKCS1_PADDING);
cout<<bufferout<<endl;
RSA_free(rsa);
RSA_free(rsa2);
//binfile = BIO_new_file(infilename, "r");
//Chain on the input
//Chain on the output
//BIO_push(hash, boutfile);
int actualRead, actualWritten;
char mdbuf[EVP_MAX_MD_SIZE];
int mdlen = BIO_gets(hash, mdbuf, EVP_MAX_MD_SIZE);
while((actualRead = BIO_read(hash, bufferout, 1024)) >= 1)
{
//Could send this to multiple chains from here
actualWritten = BIO_write(boutfile, bufferout, actualRead);
}
//Get digest
for(int i = 0; i < mdlen; i++)
{
//Print two hexadecimal digits (8 bits or 1 character) at a time
printf("%02x", mdbuf[i] & 0xFF);
}
printf("\n");
BIO_free_all(boutfile);
BIO_free_all(hash);
return 0;
}
//This function offers an example of chaining a DES cipher to a base 64 encoder
// to a buffer to a file, using BIOs. Taken almost directly from the example code
// in the book "Network Security with OpenSSL". The concepts should be useful
// for preparing the RSA hash and signature.
// Uncomment the function to try it out.
/*
int write_data(const char *filename, char *out, int len, unsigned char *key)
{
int total, written;
BIO *cipher, *b64, *buffer, *file;
// Create a buffered file BIO for writing
file = BIO_new_file(filename, "w") ;
if (! file)
return 0;
// Create a buffering filter BIO to buffer writes to the file
buffer = BIO_new(BIO_f_buffer( ));
// Create a base64 encoding filter BIO
b64 = BIO_new(BIO_f_base64( ));
// Create the cipher filter BIO and set the key. The last parameter of
// BIO_set_cipher is 1 for encryption and 0 for decryption
cipher = BIO_new(BIO_f_cipher( ));
BIO_set_cipher(cipher, EVP_des_ede3_cbc( ), key, NULL, 1);
// Assemble the BIO chain to be in the order cipher-b64-buffer-file
BIO_push(cipher, b64);
BIO_push(b64, buffer);
BIO_push(buffer, file);
// This loop writes the data to the file. It checks for errors as if the
// underlying file were non-blocking
for (total = 0; total < len; total += written)
{
if ((written = BIO_write(cipher, out + total, len - total) ) <= 0)
{
if (BIO_should_retry(cipher) )
{
written = 0;
continue;
}
break;
}
}
// Ensure all of our data is pushed all the way to the file
BIO_flush(cipher) ;
// We now need to free the BIO chain. A call to BIO_free_all(cipher) would
// accomplish this, but we' ll first remove b64 from the chain for
// demonstration purposes.
BIO_pop(b64) ;
// At this point the b64 BIO is isolated and the chain is cipher-buffer-file.
// The following frees all of that memory
BIO_free(b64) ;
BIO_free_all(cipher) ;
return 0;
}
*/