T
TimLeung
I'm trying to use RSA encryption in the compact framework (v2.0) and hope
that someone can help me.
I've been working off the MSDN example which can be found here:
http://msdn.microsoft.com/en-us/library/system.security.cryptography.rsacryptoserviceprovider.aspx
In order to load key information, a method called FromXmlString is used.
http://msdn.microsoft.com/en-us/library/system.security.cryptography.rsa.fromxmlstring.aspx
The problem I have is that FromXmlString isn't supported in the compact
framework so I need some help in working out how to load the keys without the
use of this method.
I have pregenerated some keys and have saved them out using the ToXmlString
method. The XML can be found below.
Here's the code that I'm trying to use to encrypt the data. When I call the
ImportParameters method, I get a "Bad Data" exception. Can someone work out
what's wrong with this code or point me to to a working example?
Thanks in advance,
Tim
string p, q, dp, dq, inverse, d, exponent, modulus, clearText;
exponent = @"AQAB";
modulus =
@"1fHlrCs61j0VAIVMsfTcJnF51tisdL+PXBqMt971M3CZOUZJhEyTdlgJoIv8FJif41wg646fS8O+
PoWz7lX3ioyN2AhkTtvEpude5zJGBXEAjOMErhag1ILQTNu+8vqF2hxgBd5pPk/fJOAa9+gM8Pf7
CebNPBSX4zLzzvPhsHU=";
p =
@"++HaZBLVTwRCsMDzvrhdXou73wzgsfa/OSVBsasJgcHIr2y9EvwgowdVXR7oc1/tLc5KpW8TMIi8
RpFvuvzmrQ==";
q =
@"2XFF/B9pGdpSqmw+eSU1IHUaqQo6Sa9J2/F7dIs+/GMhC/TRZ5SHYLrfRrhEIGvIyG4CSeRTdChs
tnHve3NR6Q==";
dp =
@"FzDaPLCHNzk7Mq7xQjQTVKsk5wp2fawsOwS1IbiPrYS8B3rJhb/0+U2pjGNWH5lLaxY9RTIO4D8W
pqCpWtwCGQ==";
dq =
@"poREYAt95Tp4S+KRBR0lTjHftMwMTxlZqoGHVXy9Q3J2gD1/j58C/WSeT/4Uze8816DHR+b+sdmz
RxSBnTx24Q==";
inverse =
@"2VY9lcVhg1CJ+b1UjM5tOZ0dZqP6pIBVfhEXWLwnfOJyFc7CHqkUIBwtb4VqvCbhi8ZPG56yUXpw
7MYMX17R3w==";
d =
@"J3OMy5vJJLpsxgTOqL4jXn9sXT7lRj4QvIY0zq8xfgPUbWhVpCzOwHLpi45ktJOUYEOb2nQZUcpN
qY/UJy35EF/hVv6QBNJ7NF8qx1mpqxCO+9x2lLrT5nFcV3C9O9rlsU3hfriNtuFgMLicM8Fcqfnf
Z08nS0FwRYQuyxwe8WE=";
clearText = "this is the guid that i want to encrypt";
try
{
string cipherText = DoEncrypt(clearText, exponent, modulus,
p, q, dp, dq, inverse, d);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message );
}
//Here's the DoEncrypt function
public static string DoEncrypt(string cipherText, string Exponent,
string Modulus, string p, string q, string dp, string dq, string inverse,
string d)
{
string strRetval = "";
try
{
byte[] enecryptedData;
UnicodeEncoding ByteConverter = new UnicodeEncoding();
RSACryptoServiceProvider rsaDoDecrypt = new
RSACryptoServiceProvider();
RSAParameters rsaParamsPublic = new RSAParameters();
rsaParamsPublic.Exponent = ByteConverter.GetBytes(Exponent);
rsaParamsPublic.Modulus = ByteConverter.GetBytes(Modulus);
rsaParamsPublic.P = ByteConverter.GetBytes(p);
rsaParamsPublic.Q = ByteConverter.GetBytes(q);
rsaParamsPublic.DP = ByteConverter.GetBytes(dp);
rsaParamsPublic.DQ = ByteConverter.GetBytes(dq);
rsaParamsPublic.InverseQ = ByteConverter.GetBytes(inverse);
rsaParamsPublic.D = ByteConverter.GetBytes(d);
//the exception happens on the line below
rsaDoDecrypt.ImportParameters(rsaParamsPublic);
using (rsaDoDecrypt)
{
byte[] dataToEncrypt = ByteConverter.GetBytes(cipherText);
enecryptedData = rsaDoDecrypt.Encrypt(dataToEncrypt,
false);
strRetval = ByteConverter.GetString(enecryptedData, 0,
enecryptedData.Length);
}
}
catch (ArgumentNullException)
{
Console.WriteLine("Encryption failed.");
}
return strRetval;
}
<?xml version="1.0"?>
<RSAParameters xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Exponent>AQAB</Exponent>
<Modulus>1fHlrCs61j0VAIVMsfTcJnF51tisdL+PXBqMt971M3CZOUZJhEyTdlgJoIv8FJif41wg646fS8O+
PoWz7lX3ioyN2AhkTtvEpude5zJGBXEAjOMErhag1ILQTNu+8vqF2hxgBd5pPk/fJOAa9+gM8Pf7
CebNPBSX4zLzzvPhsHU=</Modulus>
<P>++HaZBLVTwRCsMDzvrhdXou73wzgsfa/OSVBsasJgcHIr2y9EvwgowdVXR7oc1/tLc5KpW8TMIi8
RpFvuvzmrQ==</P>
<Q>2XFF/B9pGdpSqmw+eSU1IHUaqQo6Sa9J2/F7dIs+/GMhC/TRZ5SHYLrfRrhEIGvIyG4CSeRTdChs
tnHve3NR6Q==</Q>
<DP>FzDaPLCHNzk7Mq7xQjQTVKsk5wp2fawsOwS1IbiPrYS8B3rJhb/0+U2pjGNWH5lLaxY9RTIO4D8W
pqCpWtwCGQ==</DP>
<DQ>poREYAt95Tp4S+KRBR0lTjHftMwMTxlZqoGHVXy9Q3J2gD1/j58C/WSeT/4Uze8816DHR+b+sdmz
RxSBnTx24Q==</DQ>
<InverseQ>2VY9lcVhg1CJ+b1UjM5tOZ0dZqP6pIBVfhEXWLwnfOJyFc7CHqkUIBwtb4VqvCbhi8ZPG56yUXpw
7MYMX17R3w==</InverseQ>
<D>J3OMy5vJJLpsxgTOqL4jXn9sXT7lRj4QvIY0zq8xfgPUbWhVpCzOwHLpi45ktJOUYEOb2nQZUcpN
qY/UJy35EF/hVv6QBNJ7NF8qx1mpqxCO+9x2lLrT5nFcV3C9O9rlsU3hfriNtuFgMLicM8Fcqfnf
Z08nS0FwRYQuyxwe8WE=</D>
</RSAParameters>
that someone can help me.
I've been working off the MSDN example which can be found here:
http://msdn.microsoft.com/en-us/library/system.security.cryptography.rsacryptoserviceprovider.aspx
In order to load key information, a method called FromXmlString is used.
http://msdn.microsoft.com/en-us/library/system.security.cryptography.rsa.fromxmlstring.aspx
The problem I have is that FromXmlString isn't supported in the compact
framework so I need some help in working out how to load the keys without the
use of this method.
I have pregenerated some keys and have saved them out using the ToXmlString
method. The XML can be found below.
Here's the code that I'm trying to use to encrypt the data. When I call the
ImportParameters method, I get a "Bad Data" exception. Can someone work out
what's wrong with this code or point me to to a working example?
Thanks in advance,
Tim
string p, q, dp, dq, inverse, d, exponent, modulus, clearText;
exponent = @"AQAB";
modulus =
@"1fHlrCs61j0VAIVMsfTcJnF51tisdL+PXBqMt971M3CZOUZJhEyTdlgJoIv8FJif41wg646fS8O+
PoWz7lX3ioyN2AhkTtvEpude5zJGBXEAjOMErhag1ILQTNu+8vqF2hxgBd5pPk/fJOAa9+gM8Pf7
CebNPBSX4zLzzvPhsHU=";
p =
@"++HaZBLVTwRCsMDzvrhdXou73wzgsfa/OSVBsasJgcHIr2y9EvwgowdVXR7oc1/tLc5KpW8TMIi8
RpFvuvzmrQ==";
q =
@"2XFF/B9pGdpSqmw+eSU1IHUaqQo6Sa9J2/F7dIs+/GMhC/TRZ5SHYLrfRrhEIGvIyG4CSeRTdChs
tnHve3NR6Q==";
dp =
@"FzDaPLCHNzk7Mq7xQjQTVKsk5wp2fawsOwS1IbiPrYS8B3rJhb/0+U2pjGNWH5lLaxY9RTIO4D8W
pqCpWtwCGQ==";
dq =
@"poREYAt95Tp4S+KRBR0lTjHftMwMTxlZqoGHVXy9Q3J2gD1/j58C/WSeT/4Uze8816DHR+b+sdmz
RxSBnTx24Q==";
inverse =
@"2VY9lcVhg1CJ+b1UjM5tOZ0dZqP6pIBVfhEXWLwnfOJyFc7CHqkUIBwtb4VqvCbhi8ZPG56yUXpw
7MYMX17R3w==";
d =
@"J3OMy5vJJLpsxgTOqL4jXn9sXT7lRj4QvIY0zq8xfgPUbWhVpCzOwHLpi45ktJOUYEOb2nQZUcpN
qY/UJy35EF/hVv6QBNJ7NF8qx1mpqxCO+9x2lLrT5nFcV3C9O9rlsU3hfriNtuFgMLicM8Fcqfnf
Z08nS0FwRYQuyxwe8WE=";
clearText = "this is the guid that i want to encrypt";
try
{
string cipherText = DoEncrypt(clearText, exponent, modulus,
p, q, dp, dq, inverse, d);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message );
}
//Here's the DoEncrypt function
public static string DoEncrypt(string cipherText, string Exponent,
string Modulus, string p, string q, string dp, string dq, string inverse,
string d)
{
string strRetval = "";
try
{
byte[] enecryptedData;
UnicodeEncoding ByteConverter = new UnicodeEncoding();
RSACryptoServiceProvider rsaDoDecrypt = new
RSACryptoServiceProvider();
RSAParameters rsaParamsPublic = new RSAParameters();
rsaParamsPublic.Exponent = ByteConverter.GetBytes(Exponent);
rsaParamsPublic.Modulus = ByteConverter.GetBytes(Modulus);
rsaParamsPublic.P = ByteConverter.GetBytes(p);
rsaParamsPublic.Q = ByteConverter.GetBytes(q);
rsaParamsPublic.DP = ByteConverter.GetBytes(dp);
rsaParamsPublic.DQ = ByteConverter.GetBytes(dq);
rsaParamsPublic.InverseQ = ByteConverter.GetBytes(inverse);
rsaParamsPublic.D = ByteConverter.GetBytes(d);
//the exception happens on the line below
rsaDoDecrypt.ImportParameters(rsaParamsPublic);
using (rsaDoDecrypt)
{
byte[] dataToEncrypt = ByteConverter.GetBytes(cipherText);
enecryptedData = rsaDoDecrypt.Encrypt(dataToEncrypt,
false);
strRetval = ByteConverter.GetString(enecryptedData, 0,
enecryptedData.Length);
}
}
catch (ArgumentNullException)
{
Console.WriteLine("Encryption failed.");
}
return strRetval;
}
<?xml version="1.0"?>
<RSAParameters xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Exponent>AQAB</Exponent>
<Modulus>1fHlrCs61j0VAIVMsfTcJnF51tisdL+PXBqMt971M3CZOUZJhEyTdlgJoIv8FJif41wg646fS8O+
PoWz7lX3ioyN2AhkTtvEpude5zJGBXEAjOMErhag1ILQTNu+8vqF2hxgBd5pPk/fJOAa9+gM8Pf7
CebNPBSX4zLzzvPhsHU=</Modulus>
<P>++HaZBLVTwRCsMDzvrhdXou73wzgsfa/OSVBsasJgcHIr2y9EvwgowdVXR7oc1/tLc5KpW8TMIi8
RpFvuvzmrQ==</P>
<Q>2XFF/B9pGdpSqmw+eSU1IHUaqQo6Sa9J2/F7dIs+/GMhC/TRZ5SHYLrfRrhEIGvIyG4CSeRTdChs
tnHve3NR6Q==</Q>
<DP>FzDaPLCHNzk7Mq7xQjQTVKsk5wp2fawsOwS1IbiPrYS8B3rJhb/0+U2pjGNWH5lLaxY9RTIO4D8W
pqCpWtwCGQ==</DP>
<DQ>poREYAt95Tp4S+KRBR0lTjHftMwMTxlZqoGHVXy9Q3J2gD1/j58C/WSeT/4Uze8816DHR+b+sdmz
RxSBnTx24Q==</DQ>
<InverseQ>2VY9lcVhg1CJ+b1UjM5tOZ0dZqP6pIBVfhEXWLwnfOJyFc7CHqkUIBwtb4VqvCbhi8ZPG56yUXpw
7MYMX17R3w==</InverseQ>
<D>J3OMy5vJJLpsxgTOqL4jXn9sXT7lRj4QvIY0zq8xfgPUbWhVpCzOwHLpi45ktJOUYEOb2nQZUcpN
qY/UJy35EF/hVv6QBNJ7NF8qx1mpqxCO+9x2lLrT5nFcV3C9O9rlsU3hfriNtuFgMLicM8Fcqfnf
Z08nS0FwRYQuyxwe8WE=</D>
</RSAParameters>