Microsoft MVP성태의 닷넷 이야기
안녕하세요.. openssl 관련하여...혹시.. [링크 복사], [링크+제목 복사]
조회: 12945
글쓴 사람
나그네. (huss5210 at nate.com)
홈페이지
첨부 파일
 

안녕하세요. open소스들은 한번씩 살펴보다가
과정에서.. 잘 되지 않아.. 혹시나 참고나 답변을 얻을수 있지 않을까 해서.. 이렇게 글을 올려봅니다.

openssl를 가지고 살펴 보고 있는데..
과정에서 이거 저거 살펴 보면서 정리가 안되고 있는지.. 계속 해매고 있습니다.ㅋㅋ;;
기초가 부족한 탓일수도 있지만.. 조금 답답해서...

// openssl.cpp : 콘솔 응용 프로그램에 대한 진입점을 정의합니다.
//

#include "stdafx.h"
#include <iostream>
#include <string>
#include <fstream>
#include <openssl/bio.h>
#include <openssl/err.h>
#include <openssl/bn.h>
#include <openssl/rsa.h>
#include <openssl/evp.h>
#include <openssl/x509.h>
#include <openssl/pem.h>
#include <openssl/rand.h>
#include <openssl/err.h>
#include <openssl/pkcs12.h>


// openSSL에서 사용하는 lib
#pragma comment(lib, "libeay32.lib")
#pragma comment(lib, "ssleay32.lib")

/* Type of key */
#define PUBLIC 1
#define PRIVATE 2
 
 #define BITSLen 2048
 


using namespace std;

 
int genrsa_callback(int p, int n, BN_GENCB *cb)
{
    char c='*';

    if (p == 0) c='.';
    if (p == 1) c='+';
    if (p == 2) c='*';
    if (p == 3) c='\n';

    BIO_write((BIO*)cb->arg, &c, 1);
    (void)BIO_flush((BIO*)cb->arg);

#ifdef LINT
    p=n;
#endif

    return 1;
}

 

int testRSAOutput(RSA *r) <<이부분에서.. 이해가..
{
    int ret;
    BIO *out,*in;
    RSA *read;

    const EVP_CIPHER *enc;

    OpenSSL_add_all_algorithms();

    enc=EVP_des_ede3_ofb();

    out=BIO_new_file("pri.pem","w");

    ret=PEM_write_bio_RSAPrivateKey(out,r,enc,NULL,0,NULL,"sadfasdfsadfsadfsadfsadfsadfsdafafdsdfdsafo");
    if(ret!=1)
    {
        RSA_free(r);
        BIO_free(out);
        return -1;
    }
    BIO_flush(out);
    BIO_free(out);

    out=BIO_new_file("pub.pem","w");
    ret=PEM_write_bio_RSAPublicKey(out,r);
    
    if(ret!=1)
    {
        RSA_free(r);
        BIO_free(out);
        return -1;
    }

    BIO_flush(out);
    BIO_free(out);

    in=BIO_new_file("pri.pem","rb");
    read=RSA_new();
    read=PEM_read_bio_RSAPrivateKey(in,&read,NULL,"sadfasdfsadfsadfsadfsadfsadfsdafafdsdfdsafo");
 

    if(read->d != NULL)
    {
        cout << read->d << endl;
        printf("test ok!\n");
    }
    else
    {
        printf("err!\n");
    }
    RSA_free(read);
    BIO_free(in);
    return 0;
}
 
int _tmain(int argc, _TCHAR* argv[])
{
  
    wcout.imbue(locale("korean"));


     const EVP_CIPHER *enc;
 

    RSA *rsa;
    BIO *out,*in;

    int len, ret;

    char *cipherText;

    char *plainText = "Hello RSA!";

    char *originText;

 
    OpenSSL_add_all_algorithms();

    enc=EVP_des_ede3_ofb();

    in=BIO_new_file("pri.pem","rb");
    rsa=RSA_new();
   // rsa=PEM_read_bio_RSA_PUBKEY(in,NULL, NULL,NULL);

     rsa=PEM_read_bio_RSAPrivateKey(in,&rsa,NULL,"sadfasdfsadfsadfsadfsadfsadfsdafafdsdfdsafo");
 

    if(rsa->d != NULL)
    {
        cout << rsa->d << endl;
        printf("test ok!\n");
    }
    else
    {
        printf("err!\n");
    }
 

        cipherText = (char *) calloc (1, (size_t) RSA_size(rsa));

        len = RSA_public_encrypt(strlen(plainText), (const unsigned char*)plainText, (unsigned char*)cipherText, rsa, RSA_PKCS1_PADDING);

        if (len == -1)
        {

               perror("not encrypt data");
               exit(0);

        }

 

      // printf("plain text: %s\n", plainText);
        printf("ciper text: %s\n", cipherText);

 
        originText = (char *) calloc(1, strlen(plainText)+1);
        ret = RSA_private_decrypt(len, (unsigned char*)cipherText, (unsigned char*)originText, rsa, RSA_PKCS1_PADDING);

        if (ret == -1)

        {

               perror("not decrypt data");

               exit(0);

        }

 

        printf("origin text: %s\n", originText);

 

        free(cipherText);
        free(originText);
        RSA_free(rsa);

 
 
    return 0;
}

