RSA Decryption mit OpenSSL

ziploader

Grünschnabel
Hallo,

ich habe ein Programm in Java geschrieben, mit dem ich Daten per RSA + OAEP Padding verschlüsselt und den privaten Key DER-encoded in eine Datei geschrieben habe.

Nun möchte ich das Ganze in C++ mit der OpenSSL-Bibliothek wieder entschlüsseln.

Ausgangssituation: 1 Array mit verschlüsselten Daten, 1 Datei mit privatem Key

mein Code sieht so aus:

Code:
#include <iostream>
#include <fstream>
#include <openssl/rsa.h>
#include <openssl/rand.h>
#include <openssl/engine.h>

using namespace std;


RSA *DER_decode_RSA_private()
{
	FILE *fp = fopen("c:\\h_t\\test\\privateRSA.key", "rb");
	RSA *rsa = NULL;
	return d2i_RSAPrivateKey_fp(fp, &rsa);
}

void deCompositeFile()
{
	FILE *stream;
	int pointerPos;
	int i,j,rest;
	long endOfFile;
	char iVector[16];
	unsigned char rsa[128];
	unsigned char ergebnis[128];
	char hmac[20];
	
	// get the size of the file
	
	stream = fopen("c:\\h_t\\test\\test.encrypted.jar", "rb");
	if (stream==NULL)
		printf("the file couldn't be opened...");
	else
	{
		pointerPos = fseek(stream, 0, SEEK_END);
		if (pointerPos)
			printf("fseek failed...");
		else
		{	
			endOfFile = ftell(stream);
			cout<<"Position,Groesse: "<<endOfFile<<endl;
		}
		j = endOfFile-16;
		for (i=0;i<16;i++)
		{
			pointerPos = fseek(stream, j, SEEK_SET);
			iVector[i] = fgetc(stream);
			//cout.put(iVector[i]);
			j++;
		}
		j = endOfFile-144;
		for (i=0;i<128;i++)
		{
			pointerPos = fseek(stream, j, SEEK_SET);
			rsa[i] = fgetc(stream);
			//cout.put(rsa[i]);
			j++;
		}
		j = endOfFile-164;
		for (i=0;i<20;i++)
		{
			pointerPos = fseek(stream, j, SEEK_SET);
			hmac[i] = fgetc(stream);
			//cout.put(hmac[i]);
			j++;
		}
		rest = endOfFile-164;
		// the array contains the encrypted class ( rest = file - 16 - 128 - 20)
		char encClass[rest];
		
		j = 0;
		for (i=0;i<rest;i++)
		{
			pointerPos = fseek(stream, j, SEEK_SET);
			encClass[i] = fgetc(stream);
			j++;
		}
		fclose(stream);
		
		RSA * myKey;
		myKey = DER_decode_RSA_private();
		if (myKey==NULL)
			cout<<"myKey ist null"<<endl;
		else
			cout<<"myKey ist nicht null"<<endl;
		
		int result = RSA_private_decrypt(128, rsa, ergebnis, DER_decode_RSA_private(), RSA_PKCS1_OAEP_PADDING);
		if (result == 0)
			cout << "decrypt successful" << endl;
		else
		{
			cout << "decrypt not successful" << endl;
			cout << "Fehlercode: " << result;
		}
	}
}



int main ()
{
	cout << "Decryption of Classes" << endl;
	cout << "---------------------" << endl;
	cout << endl;
	
	deCompositeFile();
	
	return 0;
}

In der Funktion deCompositeFile() übergebe ich eine Datei, die aus mehreren Teilen besteht. Für die RSA-Entschlüsselung benötige ich nur die Bytes, die im Array rsa[] zwischengespeichert sind. Bevor ich entschlüssle habe ich nur eine kurze Abfrage eingefügt, ob überhaupt ein privater Schlüssel eingelesen wurde. Wenn man das irgendwie besser herausbekommt, dann sagt es mir bitte. Anschließend führe die Entschlüsselung mit RSA_private_decrypt(...) durch, wobei das Ergebnis im Array ergebnis[] gespeichert werden soll. Ich bekomme aber nach der Ausführung immer -1 zurückgeliefert (gleich Fehler, siehe rsa.h in OpenSSL Bibliothek).

Könnte mir bitte jemand sagen, was ich falsch gemacht habe



Gruß
Toni
 
Zurück