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
| #include <iostream> #include<openssl/pem.h> #include<openssl/ssl.h> #include<openssl/rsa.h> #include<openssl/evp.h> #include<openssl/bio.h> #include<openssl/err.h> #include <stdio.h> #include<fstream> using namespace std; int padding = RSA_PKCS1_PADDING; char publicKey[] = "-----BEGIN PUBLIC KEY-----\n"\ "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAy8Dbv8prpJ/0kKhlGeJY\n"\ "ozo2t60EG8L0561g13R29LvMR5hyvGZlGJpmn65+A4xHXInJYiPuKzrKUnApeLZ+\n"\ "vw1HocOAZtWK0z3r26uA8kQYOKX9Qt/DbCdvsF9wF8gRK0ptx9M6R13NvBxvVQAp\n"\ "fc9jB9nTzphOgM4JiEYvlV8FLhg9yZovMYd6Wwf3aoXK891VQxTr/kQYoq1Yp+68\n"\ "i6T4nNq7NWC+UNVjQHxNQMQMzU6lWCX8zyg3yH88OAQkUXIXKfQ+NkvYQ1cxaMoV\n"\ "PpY72+eVthKzpMeyHkBn7ciumk5qgLTEJAfWZpe4f4eFZj/Rc8Y8Jj2IS5kVPjUy\n"\ "wQIDAQAB\n"\ "-----END PUBLIC KEY-----\n"; char privateKey[] = "-----BEGIN RSA PRIVATE KEY-----\n"\ "MIIEowIBAAKCAQEAy8Dbv8prpJ/0kKhlGeJYoz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VV\n"\ "WBkTeC5D6c/QXcai6yw6OYyNNdp0uznKURe1xvMCgYBVYYcEjWqMuAvyferFGV+5\n"\ "nWqr5gM+yJMFM2bEqupD/HHSLoeiMm2O8KIKvwSeRYzNohKTdZ7FwgZYxr8fGMoG\n"\ "PxQ1VK9DxCvZL4tRpVaU5Rmknud9hg9DQG6xIbgIDR+f79sb8QjYWmcFGc1SyWOA\n"\ "SkjlykZ2yt4xnqi3BfiD9QKBgGqLgRYXmXp1QoVIBRaWUi55nzHg1XbkWZqPXvz1\n"\ "I3uMLv1jLjJlHk3euKqTPmC05HoApKwSHeA0/gOBmg404xyAYJTDcCidTg6hlF96\n"\ "ZBja3xApZuxqM62F6dV4FQqzFX0WWhWp5n301N33r0qR6FumMKJzmVJ1TA8tmzEF\n"\ "yINRAoGBAJqioYs8rK6eXzA8ywYLjqTLu/yQSLBn/4ta36K8DyCoLNlNxSuox+A5\n"\ "w6z2vEfRVQDq4Hm4vBzjdi3QfYLNkTiTqLcvgWZ+eX44ogXtdTDO7c+GeMKWz4XX\n"\ "uJSUVL5+CVjKLjZEJ6Qc2WZLl94xSwL71E41H4YciVnSCQxVc4Jw\n"\ "-----END RSA PRIVATE KEY-----\n";
int createPublicFile(char* file, const string& pubstr) { if (pubstr.empty()) { printf("public key read error\n"); return (-1); } int len = pubstr.length(); string tmp = pubstr; for (int i = 64; i < len; i += 64) { if (tmp[i] != '\n') tmp.insert(i, "\n"); i++; } tmp.insert(0, "-----BEGIN PUBLIC KEY-----\n"); tmp.append("\n-----END PUBLIC KEY-----\n"); ofstream fout(file); fout << tmp.c_str(); return (0); }
int createPrivateFile(char* file, const string& pristr) { if (pristr.empty()) { printf("public key read error\n"); return (-1); } int len = pristr.length(); string tmp = pristr; for (int i = 64; i < len; i += 64) { if (tmp[i] != '\n') tmp.insert(i, "\n"); i++; } tmp.insert(0, "-----BEGIN RSA PRIVATE KEY-----\n"); tmp.append("-----END RSA PRIVATE KEY-----\n"); ofstream fout(file); fout << tmp.c_str(); return (0); }
RSA* createRSA(unsigned char* key, int publi) { RSA* rsa = NULL; BIO* keybio; keybio = BIO_new_mem_buf(key, -1); if (keybio == NULL) { printf("Failed to create key BIO\n"); return 0; } if (publi) rsa = PEM_read_bio_RSA_PUBKEY(keybio, &rsa, NULL, NULL); else rsa = PEM_read_bio_RSAPrivateKey(keybio, &rsa, NULL, NULL); if (rsa == NULL) printf("Failed to create RSA\n"); return rsa; }
int public_encrypt(unsigned char* data, int data_len, unsigned char* key, unsigned char* encrypted) { RSA* rsa = createRSA(key, 1); int result = RSA_public_encrypt(data_len, data, encrypted, rsa, padding); return result; } int private_decrypt(unsigned char* enc_data, int data_len, unsigned char* key, unsigned char* decrypted) { RSA* rsa = createRSA(key, 0); int result = RSA_private_decrypt(data_len, enc_data, decrypted, rsa, padding); return result; } int public_decrypt(unsigned char* enc_data, int data_len, unsigned char* key, unsigned char* decrypted) { RSA* rsa = createRSA(key, 1); int result = RSA_public_decrypt(data_len, enc_data, decrypted, rsa, padding); return result; }
int private_sign(const unsigned char* in_str, unsigned int in_str_len, unsigned char* outret, unsigned int* outlen, unsigned char* key) { RSA* rsa = createRSA(key, 0); unsigned char md[20] = ""; SHA1(in_str, in_str_len, md); int result = RSA_sign(NID_sha1, md, 20, outret, outlen, rsa); if (result != 1) { printf("sign error\n"); return -1; } return result; } int public_verify(const unsigned char* in_str, unsigned int in_len, unsigned char* outret, unsigned int outlen, unsigned char* key) { RSA* rsa = createRSA(key, 1); unsigned char md[20] = ""; SHA1(in_str, in_len, md); int result = RSA_verify(NID_sha1, md, 20, outret, outlen, rsa); if (result != 1) { printf("verify error\n"); return -1; } return result; } int main() { char plainText[2048 / 8] = "hello"; printf("create pem file\n"); string strPublicKey = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQChNr0TmflORv9C62+tSAYhyj4DwB6fyOHqttddq8Y+R+8cIGT7EKuqSRuUUuLVBN6IIjd14UkxxtjHqrDxPWZz9WfX0LB2lTmnSdkg9Q10IfP9ZrVCW8Pe5vJ7gt5iQ4lOebdqR47+ef9E7oE+eJFQhxSYGGy/FnKjBkadJQtwPQIDAQAB"; int file_ret = createPublicFile((char*)"public_test.pem", strPublicKey); unsigned char encrypted[4098] = {}; unsigned char decrypted[4098] = {}; unsigned char signret[4098] = {}; unsigned int siglen; printf("source data=[%s]\n", plainText); printf("public encrytpt ----private decrypt\n"); int encrypted_length = public_encrypt((unsigned char*)plainText, strlen(plainText), (unsigned char*)publicKey, encrypted); if (encrypted_length == -1) { printf("encrypted error \n"); exit(0); } printf("Encrypted length =%d\n", encrypted_length); int decrypted_length = private_decrypt((unsigned char*)encrypted, encrypted_length, (unsigned char*)privateKey, decrypted); if (decrypted_length == -1) { printf("decrypted error \n"); exit(0); } printf("DecryptedText =%s\n", decrypted); printf("DecryptedLength =%d\n", decrypted_length); printf("\nprivate sign ----public verify\n"); int ret = private_sign((const unsigned char*)plainText, strlen(plainText), signret, &siglen, (unsigned char*)privateKey); printf("sign ret =[%d]\n", ret); ret = public_verify((const unsigned char*)plainText, strlen(plainText), signret, siglen, (unsigned char*)publicKey); if (ret == 1) printf("verify OK,ret =[%d]\n", ret); else printf("verify failed,ret =[%d]\n", ret); return (0); }
|