코드는 이렇게 되어 있는데..
우선 pem생성은 잘 되는데.. 원리가..
pri + pub 두개를 합쳐서 인코딩 디코딩을 ..하는 것이 아닌지..
rsa=PEM_read_bio_RSA_PUBKEY(in,NULL, NULL,NULL); 에러 발생하고... rsa=PEM_read_bio_RSAPrivateKey(in,&rsa,NULL,"sadfasdfsadfsadfsadfsadfsadfsdafafdsdfdsafo"); 할대는 정상 처리 됩니다..
제가 아직 부족해서.. 지금.. 요점을 벗어 나고 있는것은 아닌지.. 잘못 크게 이해 하고 잇는지...

혹시 아시는 다면 답변 해 주시면 감사드립니다.

구글을 통해 이리 저리 찾아보고 잇는데.. 쉽지가 않군요.. 혹시..여기서는 답변을 듣을수 있지 않을까 해서.. 올려 봅니다.
좋은 하루 보내세요..








[최초 등록일: ]
[최종 수정일: 10/6/2010]


비밀번호

댓글 작성자
 



2010-10-07 02시06분
[나그네] 아 찾아네요.. ㅋㅋㅋ 죄송합니다. 좀더 살펴보고 질문드렸어야 했는데..
쉽지 않군요. ^^
[guest]
2010-10-07 09시53분
^^ (휴~~~ 안도의 한숨을 쉬는 중! ^^)
나그네님. 멋집니다. ^^
kevin25

... 46  47  48  49  50  51  52  53  54  55  [56]  57  58  59  60  ...
NoWriterDateCnt.TitleFile(s)
1238(non...3/13/201411916(글쓴이의 요청으로 삭제합니다.) [2]
1237(non...3/11/201412036(글쓴이의 요청으로 삭제합니다.) [2]
1236(non...3/11/201412941(글쓴이의 요청으로 삭제합니다.) [2]
1235(non...3/10/201412065(글쓴이의 요청으로 삭제합니다.) [2]
1234(non...3/10/201414080(글쓴이의 요청으로 삭제합니다.) [3]
1233(non...3/9/201412200(글쓴이의 요청으로 삭제합니다.) [4]
1232(non...3/8/201411302(글쓴이의 요청으로 삭제합니다.) [2]
1231(non...3/7/201412597(글쓴이의 요청으로 삭제합니다.) [9]
1230POCO3/7/201412946쓰레드 안에서 DependencyProperty get, set시 또 다른 스레드 오류.. [1]
1229(non...3/6/201412668(글쓴이의 요청으로 삭제합니다.) [11]
1228POCO3/6/201411189안녕하세요. 질문이 있습니다. [1]
1226김형진3/4/201419777안녕하세요 windows azure에 관해 질문했던 사람입니다. [2]
1224(non...3/3/201415893(글쓴이의 요청으로 삭제합니다.) [11]
1223sadf...3/3/201411495아래 질문에 답변 감사드립니다. 한가지 더 궁금한점이 있어 질문드립니다. [1]
1222(non...3/2/201412334(글쓴이의 요청으로 삭제합니다.) [4]
1221(non...3/1/201412383(글쓴이의 요청으로 삭제합니다.) [2]
1220Until2/28/201410912질문드립니다. [1]
1219이성환2/28/201410588string.Join()과 Enumerable.Aggregate()의 차이가 궁금합니다. [2]파일 다운로드1
1218김형진2/25/201412032안녕하세요. window azure에 대해서 질문이 있어서 문의 드립니다 [4]
1217(non...2/23/201412503(글쓴이의 요청으로 삭제합니다.) [1]
1215아리수2/20/201416024C# 공부하면서 WPF에 대한 질문. [2]
1214조광훈2/20/201414262IIS8 응용프로그램 풀 관련 질문 드립니다. [2]파일 다운로드1
1213김태훈2/17/201411159가상화 프로그램 질문입니다. [1]파일 다운로드1
1212조광훈2/13/201410314ISAPI 필터에서 커스텀 헤더 정보 추가 [1]파일 다운로드1
1211조광훈2/12/201413740isapi 필터 로드 오류 [2]
1208박지호2/9/201417082[오타] 시작하세요 C# 프로그래밍 p.267 ~ 350 [1]
... 46  47  48  49  50  51  52  53  54  55  [56]  57  58  59  60  ...