Incompatible Exponent XML?

Mar 6, 2012 at 4:32 PM
Edited Mar 6, 2012 at 5:58 PM

I generated a set of keys using Scrypt resulting in <Exponent>AAEAAQ==</Exponent> in the output of ToXmlString()

However on the server side if I import to RSACryptoServiceProvider using FromXmlString and re-export it changes the content to <Exponent>AQAB</Exponent>

It works dispite that, initially I thought it was a problem but it is not.

private static RSACrypto oRSA;

static void Main(string[] args)
{
	oRSA = new RSACrypto(2048);
	oRSA.OnKeysGenerated += new RSACrypto.KeysGenerated(oRSA_OnKeysGenerated);
	oRSA.GenerateKeys();

	Console.WriteLine("Press any key...");
	Console.Read();
}

static void oRSA_OnKeysGenerated(object sender)
{
	string rsaXml = oRSA.ToXmlString(true);

		RSACryptoServiceProvider dotNETRSA = new RSACryptoServiceProvider();
	dotNETRSA.FromXmlString(rsaXml);

	string test = "TestString";

	byte[] rawBytes = System.Text.Encoding.UTF8.GetBytes(test);
	byte[] encBytes = oRSA.Encrypt(rawBytes);
	byte[] decBytes = dotNETRSA.Decrypt(encBytes, true);

	for (int i = 0; i < rawBytes.Length; i++)
	{
		if (rawBytes[i] != decBytes[i])
		{
			System.Diagnostics.Debug.WriteLine("Not Equal at " + i);
		}
	}


	RSACrypto oRSA2 = new RSACrypto();
	oRSA2.FromXmlString(rsaXml);
	encBytes = oRSA2.Encrypt(rawBytes);
	decBytes = dotNETRSA.Decrypt(encBytes, true);

	for (int i = 0; i < rawBytes.Length; i++)
	{
		if (rawBytes[i] != decBytes[i])
		{
			System.Diagnostics.Debug.WriteLine("Not Equal at " + i);
		}
	}

	if (oRSA.ToXmlString(true) != dotNETRSA.ToXmlString(true))
	{
		System.Diagnostics.Debug.WriteLine("Not Equal");
	}
}

awry.

Coordinator
Mar 6, 2012 at 9:41 PM

Interesting... I believe the exponent being used is 65537.  I'm not sure why the values would be different.  Out of curiosity... if you do the following:

1. Export the key from Scrypt

2.  Import the key into RSACSP and then re-export it...

3.  Import that newest key back into Scrypt..

Does it still work?  There might be some null characters being included in the export from Scrypt.. I'll need to have a look but I believe the final value is the same.