在asp.net中解密字节

时间:2011-01-22 13:14:21

标签: asp.net visual-studio-2010 cryptography applet encryption

以下是我的Java小程序代码

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.");

在服务器端我尝试使用公钥解密字节

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));

任何人都可以帮助我跟踪我在byte[] decbytes = publicprovider.Decrypt(b, false);

获得的异常
  

mscorlib.dll中出现'System.Security.Cryptography.CryptographicException'类型的第一次机会异常   密钥不存在。

并且证书没有安装在nay密钥库中。我也可以使用Java servlet成功解密数据。

我在Windows 7上使用asp.net vs2010 公钥和私钥存储在单独的文件中

1 个答案:

答案 0 :(得分:2)

以下是一些可能对您有所帮助的文章:Java RSA Encrypt - Decrypt .NET(看起来就像您要找的那样) 和http://www.jensign.com/JavaScience/dotnet/RSAEncrypt/