1

Following is my Java applet code

KeyFactory keyFactory = KeyFactory.getInstance("RSA");
byte[] privKeyBytes = loadPriavteKeyFromFile(fileName, new String(txtPassword.getPassword()));
PKCS8EncodedKeySpec privSpec = new PKCS8EncodedKeySpec(privKeyBytes);
RSAPrivateKey privKey = (RSAPrivateKey) keyFactory.generatePrivate(privSpec);
Cipher rsaCipher = Cipher.getInstance("RSA");
rsaCipher.init(Cipher.ENCRYPT_MODE, privKey);
byte[] ciphertext = null;
ciphertext = rsaCipher.doFinal(xmlToSign.getBytes());
String urlString = "http://localhost:3290/SignApplet.aspx";
String senddata  = Base64.encodeBase64String(ciphertext);
doHttpUrlConnectionAction(urlString,senddata.getBytes());
JOptionPane.showMessageDialog(this, "XML successfully signed and sent to server.");

on the server side i am trying too decrypt the byte using the public key

byte[] b;
b = Request.BinaryRead(178);
string encodedbytes = new System.Text.UTF8Encoding().GetString(b);
b = Convert.FromBase64String(encodedbytes);
Debug.WriteLine("decrypted bytes:" + new System.Text.UTF8Encoding().GetString(b));
// The path to the certificate.
string Certificate = @"c:\certs\lafa801114sd3.cer";

//// Load the certificate into an X509Certificate object.
X509Certificate cert = new X509Certificate(Certificate);
RSACryptoServiceProvider publicprovider = (RSACryptoServiceProvider)CertUtil.GetCertPublicKey(cert);
byte[] decbytes = publicprovider.Decrypt(b, false);
Debug.WriteLine("decrypted bytes" + new System.Text.UTF8Encoding().GetString(decbytes));

can any one help in following exception which i am getting at byte[] decbytes = publicprovider.Decrypt(b, false); line

A first chance exception of type 'System.Security.Cryptography.CryptographicException' occurred in mscorlib.dll Key does not exist.

and the certificate is not installed in nay key store.Also i can successfully decrypt the data using Java servlet .

i am using asp.net vs2010 on windows 7 the public and private keys are stored in separate files

1
  • whay do i get exception that key does not exists? Commented Jan 22, 2011 at 14:28

1 Answer 1

2

Here are a few articles that might help you: Java RSA Encrypt - Decrypt .NET (which seems like what you are looking for) and http://www.jensign.com/JavaScience/dotnet/RSAEncrypt/

Sign up to request clarification or add additional context in comments.

6 Comments

ok loaded the certificate file i get null for cert.CspKeyContainerInfo.KeyContainerName any ideas
I dont know what you are looking for. I dont know the code that you have now. :) I would recommend getting the samples in the second example working with your key and then integrating it within your sources after that. It is hard to understand the issue just with a single line. :)
I am talking about the very first in the Decrypt code which states const string ContainerName = "{myCryptoAPIkeycontainername}" ; in link jensign.com/JavaScience/dotnet/RSAEncrypt the issue is that i cannot figure out what is the container name that I have to mention also when i load the the Certificate i and access the key-container (cert.CspKeyContainerInfo.KeyContainerName) property of certificate i get null.
I cannot understand why do i get exception Key does not exists when i try to decrypt the binary data
This link msdn.microsoft.com/en-us/library/tswxhw92.aspx explains how the key container it talks about. It is the place to store the private key which you had used to encrypt the data. BTW: YOu should also see the other link from stackoverflow. That one is more clearer.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.