I have a web site uses RSA for encrypt some data. when i run it on local host, the RSA function works correctly. But after publishing my web site the function gives me an error. the function call is
Cryptography encryptor = new Cryptography();
string pkpath = Server.MapPath("publickey.xml");
byte[] RSAcipher = encryptor.RSAEncryptData(RSAplain, pkpath);
the stack trace of the error is
Server Error in '/' Application.<br />
Object already exists.<br />
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. <br />
<br />
Exception Details: System.Security.Cryptography.CryptographicException: Object already exists.<br />
<br />
<br />
Source Error: <br />
<br />
An unhandled exception was generated during the execution of the current web request. Information regarding the origin and location of the exception can be identified using the exception stack trace below.<br />
<br />
Stack Trace: <br />
<br />
<br />
[CryptographicException: Object already exists.<br />
]<br />
System.Security.Cryptography.CryptographicException.ThrowCryptographicException(Int32 hr) +36<br />
System.Security.Cryptography.Utils._CreateCSP(CspParameters param, Boolean randomKeyContainer, SafeProvHandle& hProv) +0<br />
System.Security.Cryptography.Utils.CreateProvHandle(CspParameters parameters, Boolean randomKeyContainer) +234<br />
System.Security.Cryptography.Utils.GetKeyPairHelper(CspAlgorithmType keyType, CspParameters parameters, Boolean randomKeyContainer, Int32 dwKeySize, SafeProvHandle& safeProvHandle, SafeKeyHandle& safeKeyHandle) +69<br />
System.Security.Cryptography.RSACryptoServiceProvider.GetKeyPair() +92<br />
System.Security.Cryptography.RSACryptoServiceProvider..ctor(Int32 dwKeySize, CspParameters parameters, Boolean useDefaultKeySize) +173<br />
Cryptography.AssignParameter() +109<br />
Cryptography.RSADecryptData(Byte[] data2Decrypt, String prpath) +12<br />
CryptoStegoApp.mywebapp.extbtn_Click(Object sender, EventArgs e) +546<br />
System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9815014<br />
System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +204<br />
System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +12<br />
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +15<br />
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35<br />
System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1639
What I have tried:
and the RSAEncryptData() function code is
public void AssignParameter()
{
const int PROVIDER_RSA_FULL = 1;
const string CONTAINER_NAME = "SpiderContainer";
CspParameters cspParams;
cspParams = new CspParameters(PROVIDER_RSA_FULL);
cspParams.KeyContainerName = CONTAINER_NAME;
cspParams.Flags = CspProviderFlags.UseMachineKeyStore;
cspParams.ProviderName = "Microsoft Strong Cryptographic Provider";
rsa = new RSACryptoServiceProvider(cspParams);
}
public byte[] RSAEncryptData(byte[] data2Encrypt,string pkpath)
{
AssignParameter();
StreamReader reader = new StreamReader(@pkpath);
string publicOnlyKeyXML = reader.ReadToEnd();
rsa.FromXmlString(publicOnlyKeyXML);
reader.Close();
byte[] cipherbytes = rsa.Encrypt(data2Encrypt, false);
return cipherbytes;
}
public byte[] RSADecryptData(byte[] data2Decrypt,string prpath)
{
AssignParameter();
StreamReader reader = new StreamReader(@prpath);
string publicPrivateKeyXML = reader.ReadToEnd();
rsa.FromXmlString(publicPrivateKeyXML);
reader.Close();
byte[] plain = rsa.Decrypt(data2Decrypt, false);
return plain;
}