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
| #pragma once #include <iostream> #include <windows.h> #include <openssl/md5.h> #include <openssl/sha.h> #include <openssl/bio.h> #include <openssl/evp.h> #include <openssl/rsa.h> #include <openssl/pem.h> #include <openssl/rand.h> #include <openssl/buffer.h> #include <openssl/applink.c> #pragma comment(lib, "libcrypto.lib") #define PUB_KEY_FILE "pubkey.pem" #define PRI_KEY_FILE "prikey.pem" int md5_encrypt(const void* data, size_t len, unsigned char* md5) { MD5_CTX ctx = { 0 }; MD5_Init(&ctx); MD5_Update(&ctx, data, len); MD5_Final(md5, &ctx); return 0; }
char* Base64Encode(const char* input, int length, bool with_new_line) { BIO* b64 = BIO_new(BIO_f_base64()); if (!with_new_line) BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); b64 = BIO_push(b64, BIO_new(BIO_s_mem())); BIO_write(b64, input, length); BIO_flush(b64); BUF_MEM* bptr = NULL; BIO_get_mem_ptr(b64, &bptr); char* b64encode = new char[bptr->max]{}; memcpy(b64encode, bptr->data, bptr->max); BIO_free_all(b64); return b64encode; } char* Base64Decode(char* input, int length, bool with_new_line) { BIO* b64 = BIO_new(BIO_f_base64()); if (!with_new_line) BIO_set_flags(b64, BIO_FLAGS_BASE64_NO_NL); char* buffer = (char*)malloc(length); if (buffer) memset(buffer, 0, length); BIO* bmem = BIO_push(b64, BIO_new_mem_buf(input, length)); BIO_read(bmem, buffer, length); BIO_free_all(bmem); return buffer; }
RSA* get_public_key() { FILE* public_file = nullptr; if (fopen_s(&public_file, PUB_KEY_FILE, "r") == NULL) { RSA* rsa = PEM_read_RSAPublicKey(public_file, NULL, NULL, NULL); if (public_file) fclose(public_file); return rsa; } return nullptr; } RSA* get_private_key() { FILE* private_file = nullptr; if (fopen_s(&private_file, PRI_KEY_FILE, "r") == NULL) { RSA* rsa = PEM_read_RSAPrivateKey(private_file, NULL, NULL, NULL); if (private_file) fclose(private_file); return rsa; } return nullptr; } BYTE* rsa_encrypt(BYTE* data, RSA* rsa) { int rsa_len = RSA_size(rsa); BYTE* encrypt = (BYTE*)malloc(rsa_len); RSA_public_encrypt(16, data, encrypt, rsa, RSA_PKCS1_PADDING); return encrypt; }
BYTE* rsa_decrypt(BYTE* data, RSA* rsa) { int rsa_len = RSA_size(rsa); BYTE* decrypt = (BYTE*)malloc(rsa_len); RSA_private_decrypt(rsa_len, data, decrypt, rsa, RSA_PKCS1_PADDING); return decrypt; }
void generate_rsa_key() { RSA* keypair = RSA_generate_key(1024, RSA_F4, NULL, NULL); BIO* pri = BIO_new(BIO_s_mem()); PEM_write_bio_RSAPrivateKey(pri, keypair, NULL, NULL, 0, NULL, NULL); size_t pri_len = BIO_pending(pri); char* pri_key = (char*)calloc(pri_len + 1, sizeof(char)); BIO_read(pri, pri_key, pri_len); FILE* private_file = nullptr; if (fopen_s(&private_file, PRI_KEY_FILE, "w") == NULL) { if (pri_key && private_file) { fputs(pri_key, private_file); fclose(private_file); } } BIO* pub = BIO_new(BIO_s_mem()); PEM_write_bio_RSAPublicKey(pub, keypair); size_t pub_len = BIO_pending(pub); char* pub_key = (char*)calloc(pub_len + 1, sizeof(char)); BIO_read(pub, pub_key, pub_len); FILE* public_file = nullptr; if (fopen_s(&public_file, PUB_KEY_FILE, "w") == NULL) { if (pub_key && public_file) { fputs(pub_key, public_file); fclose(public_file); } } RSA_free(keypair); BIO_free_all(pub); BIO_free_all(pri); free(pri_key); free(pub_key); } void calc_file_sig(LPCSTR filename, LPSTR out) { HANDLE file = CreateFileA(filename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); int size = GetFileSize(file, NULL); int count = size % 256 == 0 ? size / 256 : size / 256 + 1; DWORD read = 0; BYTE* file_buffer = new BYTE[size]{ 0 }; ReadFile(file, file_buffer, size, &read, NULL); UCHAR md5[17] = { 0 }; md5_encrypt(file_buffer, size, md5); RSA* pub_key = get_public_key(); BYTE* sig = rsa_encrypt(md5, pub_key); memcpy(out, sig, 128); } bool verify_file_sig(LPCSTR filename, LPSTR sig) { HANDLE file = CreateFileA(filename, GENERIC_READ, NULL, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL); int size = GetFileSize(file, NULL); int count = size % 256 == 0 ? size / 256 : size / 256 + 1; DWORD read = 0; BYTE* file_buffer = new BYTE[size]{ 0 }; ReadFile(file, file_buffer, size, &read, NULL); UCHAR md5[17] = { 0 }; md5_encrypt(file_buffer, size, md5); RSA* pri_key = get_private_key(); BYTE* sig_md5 = rsa_decrypt((BYTE*)sig, pri_key); return !memcmp(sig_md5, md5, 128); }
|