Showing
3 changed files
with
152 additions
and
0 deletions
doc/미팅 발표 자료/캡스톤디자인2 최종발표.pptx
0 → 100644
This file is too large to display.
No preview for this file type
source code(OPENSSL)/Practice_code.c
0 → 100644
1 | +#include <openssl/pem.h> | ||
2 | +#include <openssl/ssl.h> | ||
3 | +#include <openssl/rsa.h> | ||
4 | +#include <openssl/evp.h> | ||
5 | +#include <openssl/bio.h> | ||
6 | +#include <openssl/err.h> | ||
7 | +#include <stdio.h> | ||
8 | + | ||
9 | +int padding = RSA_PKCS1_PADDING; | ||
10 | + | ||
11 | +RSA *createRSA(unsigned char *key, int public) | ||
12 | +{ | ||
13 | + RSA *rsa = NULL; | ||
14 | + BIO *keybio; | ||
15 | + keybio = BIO_new_mem_buf(key, -1); | ||
16 | + if (keybio == NULL) | ||
17 | + { | ||
18 | + printf("Failed to create key BIO"); | ||
19 | + return 0; | ||
20 | + } | ||
21 | + if (public) | ||
22 | + { | ||
23 | + rsa = PEM_read_bio_RSA_PUBKEY(keybio, &rsa, NULL, NULL); | ||
24 | + } | ||
25 | + else | ||
26 | + { | ||
27 | + rsa = PEM_read_bio_RSAPrivateKey(keybio, &rsa, NULL, NULL); | ||
28 | + } | ||
29 | + if (rsa == NULL) | ||
30 | + { | ||
31 | + printf("Failed to create RSA"); | ||
32 | + } | ||
33 | + | ||
34 | + return rsa; | ||
35 | +} | ||
36 | + | ||
37 | +int public_encrypt(unsigned char *data, int data_len, unsigned char *key, unsigned char *encrypted) | ||
38 | +{ | ||
39 | + RSA *rsa = createRSA(key, 1); | ||
40 | + int result = RSA_public_encrypt(data_len, data, encrypted, rsa, padding); | ||
41 | + return result; | ||
42 | +} | ||
43 | +int private_decrypt(unsigned char *enc_data, int data_len, unsigned char *key, unsigned char *decrypted) | ||
44 | +{ | ||
45 | + RSA *rsa = createRSA(key, 0); | ||
46 | + int result = RSA_private_decrypt(data_len, enc_data, decrypted, rsa, padding); | ||
47 | + return result; | ||
48 | +} | ||
49 | + | ||
50 | +int private_encrypt(unsigned char *data, int data_len, unsigned char *key, unsigned char *encrypted) | ||
51 | +{ | ||
52 | + RSA *rsa = createRSA(key, 0); | ||
53 | + int result = RSA_private_encrypt(data_len, data, encrypted, rsa, padding); | ||
54 | + return result; | ||
55 | +} | ||
56 | +int public_decrypt(unsigned char *enc_data, int data_len, unsigned char *key, unsigned char *decrypted) | ||
57 | +{ | ||
58 | + RSA *rsa = createRSA(key, 1); | ||
59 | + int result = RSA_public_decrypt(data_len, enc_data, decrypted, rsa, padding); | ||
60 | + return result; | ||
61 | +} | ||
62 | + | ||
63 | +void printLastError(char *msg) | ||
64 | +{ | ||
65 | + char *err = malloc(130); | ||
66 | + ; | ||
67 | + ERR_load_crypto_strings(); | ||
68 | + ERR_error_string(ERR_get_error(), err); | ||
69 | + printf("%s ERROR: %s\n", msg, err); | ||
70 | + free(err); | ||
71 | +} | ||
72 | + | ||
73 | +int main() | ||
74 | +{ | ||
75 | + | ||
76 | + char plainText[2048 / 8] = "Hello this is Ravi"; //key length : 2048 | ||
77 | + | ||
78 | + char publicKey[] = "-----BEGIN PUBLIC KEY-----\n" | ||
79 | + "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAy8Dbv8prpJ/0kKhlGeJY\n" | ||
80 | + "ozo2t60EG8L0561g13R29LvMR5hyvGZlGJpmn65+A4xHXInJYiPuKzrKUnApeLZ+\n" | ||
81 | + "vw1HocOAZtWK0z3r26uA8kQYOKX9Qt/DbCdvsF9wF8gRK0ptx9M6R13NvBxvVQAp\n" | ||
82 | + "fc9jB9nTzphOgM4JiEYvlV8FLhg9yZovMYd6Wwf3aoXK891VQxTr/kQYoq1Yp+68\n" | ||
83 | + "i6T4nNq7NWC+UNVjQHxNQMQMzU6lWCX8zyg3yH88OAQkUXIXKfQ+NkvYQ1cxaMoV\n" | ||
84 | + "PpY72+eVthKzpMeyHkBn7ciumk5qgLTEJAfWZpe4f4eFZj/Rc8Y8Jj2IS5kVPjUy\n" | ||
85 | + "wQIDAQAB\n" | ||
86 | + "-----END PUBLIC KEY-----\n"; | ||
87 | + | ||
88 | + char privateKey[] = "-----BEGIN RSA PRIVATE KEY-----\n" | ||
89 | + "MIIEowIBAAKCAQEAy8Dbv8prpJ/0kKhlGeJYozo2t60EG8L0561g13R29LvMR5hy\n" | ||
90 | + "vGZlGJpmn65+A4xHXInJYiPuKzrKUnApeLZ+vw1HocOAZtWK0z3r26uA8kQYOKX9\n" | ||
91 | + "Qt/DbCdvsF9wF8gRK0ptx9M6R13NvBxvVQApfc9jB9nTzphOgM4JiEYvlV8FLhg9\n" | ||
92 | + "yZovMYd6Wwf3aoXK891VQxTr/kQYoq1Yp+68i6T4nNq7NWC+UNVjQHxNQMQMzU6l\n" | ||
93 | + "WCX8zyg3yH88OAQkUXIXKfQ+NkvYQ1cxaMoVPpY72+eVthKzpMeyHkBn7ciumk5q\n" | ||
94 | + "gLTEJAfWZpe4f4eFZj/Rc8Y8Jj2IS5kVPjUywQIDAQABAoIBADhg1u1Mv1hAAlX8\n" | ||
95 | + "omz1Gn2f4AAW2aos2cM5UDCNw1SYmj+9SRIkaxjRsE/C4o9sw1oxrg1/z6kajV0e\n" | ||
96 | + "N/t008FdlVKHXAIYWF93JMoVvIpMmT8jft6AN/y3NMpivgt2inmmEJZYNioFJKZG\n" | ||
97 | + "X+/vKYvsVISZm2fw8NfnKvAQK55yu+GRWBZGOeS9K+LbYvOwcrjKhHz66m4bedKd\n" | ||
98 | + "gVAix6NE5iwmjNXktSQlJMCjbtdNXg/xo1/G4kG2p/MO1HLcKfe1N5FgBiXj3Qjl\n" | ||
99 | + "vgvjJZkh1as2KTgaPOBqZaP03738VnYg23ISyvfT/teArVGtxrmFP7939EvJFKpF\n" | ||
100 | + "1wTxuDkCgYEA7t0DR37zt+dEJy+5vm7zSmN97VenwQJFWMiulkHGa0yU3lLasxxu\n" | ||
101 | + "m0oUtndIjenIvSx6t3Y+agK2F3EPbb0AZ5wZ1p1IXs4vktgeQwSSBdqcM8LZFDvZ\n" | ||
102 | + "uPboQnJoRdIkd62XnP5ekIEIBAfOp8v2wFpSfE7nNH2u4CpAXNSF9HsCgYEA2l8D\n" | ||
103 | + "JrDE5m9Kkn+J4l+AdGfeBL1igPF3DnuPoV67BpgiaAgI4h25UJzXiDKKoa706S0D\n" | ||
104 | + "4XB74zOLX11MaGPMIdhlG+SgeQfNoC5lE4ZWXNyESJH1SVgRGT9nBC2vtL6bxCVV\n" | ||
105 | + "WBkTeC5D6c/QXcai6yw6OYyNNdp0uznKURe1xvMCgYBVYYcEjWqMuAvyferFGV+5\n" | ||
106 | + "nWqr5gM+yJMFM2bEqupD/HHSLoeiMm2O8KIKvwSeRYzNohKTdZ7FwgZYxr8fGMoG\n" | ||
107 | + "PxQ1VK9DxCvZL4tRpVaU5Rmknud9hg9DQG6xIbgIDR+f79sb8QjYWmcFGc1SyWOA\n" | ||
108 | + "SkjlykZ2yt4xnqi3BfiD9QKBgGqLgRYXmXp1QoVIBRaWUi55nzHg1XbkWZqPXvz1\n" | ||
109 | + "I3uMLv1jLjJlHk3euKqTPmC05HoApKwSHeA0/gOBmg404xyAYJTDcCidTg6hlF96\n" | ||
110 | + "ZBja3xApZuxqM62F6dV4FQqzFX0WWhWp5n301N33r0qR6FumMKJzmVJ1TA8tmzEF\n" | ||
111 | + "yINRAoGBAJqioYs8rK6eXzA8ywYLjqTLu/yQSLBn/4ta36K8DyCoLNlNxSuox+A5\n" | ||
112 | + "w6z2vEfRVQDq4Hm4vBzjdi3QfYLNkTiTqLcvgWZ+eX44ogXtdTDO7c+GeMKWz4XX\n" | ||
113 | + "uJSUVL5+CVjKLjZEJ6Qc2WZLl94xSwL71E41H4YciVnSCQxVc4Jw\n" | ||
114 | + "-----END RSA PRIVATE KEY-----\n"; | ||
115 | + | ||
116 | + unsigned char encrypted[4098] = {}; | ||
117 | + unsigned char decrypted[4098] = {}; | ||
118 | + | ||
119 | + int encrypted_length = public_encrypt(plainText, strlen(plainText), publicKey, encrypted); | ||
120 | + if (encrypted_length == -1) | ||
121 | + { | ||
122 | + printLastError("Public Encrypt failed "); | ||
123 | + exit(0); | ||
124 | + } | ||
125 | + printf("Encrypted length =%d\n", encrypted_length); | ||
126 | + | ||
127 | + int decrypted_length = private_decrypt(encrypted, encrypted_length, privateKey, decrypted); | ||
128 | + if (decrypted_length == -1) | ||
129 | + { | ||
130 | + printLastError("Private Decrypt failed "); | ||
131 | + exit(0); | ||
132 | + } | ||
133 | + printf("Decrypted Text =%s\n", decrypted); | ||
134 | + printf("Decrypted Length =%d\n", decrypted_length); | ||
135 | + | ||
136 | + encrypted_length = private_encrypt(plainText, strlen(plainText), privateKey, encrypted); | ||
137 | + if (encrypted_length == -1) | ||
138 | + { | ||
139 | + printLastError("Private Encrypt failed"); | ||
140 | + exit(0); | ||
141 | + } | ||
142 | + printf("Encrypted length =%d\n", encrypted_length); | ||
143 | + | ||
144 | + decrypted_length = public_decrypt(encrypted, encrypted_length, publicKey, decrypted); | ||
145 | + if (decrypted_length == -1) | ||
146 | + { | ||
147 | + printLastError("Public Decrypt failed"); | ||
148 | + exit(0); | ||
149 | + } | ||
150 | + printf("Decrypted Text =%s\n", decrypted); | ||
151 | + printf("Decrypted Length =%d\n", decrypted_length); | ||
152 | +} | ||
... | \ No newline at end of file | ... | \ No newline at end of file |
-
Please register or login to post a comment