公開鍵を読み込んで表示する

PEM_read_RSAPublicKey を運用できなかった.http://ml.tietew.jp/cppll/cppll_novice/thread_articles/446 の PEM_read_PUBKEY と EVP_PKEY_get1_RSA を利用するようにって事を適用したら解決できた.エラーコードという言葉もあるが,こちらは分からない.PEM_read_RSAPublicKey しても RSA *rpubKey が NULL だったから困っていただけで.

showpubKey.cpp

rsaCodec.cpp からの抜粋で,動作確認済み.VC++ 2008 Express.

#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <stdlib.h>
// openssl/applink.c 内の fopen() への warning C4996 の抑制
#ifdef _WIN32
#ifdef WIN32
#define WIN32_PREDEFINED
#else
#define WIN32
#endif
#pragma warning( disable: 4996 )
#endif
#include <openssl/applink.c>
#ifdef WIN32
#ifdef WIN32_PREDEFINED
#undef WIN32_PREDEFINED
#else
#undef WIN32
#endif
#pragma warning( default: 4996 )
#endif
// warning C4996 の抑制はここまで

const char *pubKeyFilename = "C:\\data\\PEM\\Public\\ossl100a_pubKey.pem";

// 公開鍵で暗号化し,秘密鍵で復号化する
int main ( int argc, char *argv[] )
{
	// 公開鍵ファイルを読み込む
	FILE *fp;
	fopen_s( &fp, pubKeyFilename, "r");
	EVP_PKEY *epubKey = PEM_read_PUBKEY( fp, NULL, NULL, NULL );
	RSA *rpubKey = EVP_PKEY_get1_RSA( epubKey );
	RSA_print_fp( stdout, rpubKey, 0 );

	return 0;
}