I have a Windows Application which contains functions for Encrypt/Decrypt which is working fine when both the functions are put together as a single code. Now, I wanted to call encrypt and decrypt functions independently of each other, thus, I split the code temporarily in say, function1() and function2() for encrypt / decrypt respectively.

There are few parameters like key etc, which are created only during encryption, i.e. in function1() but will also be used when decryption, i.e. function2() would be called.

I am not able to access the values of the variables being created in the function1() from function2()

Is there any way I can achieve this?
Declaring the functions private or public doesn't make any difference. Also, I cannot create new keys as it would result in improper decryption.

All the above functions are in a single code, Form1.cs

The code which creates the keys is as follows:

ElGamalParameters dhParams = new ElGamalParameters(p, g, privateValueSize);
ElGamalKeyGenerationParameters ekgParams = new ElGamalKeyGenerationParameters(new SecureRandom(), dhParams);

ElGamalKeyPairGenerator kpGen = new ElGamalKeyPairGenerator();

kpGen.Init(ekgParams);
AsymmetricCipherKeyPair pair = kpGen.GenerateKeyPair();
ElGamalPublicKeyParameters pu = (ElGamalPublicKeyParameters)pair.Public;
ElGamalPrivateKeyParameters pv = (ElGamalPrivateKeyParameters)pair.Private;

The pubic key is accessed using

pu.Y

and the private key using

pv.X

Many thanks,
Abhi

I have a Windows Application which contains functions for Encrypt/Decrypt which is working fine when both the functions are put together as a single code. Now, I wanted to call encrypt and decrypt functions independently of each other, thus, I split the code temporarily in say, function1() and function2() for encrypt / decrypt respectively.

There are few parameters like key etc, which are created only during encryption, i.e. in function1() but will also be used when decryption, i.e. function2() would be called.

I am not able to access the values of the variables being created in the function1() from function2()

Is there any way I can achieve this?
Declaring the functions private or public doesn't make any difference. Also, I cannot create new keys as it would result in improper decryption.

If you're generating the key in function1, but it needs to be used in function2, then you should return the key somehow from function1 (as a return value, or in an out parameter) and pass it to function2 as a method parameter.

In general, I recommend separating key generation from the encrypt/decrypt methods; pass the key as a parameter instead, for example:

AsymmetricCipherKeyPair CreateKeyPair(ElGamalParameters parameters)
{
    ElGamalKeyGenerationParameters ekgParams = new ElGamalKeyGenerationParameters(new SecureRandom(), dhParams);

    ElGamalKeyPairGenerator kpGen = new ElGamalKeyPairGenerator();
    kpGen.Init(ekgParams);

    return kpGen.GenerateKeyPair();
}

byte[] Encrypt(byte[] data, AsymmetricKeyParameter publicKey)
{
    // Return encrypted data...
}

byte[] Decrypt(byte[] data, AsymmetricKeyParameter privateKey)
{
    // Return decrypted data...
}

Then somewhere else you have:

ElGamalParameters dhParams = new ElGamalParameters(p, g, privateValueSize);

AsymmetricCipherKeyPair keyPair = CreateKeyPair(dhparams);

byte[] encrypted = Encrypt(data, keyPair.Public);

byte[] decrypted = Decrypt(encrypted, keyPair.Private);

Oh, and this is all for the Bouncy Castle API--it looks like that's what you're using.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.