dear all

i made a code to sign some data then verify it  part of this data should be
encrypted using rsa then sign it my problems is

1- i generate rsa key pairs and try to print it in a pem file but when i
open the file it was empty

2- when i use function RSA_public_encrypt () to encrypt some data it does
nothing because i print the data using cout<< before encryption then print
it after encryption it was the same

3- the sign function RSA_sign () has a problem
No source available for "RSA_sign() at 0xb7e525e5"

i have attached the code may be this help to solve my problem and know what
i did wrong
thx allot for help
-- 
Warmest regards and best wishes for a good health,*urs sincerely *
*mero*
//============================================================================
// Name        : rsa_sign.cpp
// Author      : Amir
// Version     :
// Copyright   : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================

#include <iostream>
#include "sign.h"
using namespace std;

int main()
{
	Sign test;

	test.SetANData(16);
	test.SetGSData(16,256);

	test.EncryptSharedKey();
	test.DecryptSharedKey();

	test.SignData();
	test.VerifyData();

	return 0;
}
/*
 * sign.cc
 *
 *  Created on: Nov 1, 2014
 *      Author: amir
 */

#include "sign.h"

Sign::Sign()
{
	SetSharedKey();
	GenerateRSAPairs();
}

Sign::~Sign()
{
	  RSA_free(m_rsa_pair);
}

void
Sign::SetANData(int size)
{
	cout<<"andata is "<<endl;
	for (int i = 0 ; i<size ; i++)
	{
		m_anData[i]=i;
		cout<<m_anData[i];
	}
	cout<<endl;
}

void
Sign::SetGSData(int size, int sharedkeysize)
{
	cout<<"gsdata is "<<endl;
	int totalsize = size +sharedkeysize;
	for (int i = 0 ; i<size ; i++)
	{
		m_gsData[i]=i;
		cout<<m_gsData[i];
	}
	for (int j = size ; j<totalsize ; j++)
	{
		m_gsData[j]= m_sharedKey[j];
		cout<<m_gsData[j];
	}
	cout<<endl;
}

void
Sign::SetSharedKey()
{
	cout<<"shared key is "<<endl;
	for (int i = 0; i<256 ; i++)
	{
	m_sharedKey[i] = i;
	cout<<m_sharedKey[i];
	}
	cout<<endl;
}

void
Sign::GenerateRSAPairs()
{
	 m_rsa_pair = RSA_generate_key(2048,RSA_F4,NULL,NULL);
     BIO *pubout = NULL;
     const char szPath[10] = "rsa.pem";
     pubout = BIO_new_file(szPath,"wb");
     PEM_write_bio_RSAPublicKey (pubout , m_rsa_pair);
}


void
Sign::EncryptSharedKey()
{
	int padding = RSA_PKCS1_PADDING;
    RSA_public_encrypt(256,m_sharedKey,m_encryptedSharedKey,m_rsa_pair,padding);

    cout<<"encrypted shared key is "<<endl;
    for (int i = 0 ; i<2048 ; i++)
    {
    	cout<<m_encryptedSharedKey[i];
    }
    cout<<endl;
}


void
Sign::DecryptSharedKey()
{
	int padding = RSA_PKCS1_PADDING;
    RSA_private_decrypt(2048,m_encryptedSharedKey,m_sharedKey,m_rsa_pair,padding);

    cout<<" shared key is "<<endl;
    for (int i = 0 ; i<2048 ; i++)
    {
    	cout<<m_sharedKey[i];
    }
    cout<<endl;
}


void
Sign::SignData()
{
	cout<<"i'm here"<<endl;
	unsigned int *siglen = NULL;
	RSA_sign(NID_sha1, m_anData, 16, m_ANsignedData, siglen, m_rsa_pair);
	cout<<"sign length is "<<*siglen<<endl;
}

bool
Sign::VerifyData()
{
	   int status = 0;
	   status = RSA_verify(NID_sha1, m_anData, AN_Data_Size, m_ANsignedData, 256,m_rsa_pair);

	   if (status == 1)
	   {
	    return true;
	    cout<<"verification is ok"<<endl;
	   }
	   else
	   {
	   return false;
	    cout<<"verification fail"<<endl;
	   }
	   return false;
}

/*
 * sign.h
 *
 *  Created on: Nov 1, 2014
 *      Author: amir
 */

#ifndef SIGN_H_
#define SIGN_H_

#include <iostream>
#include <openssl/rsa.h>
#include <openssl/pem.h>
 #include <openssl/x509.h>
#include <openssl/conf.h>
#include <stdlib.h>
#include <stdio.h>

using namespace std;

#define Shared_Key_Size 256
#define AN_Data_Size    16
#define GS_Data_Size    16


class Sign
{
public:

	Sign();
	~Sign();

	void SetANData(int size);
	void SetGSData(int size,int sharedkeysize);
	void SetSharedKey();

	void GenerateRSAPairs ();
	void EncryptSharedKey();
	void DecryptSharedKey();

	void SignData();
	bool VerifyData();

private:
	unsigned char         m_sharedKey[Shared_Key_Size];
	unsigned char         m_anData[AN_Data_Size];
	unsigned char         m_gsData[GS_Data_Size];
	unsigned char         m_encryptedSharedKey[2048];
	unsigned char         m_ANsignedData[256];
	RSA                  *m_rsa_pair;
};




#endif /* SIGN_H_ */

Reply via email to