¿Cómo utilizar C para leer datos de clave pública de un archivo de certificado?
En el programa C, utilicé la clave pública para cifrar la clave de sesión DES y luego utilicé la clave privada en Java para descifrar la clave de sesión; la conversión de código es la codificación DER de acuerdo con el formato inicial de la clave, y los resultados de las operaciones matemáticas también se implementan de acuerdo con la codificación DER.
En un programa Java, descifré obteniendo la clave y los resultados del cifrado de varios archivos previamente almacenados.
4\jre\lib\ext
Modificar el archivo jdk1.4\jre\lib\security\java.security:
#
# Lista de proveedores y sus órdenes de preferencia (ver arriba):
#
security.=sun.security.provider.Sun
security.provider.2=com.sun .net.ssl.internal.ssl.Provider
security.provider.3=com..sun.rsajca.Provider
security.provider.4=com.sun.crypto. proveedor.SunJCE
security.provider.5=sun.security.jgss.SunProvider
security.provider.6=org.bouncycastle.jce.provider.BouncyCastleProvider
=================================== ============= ======================
Código fuente del programa C:
#include
#include
#include
#include
#include
//#define _RSA_KEY_PAIR_ GENERATE_ // Si se genera solo una clave Active esta macro en la primera ejecución
#define _RSA_KEY_PAIR_TOFILE_ // ¿Quiere generar un par de claves? //Si escribir el par de claves en un archivo
#define MAX_RSA_KEY_LENGTH 512 //La longitud máxima de la clave es 512 bytes
#define PUBKEY_ENCRYPT
#define PRIKEY_DECRYPT
#pragma comment(lib, ".
static const char * PUBLIC_KEY_FILE = "pubkey.key";
static const char * PRIVATE_KEY_FILE = "prikey.key";
int RsaKeyPairGen(void)
{
RSA *rsa = NULL;
#ifdef_RSA_KEY_PAIR_GENERATE_
// Generar par de claves RSA:
rsa = RSA_new();
rsa = RSA_generate_key(1024, 0x10001, NULL, NULL); /p>
#endif
//Escribe el par de claves en un archivo y léelo del archivo más tarde
#ifdef _RSA_KEY_PAIR_TOFILE_
unsigned char ucPubClave[MAX_RSA_KEY
_LENGTH] = {0}, ucPriKey[MAX_RSA_KEY_LENGTH] = {0};
int len = i2d_RSAPublicKey(rsa, NULL);
unsigned char* pt = ucPubKey;
len = i2d_RSAPublicKey(rsa, amp; pt);
FILE *fpubkey = NULL;
fpubkey = fopen(PUBLIC_KEY_FILE, "wb"); p>
if(fpubkey == NULL)
{
cout lt;lt; "fopen pubkey.key falló!"
return 0x01;
}
fwrite(ucPubKey, 1, len, fpubkey);
fclose(fpubkey);
len = i2d_RSAPrivateKey(rsa, NULL);
unsigned char* pt2 = ucPriKey
len = i2d_RSAPrivateKey(rsa, amp; pt2); *fprikey = NULL;
fprikey = fopen(PRIVATE_KEY_FILE, "wb");
if(fprikey == NULL)
{
cout lt; "fopen prikey.key falló" lt; endl; 1, len. fprikey);
fclose(fprikey);
#endif
if(rsa != NULL)
{ p>
RSA_free(rsa);
rsa = NULL;
}
devuelve 0;
}
// Lea los datos de la clave privada del archivo y obtenga la clave privada en formato RSA:
int GetPriKey(unsigned char *pucPriKeyData, unsigned long KeyDataLen, RSA* *priRsa)
{
carácter sin firmar *Pt = pucPriKeyData;
* priRsa = d2i_RSAPrivateKey(NULL, amp;Pt, KeyDataLen);
if (priRsa = = NULL)
{
cout lt;lt; "priRsa == NULL! " lt; lt; endl;
devuelve 0x22;
}
devuelve 0;
}
// Obtener la clave pública en formato RSA:
int GetPubKey(unsigned char *pucPubKeyData, unsigned long KeyDataLen, RSA*
*pubRsa)
{
carácter sin firmar *Pt = pucPubKeyData;
*pubRsa = d2i_RSAPublicKey(NULL, amp; Pt, KeyDataLen);
si (pubRsa == NULL)
{
cout lt;lt; "pubRsa == NULL!" /p>
}
return 0;
}
/ / Clave de sesión de cifrado de clave pública:
int encSessionKeybyRsaPubKey( RSA *rsa, carácter sin firmar *ucKey, ulKeyLen largo sin firmar,
carácter sin firmar *outData, largo sin firmar *pulOutLen)
{
return (* pulOutLen = RSA_public_encrypt(ulKeyLen, ucKey, outData, rsa, 1));
}
/clave privada decryptSessionKey:
int decSessionKeybyRsaPriKey(RSA *rsa, carácter sin firmar *InData, ulDataLen largo sin firmar,
carácter sin firmar *ucKey, largo sin firmar *pulKeyLen)
{
return (*pulKeyLen = RSA_private_decrypt(ulDataLen, InData, ucKey, rsa, 1));
}
int main(int argc, char* argv[])
{
unsigned char ucKey[ 8] = {0x01, 0x03, 0x99, 0x4, \
0x80, 0x65, 0x34, 0x08};
carácter sin firmar ucEncryptedKey[512] = {0}, ucDecryptedKey [512 ] = {0};
encrypted_len largo sin firmar = 0, decrypted_len = 0;
#ifdef _RSA_KEY_PAIR_GENERATE_
RsaKeyPairGen(); >#endif
// Obtener la clave pública:
unsigned char ucPubKey[MAX_RSA_KEY_LENGTH] = {0};
FILE *fpubkey = NULL;< / p>
fpubkey = fopen(PUBLIC_KEY_FILE, "rb");
if(fpubkey == NULL)
{
cout lt; "¡Error en fopen pubkey.key! " lt; lt; endl;
return 0x03;
}
p>
fseek(fpubkey, 0, SEEK_END);
int len_PK = ftell(fpubkey);
fseek(fpubkey, 0, SEEK_SET);
fread (ucPubKey, 1, len_PK, fpubkey);
fclose(fpubkey);
#ifdef PUBKEY_ENCRYPT
RSA *pRsaPubKey = NULL; p>
p>
pRsaPubKey = RSA_new ();
GetPubKey(ucPubKey, len_PK, amp; pRsaPubKey);
// Cifrado de clave pública:
encSessionKeybyRsaPubKey (pRsaPubKey, ucKey, sizeof(ucKey), ucEncryptedKey, amp; encrypted_len);
// Escribir archivo:
FILE *fp = NULL
fp = fopen("ucKey.data", "wb");
fwrite(ucEncryptedKey, 1, encrypted_len, fp);
fclose(fp);
if(pRsaPubKey != NULL)
{
RSA_free(pRsaPubKey); pRsaPubKey = NULL;
}
#endif
//Obtener la clave privada:
unsigned char ucPriKey[MAX_RSA_ KEY_LENGTH] = {0};
FILE *fprikey = NULL;
fprikey = fopen(PRIVATE_KEY_FILE, "rb");
if(fprikey == NULL)
{
cout lt; lt; "fopen prikey .key falló" lt; endl;
return 0x02;
}
fseek(fprikey, 0, SEEK_END);
int len_SK = ftell( fprikey);
fseek(fprikey, 0, SEEK_SET);
fread(ucPriKey, 1, len_SK, fprikey);
fclose(fprikey);
#ifdef PRIKEY_ DECRYPT
RSA *pRsaPriKey = NULL;
pRsaPriKey = RSA_new(); p>
GetPriKey(ucPriKey, len_SK, amp; pRsaPriKey);
//Descifrar clave privada:
FILE *fp1 = NULL
fp1; = fopen("ucKey .data", "rb");
int len = ftell(fp1);
fseek(fp1, 0, SEEK_SET
f
read(ucPriKey, 1, len_SK, fp1);
fclose(fp1);
decSessionKeybyRsaPriKey(pRsaPriKey, ucEncryptedKey, encrypted_len, ucDecryptedKey, amp.decrypted_len
<); p>if(pRsaPriKey != NULL){
RSA_free(pRsaPriKey); pRsaPriKey = NULL;
}
Comparación de datos :
if(0 == memcmp(ucKey, ucDecryptedKey, descifrado _len))
{
cout lt;lt; "OK!"
}
else
{
cout lt; "FALLÓ!" }
#endif
devuelve 0;
}