Microsoft MVP성태의 닷넷 이야기
안녕하세요.. openssl 관련하여...혹시.. [링크 복사], [링크+제목 복사],
조회: 13138
글쓴 사람
나그네. (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

... 31  32  33  34  [35]  36  37  38  39  40  41  42  43  44  45  ...
NoWriterDateCnt.TitleFile(s)
4990heyg...5/21/20189561Sybase Adaptive Server Anywhere 6 버전에 대한 질문입니다. [9]
4989강한음5/18/201811808clickonce 배포 후 실행 무응답 [4]
4988C#7....5/17/20188576dynamic 키워드를 사용한 객체 핸들링에 대해서 여쭤보려고 합니다. [1]
4987포플러5/14/20189572닥터왓슨 로그 - c0000005 (액세스 위반) 분석 부탁드려도 될까요? [2]
4986익명5/14/201811278비주얼 스튜디오 wpf 프로젝트에서 어떻게 하면 exe파일과 실행에 필요한 파일들을 분리해서 정리해서 디렉토리로 묶을 수 있을까요? [4]
4985대구개발자5/7/20188848새로운 폴더만 "이름 없는 파일" 오류 [1]
4984안중언5/6/20189623교재 143page [1]
4983익명5/4/201819051(wpf) 다른 컴퓨터에서 사용하면 자꾸 ('CefSharp.Core.dll' 또는 여기에 종속되어 있는 파일이나 어셈블리 중 하나를 로드할 수 없습니다)라고 떠요.ㅠㅠ [5]
4982Soul...4/27/20189956MFC ActiveX 컨트롤 안에 있는 C# ActiveX 컨트롤 포인터 얻기 [4]
4981대전박4/25/20188864WPF IValueConverter 를 구현해서 StaticResource로 사용할때요 [1]
4980대전박4/23/201810024WPF OS버전 따라 Style 적용이 안되는 프로퍼티가 있을수 있나요? [2]
4979초보개발자4/18/201814170C# 프레임워크 버전이 다른 DLL끼리의 사용 [7]파일 다운로드1
4977Soul...4/17/20189295WebBrowser 컨트롤 Script 통신 문제 [3]
4976맹가이버4/14/201810171윈도우 서비스 프로그램에서 응용프로그램 호출하는 법 [1]
4975lemo...4/11/201810962안녕하세요 네이버로그인관련 질문드립니다. [2]
4973홍길동4/6/20189073ebook 출간 계획은 없으신가요? [2]
4978홍길동4/17/20188837    답변글 [답변]: ebook 출간 계획은 없으신가요?
4972dwkim4/3/201810053EasyHook 관련 질문 [4]
4968최홍준3/30/20188799Windows 7 Credential Provider Android와 연동 [1]
4967이대희3/30/20189960비주얼 스튜디오 설치 워크로드 중에 ".NET Core 플랫폼 간 개발" 이건 뭐하는 것인지요. [1]
4965이대희3/30/20189283자마린 설치후 안드로이드 프로젝트 생성시 디자이너가 없다는 에러가 발생합니다. [3]
4969이대희3/31/20189641    답변글 [답변]: 자마린 설치후 안드로이드 프로젝트 생성시 디자이너가 없다는 에러가 발생합니다.파일 다운로드2
4970이대희4/1/20189429        답변글 [답변]: [답변]: 자마린 설치후 안드로이드 프로젝트 생성시 디자이너가 없다는 에러가 발생합니다. [1]
4963이대희3/29/20189992UWP 스터디를 위해 찰스페졸드 저자의 Programming Windows 6판은 어떠한지요? [1]
4962포플러3/26/201810244C# 응용프로그램 (Winform)에서 unhandledexception 발생시 프로그램이 죽는 현상 이외에 재부팅될 수도 있을까요? [2]
4966포플러3/30/20189874    답변글 [답변]: C# 응용프로그램 (Winform)에서 unhandledexception 발생시 프로그램이 죽는 현상 이외에 재부팅될 수도 있을까요? [1]
... 31  32  33  34  [35]  36  37  38  39  40  41  42  43  44  45  ...