Hi all,

I am stuck at a very basic problem.

I had a quite big function which I broke into 3 parts to make is more specific. Now, there is a part of the code which assigns values to most of the variables...

when I execute the program, I am getting the context error..
Is there any way I can make the variables accessible to all the functions..

if I just declare the variables before the function definitions.. it would resolve the above error.. but will the variables be bale to contain values from all these functions..


Regards,
abhi

Recommended Answers

All 5 Replies

>I am stuck at a very basic problem.

Please show us your code. C# is an object oriented programming language.

Here is the code:

First function:

public void doTestEnc(      
			int         size,
			int         privateValueSize,
			BigInteger  g,
			BigInteger  p)
         
 
		{         
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;

            string privatekey = pv.X.ToString();
            string publickey = pu.Y.ToString();
byte[] bytes = new byte[e.GetInputBlockSize() + 2];

			try
			{
				e.ProcessBlock(bytes, 0, bytes.Length);

				Debug.Fail("out of range block not detected");
			}
			catch (DataLengthException)
			{
				// expected
			}

			try
			{
				bytes[0] = (byte)0xff;

				e.ProcessBlock(bytes, 0, bytes.Length - 1);

				Debug.Fail("out of range block not detected");
			}
			catch (DataLengthException)
			{
				// expected
			}

			try
			{
				bytes[0] = (byte)0x7f;
                
				e.ProcessBlock(bytes, 0, bytes.Length - 1);
			}
			catch (DataLengthException)
			{
				Debug.Fail("in range block failed");
			}
		}

The second function is as follows:

public void Encrypt()
        {
           // ElGamalEngine e = new ElGamalEngine();
           
            e.Init(true, pu);
            
            if (e.GetOutputBlockSize() != size / 4)
            {
                Debug.Fail(size + " GetOutputBlockSize() on encryption failed.");
            }

            System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();

            byte[] message = encoding.GetBytes("this is a test");
            string s = System.Text.ASCIIEncoding.ASCII.GetString(message);
            Console.WriteLine("Original Message =>{0}", s, "\n");

            //Console.WriteLine(
            byte[] pText = message;
            // Console.WriteLine(pText);
           byte[] cText = e.ProcessBlock(pText, 0, pText.Length);
           usethis = cText;
            string ss = System.Text.ASCIIEncoding.ASCII.GetString(cText);
            Console.WriteLine("Encrypted Message =>\n{0}", ss, "\n");
            

        }

Third function:

public void Decrypt()
        {

            e.Init(false, pv);

            if (e.GetOutputBlockSize() != (size / 8) - 1)
            {
                Debug.Fail(size + " GetOutputBlockSize() on decryption failed.");
            }
            byte[] cText = usethis;

            byte[] pText = e.ProcessBlock(cText, 0, cText.Length);
            string sss = System.Text.ASCIIEncoding.ASCII.GetString(pText);
            Console.WriteLine("Decrypted Message =>{0}\n", sss, "\n");

            // needs to be from the encryt's output...
            System.Text.ASCIIEncoding encoding = new System.Text.ASCIIEncoding();

            byte[] message = encoding.GetBytes("this is a test");
            if (!Arrays.AreEqual(message, pText))
            {
                Debug.Fail(size + " bit test failed");
            }



            e.Init(true, pu);
        }

The values of pu, pv are defined in the very first function and when I try to access them in the following functions, the context error is thrown.

I cannot completely remove the first function and make everything global, as it would conflict with the try/catch blocks and some other local parameters defined within the first function.


Many thanks,

Abhi

I hope you will get some ideas from the following code,

class Test
{
    ElGamalPublicKeyParameters pu;
    ElGamalPrivateKeyParameters pv;

    public void doTestEnc(int size, int privateValueSize, BigInteger g, BigInteger p)
    {
        .....
    }
    public void Encrypt() {
        if (pu == null || pv == null)
            return;
        .....
    }
    public void Decrypt() {
        if (pu == null || pv == null)
            return;
        .....
    }
}

Be sure that the method must be called in following order,

....
  Test obj=new Test();
  obj.doTestEnc(mSize,privateValueSize, gBigInteger, pBigInteger);
  obj.Encrypt(); // or obj.Decrypt();
  ...

Hi,

Thanks for your reply.
I followed your example and now the previous error is gone.

but, still, the variable's seem to be containing null, as I keep getting the "Object reference not set to an instance of an object" error in the other code files which access functions from the current one.

As as example:

The following code is in ElGamalEngine.cs

public void Init(
			bool				forEncryption,
			ICipherParameters	parameters)
		{
			if (parameters is ParametersWithRandom)
			{
				ParametersWithRandom p = (ParametersWithRandom) parameters;

				this.key = (ElGamalKeyParameters) p.Parameters;
				this.random = p.Random;
			}
			else
			{
				this.key = (ElGamalKeyParameters) parameters;
				this.random = new SecureRandom();
			}


			this.forEncryption = forEncryption;
            		this.bitSize = key.Parameters.P.BitLength;

			if (forEncryption)
			{
				if (!(key is ElGamalPublicKeyParameters))
				{
					throw new ArgumentException("ElGamalPublicKeyParameters are required for encryption.");
				}
			}
			else
			{
				if (!(key is ElGamalPrivateKeyParameters))
				{
					throw new ArgumentException("ElGamalPrivateKeyParameters are required for decryption.");
				}
			}
		}

Now, I get the error at line

this.bitSize = key.Parameters.P.BitLength;

Now, this init() function is called from ElGamalTest.cs, where I have defined the variables outside the functions:

ElGamalEngine e = new ElGamalEngine();
ElGamalPublicKeyParameters pu;
ElGamalPrivateKeyParameters pv;

Then, in the same code within the function doTestEnc(), I am calling the init() function as follows:

e.Init(true, pu);

the variable "pu" gets it's value in the doTestEnc function too:

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;

Where am I going wrong?

Within the init() function, I tried to print the value using

Console.WriteLine("BitLength is {0}", key.Parameters.P.BitLength);

and it does print out '512', which means it is not null, which is exactly opposite to what the error says.

Many thanks in advance and apologies for this lengthy and confusing post.

Abhi

Hi,

I tried few more ways but nothing seems to work.. It's the same error
I think the functions are not able to access each other.

Also, there is no main in the ElGamalTest.cs, as the only main is in the Form1.cs, which is the code file for the main form. I am calling all the functions from there..

Many thanks,

Abhi

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.