rsaCodec.cpp の問題を解決できたと思う

sha256c::clastest() でやってる内容は,自分のクラスオブジェクト h を作って作業している.このオブジェクトのスコープは当然 classtest() 内だから,s->hash なんかも未使用の状態だ.

だから classtest() の後に s.getHash() s.showHash() で "d7a8fbb307d7809469ca9abcb0082e4f8d5651e46d3cdb762d02d0bf37c9e592" なんかは取り出せない.破棄されているし,別のオブジェクトだし.

つまり,どういうことだってばよ?

rsaCodec.cpp

こういうこと.また全文掲載.

#include <openssl/rsa.h>
#include <openssl/pem.h>
#include "sha256c.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 int RSABITNUM = 2048;
const int RSABYTENUM = RSABITNUM/8;
const char *pKeyFilename = "C:\\data\\PEM\\Private\\ossl100a_pKey.pem";
const char *pubKeyFilename = "C:\\data\\PEM\\Public\\ossl100a_pubKey.pem";

void showHash( unsigned char *hash, size_t sz )
{
	for ( size_t i = 0; i < sz; i++ )
	{
		printf("%02x", hash[i] );
	}
	printf("\n");
}


// 公開鍵で暗号化し,秘密鍵で復号化する
int main ( int argc, char *argv[] )
{
	sha256c s;
	char *textvector = "The quick brown fox jumps over the lazy dog";
	s.update( (unsigned char *)textvector, strlen(textvector) );
	s.showHash();

	// プレーンテキスト.暗号化対象
	unsigned char *planetext = s.getHash();

	// 公開鍵ファイルを読み込む
	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 );

	// 秘密鍵ファイルを読み込む
	fopen_s( &fp, pKeyFilename, "r" );
	EVP_PKEY *epKey = PEM_read_PrivateKey( fp, NULL, NULL, NULL );
	RSA *rpKey = EVP_PKEY_get1_RSA( epKey );
	//RSA_print_fp( stdout, rpKey, 0 );

	// 暗号化後のデータ
	unsigned char enctext[RSABYTENUM];

	// 暗号化
	int enclen = RSA_public_encrypt(
		SHA256_DIGEST_LENGTH, 
		planetext,
		enctext,
		rpubKey,
		RSA_PKCS1_OAEP_PADDING
		);

	// 暗号化成立時
	if ( enclen != -1 )
	{
		// 暗号データ表示
		showHash( enctext, enclen);
		// 復号化
		enclen = RSA_private_decrypt( 
			RSABYTENUM,
			enctext,
			planetext,
			rpKey,
			RSA_PKCS1_OAEP_PADDING
			);
		if ( enclen != -1 )
		{
			// 復号データ表示
			showHash( planetext, enclen );
		}
	}

	return 0;
}