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

Abdul Khaliq
  • 2,423
  • 12
  • 40
  • 65

1 Answers1

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/

Community
  • 1
  • 1
gbvb
  • 866
  • 5
  • 10
  • ok loaded the certificate file i get null for cert.CspKeyContainerInfo.KeyContainerName any ideas – Abdul Khaliq Jan 23 '11 at 08:44
  • 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. :) – gbvb Jan 23 '11 at 15:59
  • I am talking about the very first in the Decrypt code which states const string ContainerName = "{myCryptoAPIkeycontainername}" ; in link http://www.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. – Abdul Khaliq Jan 26 '11 at 11:29
  • I cannot understand why do i get exception Key does not exists when i try to decrypt the binary data – Abdul Khaliq Jan 26 '11 at 11:42
  • This link http://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. – gbvb Jan 26 '11 at 12:11
  • hmm but how do i import the existing public private keys to a keycontainer – Abdul Khaliq Jan 27 '11 at 17:00