안녕하세요. 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]