Click here to Skip to main content
15,886,362 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hi, I'm using C++ Builder XE3, and I've generated a public/private key pair in Form1, and I want to use it in Form2 (make it Public), so I've tried the following:

in Form1.h, I've written:
class TForm1 : public TForm
{
__published:    // IDE-managed Components
    TButton *Button1;
    TButton *Button2;
    void __fastcall Button1Click(TObject *Sender);
    void __fastcall Button2Click(TObject *Sender);
private:    // User declarations
    const char * publicKey;
    const char * privateKey;

public:     // User declarations
    __fastcall TForm1(TComponent* Owner);

    const char* GetPublicKey(){return publicKey;}
    const char* GetPrivateKey(){return privateKey;}


And in Form1.cpp, which is responsible for generating the public/private keys, I've written:
void __fastcall TForm1::Button1Click(TObject *Sender)
{    CkRsa rsa;

    bool success;
    success = rsa.UnlockComponent("***************");
    if (success != true) {
        ShowMessage(rsa.lastErrorText());
        return;
    }

    //  Generate a 1024-bit key.  Chilkat RSA supports
    //  key sizes ranging from 512 bits to 4096 bits.
    success = rsa.GenerateKey(1024);
    if (success != true) {
        ShowMessage(rsa.lastErrorText());
        return;
    }

    //  Keys are exported in XML format:
    //const char * publicKey;
    publicKey = rsa.exportPublicKey();
    //printf("Public Key: ");
    ShowMessage(publicKey);

    //const char * privateKey;
    privateKey = rsa.exportPrivateKey();
    //printf("Private Key: ");
    ShowMessage(privateKey);
    Form2->Show();
    //Close();
}


And in Form2.cpp, I've written:
#include <vcl.h>
#pragma hdrstop

#include "Unit2.h"
#include "GenPubPriKeysFORM.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm2 *Form2;
//---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)
    : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void __fastcall TForm2::Button1Click(TObject *Sender)
{
const char* PublicKey = Form1->GetPublicKey();
ShowMessage(PublicKey);

const char* PrivateKey = Form1->GetPrivateKey();
ShowMessage(PrivateKey);
Close();
}


This was my first solution, but i keep getting an error message
"ACCESS VIOLATION AT ADDRESS 50062A73IN MODULE 'rtl170.bpl'. READ OF ADDRESS 00CC54E0" whenever the complier reaches Form2.cpp, which tries to implement the GetPublicKey and GetPrivateKey.


Another solution i've tried was to define the publicKey and PrivateKey as global in Form1.cpp, and then use the extern in the form2.cpp as follows:
C++
extern const char * PublicKey;
extern const char * PublicKey;


But I also get the same error message!

So, can you please help, since I can't understand the problem!

kind regards :)
Posted
Updated 25-Feb-13 8:36am
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900