RSACryptoServiceProvider only decrypts after re-import of parameters

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
I am trying to use the Windows certificate store to decrypt a piece of data that is coming from a Third Party java application.
If I hard code the values exported from the RSAParameters object and Import the private key string its works fine but it would be much better if I utilise the below code to take it from the store to the certificate is stored securely.
So I have created the below sample project. If I create an RSA object and import the parameters exported from the certificate store to decrypts, but if i try to use the certificate store directly it fails.


<div style="color:Black;background-color:White; <pre>
<span style="color:Blue; static <span style="color:Blue; void Main(<span style="color:Blue; string[] args)
{
<span style="color:Blue; string decrypted = <span style="color:#A31515; "1234567812345678";

<span style="color:Blue; byte[] encrypted = EncryptWithCert(decrypted);
<span style="color:Blue; string result = DecryptWithCert(encrypted);
result = DecryptWithCertValues(encrypted);

}

<span style="color:Blue; public <span style="color:Blue; static <span style="color:Blue; byte[] EncryptWithCert(<span style="color:Blue; string message)
{
<span style="color:Blue; byte[] bytes = <span style="color:Blue; null;

<span style="color:Blue; var store = <span style="color:Blue; new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
<span style="color:Blue; var certReceiver = store.Certificates.Find(X509FindType.FindBySubjectName, <span style="color:#A31515; "G8wayFacadeMainCert", <span style="color:Blue; false)[0];

<span style="color:Gray; ///<span style="color:Green; /Initialize RSACryptoServiceProvider
RSACryptoServiceProvider oRSA = (RSACryptoServiceProvider)certReceiver.PublicKey.Key;
bytes = oRSA.Encrypt(System.Text.ASCIIEncoding.ASCII.GetBytes(message), <span style="color:Blue; false);

<span style="color:Blue; return bytes;
}

<span style="color:Blue; public <span style="color:Blue; static <span style="color:Blue; string DecryptWithCert(<span style="color:Blue; byte[] encryptedString)
{
<span style="color:Blue; string result = <span style="color:Blue; string.Empty;
<span style="color:Blue; byte[] bytes = <span style="color:Blue; null;

<span style="color:Blue; try
{
<span style="color:Blue; var store = <span style="color:Blue; new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
<span style="color:Blue; var certReceiver = store.Certificates.Find(X509FindType.FindBySubjectName, <span style="color:#A31515; "G8wayFacadeMainCert", <span style="color:Blue; false)[0];

<span style="color:Gray; ///<span style="color:Green; /Initialize RSACryptoServiceProvider
RSACryptoServiceProvider oRSA = (RSACryptoServiceProvider)certReceiver.PrivateKey;
bytes = oRSA.Decrypt(encryptedString, <span style="color:Blue; false);
result = System.Text.ASCIIEncoding.ASCII.GetString(bytes);
}
<span style="color:Blue; catch { }

<span style="color:Blue; return result;
}

<span style="color:Blue; public <span style="color:Blue; static <span style="color:Blue; string DecryptWithCertValues(<span style="color:Blue; byte[] encryptedString)
{
<span style="color:Blue; string result = <span style="color:Blue; string.Empty;
<span style="color:Blue; byte[] bytes = <span style="color:Blue; null;

<span style="color:Blue; try
{
<span style="color:Blue; var store = <span style="color:Blue; new X509Store(StoreName.My, StoreLocation.CurrentUser);
store.Open(OpenFlags.ReadOnly | OpenFlags.OpenExistingOnly);
<span style="color:Blue; var certReceiver = store.Certificates.Find(X509FindType.FindBySubjectName, <span style="color:#A31515; "G8wayFacadeMainCert", <span style="color:Blue; false)[0];

<span style="color:Gray; ///<span style="color:Green; /Initialize RSACryptoServiceProvider
RSACryptoServiceProvider oRSA = (RSACryptoServiceProvider)certReceiver.PrivateKey;

RSAParameters param = oRSA.ExportParameters(<span style="color:Blue; true);

RSACryptoServiceProvider oRSA2 = <span style="color:Blue; new RSACryptoServiceProvider();
oRSA2.ImportParameters(param);
bytes = oRSA2.Decrypt(encryptedString, <span style="color:Blue; false);

result = System.Text.ASCIIEncoding.ASCII.GetString(bytes);
}
<span style="color:Blue; catch {}

<span style="color:Blue; return result;
}
[/code]
<br/>
<br/>


View the full article
 
Back
Top