开发者

Decrypt bytes in asp.net

开发者 https://www.devze.com 2023-02-06 17:10 出处:网络
Following is my Java applet code KeyFactory keyFactory = KeyFactory.getInstance(\"RSA\"); byte[] privKeyBytes = loadPriavteKeyFromFile(fileName, new String(txtPassword.getPassword()));

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.
X509Certif开发者_Python百科icate 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


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/

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号