opensslを使用して鍵のblobを作成しました。 opensslを使用して暗号化操作(暗号化、復号化、署名、検証など)にkeyblobを使用する必要があります。そのコードはプラットフォームに依存しません。暗号操作にKeyBlob(バイト形式で格納)を使用する可能性があります。
// Assume that "private_key" is having private key
// keyBlob is a byte array of size 2048
// e,n,p,q,dmp1,dmq1,iqmp,d are of type BIGNUM* and initialized with NULL
RSA* rsa = new RSA();
rsa = EVP_PKEY_get1_RSA (private_key);
if (NULL == rsa)
{
hResult = errno;
printf("\n\tError:EVP_PKEY_get1_RSA failed\n\n");
break;
} // if
else
{
//printf("\n\tRSA private key generated successfully\n\n");
} // else
e = rsa->e;
n = rsa->n;
p = rsa->p;
q = rsa->q;
dmp1 = rsa->dmp1;
dmq1 = rsa->dmq1;
iqmp = rsa->iqmp;
d = rsa->d;
version = rsa->version;
// Convert from bignum to binary.
// Modulus (n)
modulus = (unsigned char*)calloc(RSA_size(rsa), sizeof(unsigned char));
iResult = BN_bn2bin(n, modulus);
if(!iResult)
{
hResult = errno;
printf("\n\tError:BN_bn2bin failed\n\n");
break;
} // if
// Exponent1 (dmp1)
exponent1 = (unsigned char*)calloc(RSA_size(rsa), sizeof(unsigned char));
iResult = BN_bn2bin(dmp1, exponent1);
if(!iResult)
{
hResult = errno;
printf("\n\tError:BN_bn2bin failed\n\n");
break;
} // if
// Exponent2 (dmq1)
exponent2 = (unsigned char*)calloc(RSA_size(rsa), sizeof(unsigned char));
iResult = BN_bn2bin(dmq1, exponent2);
if(!iResult)
{
hResult = errno;
printf("\n\tError:BN_bn2bin failed\n\n");
break;
} // if
// Prime1 (p)
prime1 = (unsigned char*)calloc(RSA_size(rsa), sizeof(unsigned char));
iResult = BN_bn2bin(p, prime1);
if(!iResult)
{
hResult = errno;
printf("\n\tError:BN_bn2bin failed\n\n");
break;
} // if
// Prime2 (q)
prime2 = (unsigned char*)calloc(RSA_size(rsa), sizeof(unsigned char));
iResult = BN_bn2bin(q, prime2);
if(!iResult)
{
hResult = errno;
printf("\n\tError:BN_bn2bin failed\n\n");
break;
} // if
// Public exponent (e)
public_exponent = (unsigned char*)calloc(RSA_size(rsa), sizeof(unsigned char));
iResult = BN_bn2bin(e, public_exponent);
if(!iResult)
{
hResult = errno;
printf("\n\tError:BN_bn2bin failed\n\n");
break;
} // if
// Private exponent (d)
private_exponent = (unsigned char*)calloc(RSA_size(rsa), sizeof(unsigned char));
iResult = BN_bn2bin(d, private_exponent);
if(!iResult)
{
hResult = errno;
printf("\n\tError:BN_bn2bin failed\n\n");
break;
} // if
// Coefficient (iqmp)
coefficient = (unsigned char*)calloc(RSA_size(rsa), sizeof(unsigned char));
iResult = BN_bn2bin(iqmp, coefficient);
if(!iResult)
{
hResult = errno;
printf("\n\tError:BN_bn2bin failed\n\n");
break;
} // if
RSAPUBKEY* rsapubkey = (RSAPUBKEY*)(keyBlob + sizeof(BLOBHEADER));
rsapubkey->bitlen = MAX_CERT_LEN;
rsapubkey->magic = 0x32415352; // 0x0032a400
rsapubkey->pubexp = *public_exponent;
int m1 = rsapubkey->bitlen/8 + 20;
unsigned int i = 0;
// Convert all the components from Big Endian to Little Endian
for(i = 0; i < (rsapubkey->bitlen/8); i++)
{
keyBlob[m1 - 1 - i] = modulus[i];
} // for
int p1 = rsapubkey->bitlen/16 + m1;
for (i = 0; i < (rsapubkey->bitlen/16); i++)
{
keyBlob[p1 - 1 - i] = prime1[i];
} // for
int p2 = rsapubkey->bitlen/16 + p1;
for(i = 0; i < (rsapubkey->bitlen/16); i++)
{
keyBlob[p2- 1 - i] = prime2[i];
} // for
int e1 = rsapubkey->bitlen/16 + p2;
for(i = 0; i < (rsapubkey->bitlen/16); i++)
{
keyBlob[e1 - 1 - i] = exponent1[i];
} // for
int e2 = rsapubkey->bitlen/16 + e1;
for (i = 0; i < (rsapubkey->bitlen/16); i++)
{
keyBlob[e2 - 1 - i] = exponent2[i];
} // for
int c1 = rsapubkey->bitlen/16 + e2;
for (i = 0; i < (rsapubkey->bitlen/16); i++)
{
keyBlob[c1 - 1 - i] = coefficient[i];
} // for
int d1 = rsapubkey->bitlen/8 + c1;
for (i = 0; i < (rsapubkey->bitlen/8); i++)
{
keyBlob[d1 - 1 - i] = private_exponent[i];
} // for
/*
FYI
struct _RSAPUBKEY {
DWORD magic; // Has to be RSA1
DWORD bitlen; // # of bits in modulus
DWORD pubexp; // public exponent
// Modulus data follows
} RSAPUBKEY
and
struct _PUBLICKEYSTRUC {
BYTE bType;
BYTE bVersion;
WORD reserved;
ALG_ID aiKeyAlg;
} BLOBHEADER, PUBLICKEYSTRUC;
*/
these structures already in wincrypt.h(in windows), but for linux we need add manually..
ここでkeyblobは "keyBlob"に格納されます。 ここまでうまく動作します。 他の関数では、このkeyBlobを(rsa apiを使用して)暗号操作に使用する方法を説明します。 は、バイト配列に...
RSA* rsa = new RSA();
...
e = rsa->e;
n = rsa->n;
p = rsa->p;
q = rsa->q;
dmp1 = rsa->dmp1;
dmq1 = rsa->dmq1;
iqmp = rsa->iqmp;
...私はこれらをひそかすることが期待道を信じて、我々は暗号化
コードを表示し、問題があることを明記してください。 – jww
上記の編集済みの説明を確認してください – Srujan
あなたの問題は何ですか?仮説的なものではなく、本当の問題を述べてください。また、 'RSAPUBKEY'構造と' BLOBHEADER'構造を表示する必要があります。 – jww