Hello all,
I've been trying to get a public key solution working but have been having a
few problems. For starters there is a lot of contradictory information out
there, MSDN is not much help, and a lot of the sample code available I have
found are buggy so don't provide much insight.
Unfortunately for me every developer I know either gives me a completely
blank look when I try to talk crypto, or they have some wildly inaccurate
and insecure views on how to use it (with no working code to back them up).
Anyway, I am hoping somebody here can shed some light on the class I have
attached. The code is a little rough, and I've cut some bits out so I hope I
got the right bits and the code still works. I seem to be able to encrypt
the string I pass in ok (although how do you tell?) but when I decrypt I get
the infamous "bad data" error. Maybe I've been staring at the screen too
long but I can't see the problem.
Oh yeah, another thing I have found - some sites state the maximum block
size is keylength / 32, some say (keylength /8) - 11, and I've seen others.
I have tried them all and not sure what effect it is having. Any solid
information on this? I know RSA is for small messages but I still want to be
able to process as big a string as I want if need be.
If anybody can help me get this working I'd appreciate the effort.
Regards, and thanks
public class Asymmetric
{
public Asymmetric()
{
}
public class RSAProvider
{
public static RSACryptoServiceProvider GetCryptoProvider()
{
try
{
CspParameters cspParam = new CspParameters();
cspParam.Flags=CspProviderFlags.UseMachineKeyStore ;
return new RSACryptoServiceProvider(cspParam);
}
catch(Exception ex)
{
return null;
}
}
public static string GeneratePublicKey(RSACryptoServiceProvider
rsaCrypto)
{
try
{
if (rsaCrypto != null)
{
return rsaCrypto.ToXmlString(false);
}
return "";
}
catch(Exception ex)
{
return "";
}
}
public static string GeneratePublicKey()
{
RSACryptoServiceProvider rsaCrypto;
try
{
rsaCrypto = new RSACryptoServiceProvider();
if (rsaCrypto != null)
{
return rsaCrypto.ToXmlString(false);
}
return "";
}
catch(Exception ex)
{
return "";
}
}
public static string GeneratePrivateKey(RSACryptoServiceProvider
rsaCrypto)
{
try
{
if (rsaCrypto != null)
{
return rsaCrypto.ToXmlString(true);
}
return "";
}
catch(Exception ex)
{
return "";
}
}
public static string GeneratePrivateKey()
{
RSACryptoServiceProvider rsaCrypto;
try
{
rsaCrypto = new RSACryptoServiceProvider();
if (rsaCrypto != null)
{
return rsaCrypto.ToXmlString(true);
}
return "";
}
catch(Exception ex)
{
return "";
}
}
}
public static string EncryptString(string strPublicKey, string
strClearText)
{
string strCipherText="";
string strTemp = "";
string strOriginal = "";
RSACryptoServiceProvider rsaCrypto=null;
try
{
rsaCrypto = RSAProvider.GetCryptoProvider();
if (rsaCrypto != null)
{
strOriginal=strClearText;
rsaCrypto.FromXmlString(strPublicKey);
Int32 intBlockSize = rsaCrypto.KeySize / 8; // - 11;
while(strClearText.Length 0)
{
if (strClearText.Length intBlockSize)
{
strTemp=strClearText.Substring(0,intBlockSize);
strCipherText += EncryptBlock(strTemp, rsaCrypto);
strClearText=strClearText.Substring(intBlockSize);
}
else
{
strCipherText += EncryptBlock(strClearText, rsaCrypto);
strClearText="";
}
}
}
return strCipherText;
}
catch(Exception ex)
{
return "Failed to encrypt string: " + ex.Message;
}
}
private static string EncryptBlock(string strClearText,
RSACryptoServiceProvider rsaCrypto)
{
return
UnicodeEncoding.Default.GetString(rsaCrypto.Encryp t(UnicodeEncoding.Default.GetBytes(strClearText),
false));
}
public static string DecryptString(string strPrivateKey, string
strCipherText)
{
string strClearText = "";
string strTemp = "";
RSACryptoServiceProvider rsaCrypto=null;
try
{
rsaCrypto = RSAProvider.GetCryptoProvider();
if (rsaCrypto != null)
{
// Apply the decryption key to the crypto provider
rsaCrypto.FromXmlString(strPrivateKey);
Int32 intBlockSize = rsaCrypto.KeySize-11; // / 8;
while(strCipherText.Length 0)
{
if (strCipherText.Length intBlockSize)
{
strTemp=strCipherText.Substring(0,intBlockSize);
strClearText += DecryptBlock(strTemp, rsaCrypto);
strCipherText=strCipherText.Substring(intBlockSize );
}
else
{
strClearText += DecryptBlock(strCipherText, rsaCrypto);
strCipherText="";
}
}
}
return strClearText;
}
catch(Exception ex)
{
return "Failed to decrypt string: " + ex.Message;
}
}
private static string DecryptBlock(string strCipherText,
RSACryptoServiceProvider rsaCrypto)
{
return
UnicodeEncoding.Default.GetString(rsaCrypto.Decryp t(UnicodeEncoding.Default.GetBytes(strCipherText),
false));
}
}