일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- sample_n()
- select()
- AES
- summarize()
- 대칭형 알고리즘
- proc contents
- dplyr
- mutate()
- groupe_by()
- samp;e_frac()
- arrange()
- distinct()
- filter()
Archives
- Today
- Total
Gae Ko's Blog
[암호] openssl를 이용한 RSA 암복호화 본문
openssl을 이용한 RSA 암복호화해주는 소스코드를 분석해보았다.
코드는 다음과 같다.
#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> int padding = RSA_PKCS1_PADDING; RSA * createRSA(unsigned char * key,int public) { RSA *rsa= NULL; BIO *keybio ; keybio = BIO_new_mem_buf(key, -1); // 읽기 전용 메모리 만들기 BIO if (keybio==NULL) { printf( "Failed to create key BIO"); return 0; } /* PEM형식인 키 파일을 읽어와서 RSA 구조체 형식으로 변환 */ if(public) // PEM public 키로 RSA 생성 { rsa = PEM_read_bio_RSA_PUBKEY(keybio, &rsa, NULL, NULL); }else // PEM private 키로 RSA 생성 { rsa = PEM_read_bio_RSAPrivateKey(keybio, &rsa, NULL, NULL); } if(rsa == NULL) { printf( "Failed to create RSA"); } 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; // RSA_public_encrypt() returns the size of the encrypted data } /* 개인키로 복호화 */ 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 private_encrypt(unsigned char * data,int data_len,unsigned char * key, unsigned char *encrypted) { RSA * rsa = createRSA(key,0); int result = RSA_private_encrypt(data_len,data,encrypted,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; } /* 에러날 때 해당 에러문 출력 */ void printLastError(char *msg) { char * err = malloc(130);; ERR_load_crypto_strings(); ERR_error_string(ERR_get_error(), err); printf("%s ERROR: %s\n",msg, err); free(err); } int main(){ char plainText[2048/8] = "Hello this is Ravi"; //key length : 2048 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/0kKhlGeJYozo2t60EG8L0561g13R29LvMR5hy\n"\ "vGZlGJpmn65+A4xHXInJYiPuKzrKUnApeLZ+vw1HocOAZtWK0z3r26uA8kQYOKX9\n"\ "Qt/DbCdvsF9wF8gRK0ptx9M6R13NvBxvVQApfc9jB9nTz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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"; unsigned char encrypted[4098]={}; // 암호화한 결과를 저장할 공간 unsigned char decrypted[4098]={}; // 복호화한 결과를 저장할 공간 printf("Plain Text = %s\n", plainText); /* 공개키로 암호화하기 */ int encrypted_length= public_encrypt(plainText,strlen(plainText),publicKey,encrypted); if(encrypted_length == -1) // RSA_public_encrypt() returns -1 on error { printLastError("Public Encrypt failed "); exit(0); } printf("Encrypted Text = %s\n",encrypted); //printf("Encrypted length =%d\n",encrypted_length); /* 개인키로 복호화하기 */ int decrypted_length = private_decrypt(encrypted,encrypted_length,privateKey, decrypted); if(decrypted_length == -1) // RSA_private_decrypt() returns -1 on error { printLastError("Private Decrypt failed "); exit(0); } printf("Decrypted Text = %s\n",decrypted); //printf("Decrypted Length =%d\n",decrypted_length); /* 개인키로 암호화하기 encrypted_length= private_encrypt(plainText,strlen(plainText),privateKey,encrypted); if(encrypted_length == -1) { printLastError("Private Encrypt failed"); exit(0); } printf("Encrypted Text = %s\n",encrypted); 공개키로 복호화하기 decrypted_length = public_decrypt(encrypted,encrypted_length,publicKey, decrypted); if(decrypted_length == -1) { printLastError("Public Decrypt failed"); exit(0); } printf("Decrypted Text =%s\n",decrypted); */ } /* RSA_public_encrypt() returns the size of the encrypted data (i.e., RSA_size(rsa)). RSA_private_decrypt() returns the size of the recovered plaintext. On error, -1 is returned */ | cs |
실행파일을 실행한 결과
'암호' 카테고리의 다른 글
2016 국가암호공모전 (Ⅱ-A) 분야 문제 01 문제 (0) | 2018.08.02 |
---|---|
[암호] 해시함수 (0) | 2018.02.12 |
[암호] openssl로 파일을 암호화/복호화 하기 (0) | 2018.01.25 |
[암호] RSA 암호화 (0) | 2018.01.24 |
[암호] 대칭키 암호 vs 공개키 암호 (0) | 2018.01.24 |