最近遇到一个项目需要做单点,用户方是采用java rsa公钥加密传递信息的,我这边是.net使用私钥解密。而对方提供的解密源码是java版本的,并且也没有做过与.net平台的单点对接。
于是在网上找到了如下版本的c# 版本rsa私钥解密方法
1 ///2 /// RSA的解密函数 3 /// 4 /// 私钥 5 /// 待解密的字符串 6 /// 7 public string RSADecrypt(string xmlPrivateKey, string decryptString) 8 { 9 byte[] PlainTextBArray;//解密前字节流 10 byte[] DypherTextBArray;//解密后字节流 11 string Result = ""; 12 System.Security.Cryptography.RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); 13 rsa.FromXmlString(xmlPrivateKey); 14 PlainTextBArray = Convert.FromBase64String(decryptString); 15 DypherTextBArray = rsa.Decrypt(PlainTextBArray, false); 16 Result = Encoding.UTF8.GetString(DypherTextBArray); 17 return Result; 18 }
拿过来之后把私钥和带解密字符串传入,发现报错,于是研究发现,c#的rsa解密私钥是xml格式的,而对方提供的是base64字符串,所以需要先把私钥转成xml格式,
转换无法在c#中进行,需要打开在java下面转换,可以下个exclipse把如下代码复制进去就行。
1 package com; 2 3 import java.security.KeyFactory; 4 import java.security.PublicKey; 5 import java.security.interfaces.RSAPrivateCrtKey; 6 import java.security.interfaces.RSAPublicKey; 7 import java.security.spec.PKCS8EncodedKeySpec; 8 import java.security.spec.X509EncodedKeySpec; 9 //import org.castor.util.Base64Decoder; 10 //import org.castor.util.Base64Encoder; 11 12 public class test { 13 14 public static void main(String[] args) 15 { 16 String tes="MIICeQIBADANBgkqhkiG9w0BAQEFAASCAmMwggJfAgEAAoGBAL8z2QlXCL6w7rvY0Gbl8ARtQSXY+pEW5hlUHlmspqHt4k8/SkoF796gDqk4yyOcoWhkZWLPPugK35Mn7V+m5Jyfu8C0gVKOfWOA8A0T4hxV2ThAoMUq7QtB2K6s9AoumrxDfAkMBbsXEHYwfD/hxr/3DQ3lUvSFB6BnhiHEOyzpAgMBAAECgYEAol/9qRjorEjF9XEjSr9rHddKxEGIST8RGeF+BNnCiTHkRziQdlykYIO876jzmsKhsG3STB+EZLsXM3ls9RZefcsPF5mLOCSOCow3DikfCtAy4hntsU9JwpuYE0V4A+Sgfd24fatqbu+JxE2nvpSbAPczDOgBFPNfYBkhMiuZ/iECQQDzUeq7lFcIE4uWhRGveVFjNAGuSsW+q9GOwO7tS5YwuAIQ2M+XgYGRFo8xMC6V/9SfqJtmSU1zk72pMlYufIqHAkEAySqkcKbWuobq5I9KSQISq2qCuGKtj/iUFho4PCD1YxhnQ7gcHA4OpS1dRFjtXJYQPTX9be+mmypsCFIyofE5DwJBAPGZ20wahTh9v9Lbmq3z9n5ce3bGxAcJsHDg3d09eooxi8uSnL5BV5frII+k2f0TI9rMnlE4Y/FpN5+zXaOXAi0CQQCs3Aqfjo23jJWtPv/LSo+2YnjfblPMAgNmFrO532xc8axSgZMN/HpTL28UewHD7GMZ5hnWbPcSIFrir5c4luq7AkEAi90WdnZVPxtSTqkkLYbnh4Ro2WhdwRjkfyBxBZZx8hfaM6MfLPi3A0rw9DPOSB4M/BMchtEh3bXuI7bue2tG+A=="; 17 byte[] temp=b64decode(tes); 18 String ver=getRSAPrivateKeyAsNetFormat(temp);//转换私钥 19 20 String tes1="MIGfMA0GCSqGSIb4DQEBAQUAA4GNADCBiQKBgQC/M9kJVwi+sO672NBm5fAEbUEl2PqRFuYZVB5ZrKah7eJPP0pKBe/eoA6pOMsjnKFoZGVizz7oCt+TJ+1fpuScn7vAtIFSjn1jgPANE+IcVdk4QKDFKu0LQdiurPQKLpq8Q3wJDAW7FxB2MHw/4ca/9w0N5VL0hQegZ4YhxDss6QIDAQAB"; 21 byte[] temp1=b64decode(tes1); 22 String ver1=getRSAPublicKeyAsNetFormat(temp1);//转换公钥 23 //String temp2= encodePublicKeyToXml(temp1); 24 25 } 26 27 private static String getRSAPrivateKeyAsNetFormat(byte[] encodedPrivkey) { 28 try { 29 StringBuffer buff = new StringBuffer(1024); 30 31 PKCS8EncodedKeySpec pvkKeySpec = new PKCS8EncodedKeySpec( 32 encodedPrivkey); 33 KeyFactory keyFactory = KeyFactory.getInstance("RSA"); 34 RSAPrivateCrtKey pvkKey = (RSAPrivateCrtKey) keyFactory 35 .generatePrivate(pvkKeySpec); 36 37 buff.append(""); 38 buff.append(" "); 70 71 return buff.toString().replaceAll("[ \t\n\r]", ""); 72 } catch (Exception e) { 73 System.err.println(e); 74 return null; 75 } 76 } 77 78 private static String getRSAPublicKeyAsNetFormat(byte[] encodedPrivkey) { 79 try { 80 StringBuffer buff = new StringBuffer(1024); 81 82 PKCS8EncodedKeySpec pvkKeySpec = new PKCS8EncodedKeySpec(encodedPrivkey); 83 KeyFactory keyFactory = KeyFactory.getInstance("RSA"); 84 RSAPublicKey pukKey=(RSAPublicKey) keyFactory.generatePublic(new X509EncodedKeySpec(encodedPrivkey)); 85 // RSAPrivateCrtKey pvkKey = (RSAPrivateCrtKey) keyFactory.generatePrivate(pvkKeySpec); 86 //PublicKey publicKey =KeyFactory.getInstance("RSA").generatePublic(pvkKeySpec); 87 buff.append("" 39 + b64encode(removeMSZero(pvkKey.getModulus().toByteArray())) 40 + " "); 41 42 buff.append("" 43 + b64encode(removeMSZero(pvkKey.getPublicExponent() 44 .toByteArray())) + " "); 45 46 buff.append("" 47 + b64encode(removeMSZero(pvkKey.getPrimeP().toByteArray())) 48 + "
"); 49 50 buff.append("" 51 + b64encode(removeMSZero(pvkKey.getPrimeQ().toByteArray())) 52 + ""); 53 54 buff.append("" 55 + b64encode(removeMSZero(pvkKey.getPrimeExponentP() 56 .toByteArray())) + " "); 57 58 buff.append("" 59 + b64encode(removeMSZero(pvkKey.getPrimeExponentQ() 60 .toByteArray())) + " "); 61 62 buff.append("" 63 + b64encode(removeMSZero(pvkKey.getCrtCoefficient() 64 .toByteArray())) + " "); 65 66 buff.append("" 67 + b64encode(removeMSZero(pvkKey.getPrivateExponent() 68 .toByteArray())) + " "); 69 buff.append(""); 88 buff.append(" "); 95 return buff.toString().replaceAll("[ \t\n\r]", ""); 96 } catch (Exception e) { 97 System.err.println(e); 98 return null; 99 } 100 } 101 public static String encodePublicKeyToXml(PublicKey key) { 102 if (!RSAPublicKey.class.isInstance(key)) { 103 return null; 104 } 105 RSAPublicKey pubKey = (RSAPublicKey) key; 106 StringBuilder sb = new StringBuilder(); 107 sb.append("" 89 + b64encode(removeMSZero(pukKey.getModulus().toByteArray())) 90 + " "); 91 buff.append("" 92 + b64encode(removeMSZero(pukKey.getPublicExponent() 93 .toByteArray())) + " "); 94 buff.append(""); 108 sb.append(" "); 115 return sb.toString(); 116 } 117 118 private static byte[] removeMSZero(byte[] data) { 119 byte[] data1; 120 int len = data.length; 121 if (data[0] == 0) { 122 data1 = new byte[data.length - 1]; 123 System.arraycopy(data, 1, data1, 0, len - 1); 124 } else 125 data1 = data; 126 return data1; 127 } 128 private static String b64encode(byte[] data) { 129 130 String b64str = new String(Base64.encode(data)); 131 return b64str; 132 } 133 134 private static byte[] b64decode(String data) { 135 byte[] decodeData = Base64.decode(data); 136 return decodeData; 137 } 138 }") 109 .append(Base64.encode(pubKey.getModulus().toByteArray())) 110 .append(" "); 111 sb.append("") 112 .append(Base64.encode(pubKey.getPublicExponent() 113 .toByteArray())).append(" "); 114 sb.append("
参考:http://www.cnblogs.com/hvaning/p/3636288.html
现在拿到c#的私钥了,发现解密还是报错,又看了下,发现用户给的加密文本是16进制文本,并不是base64文本,于是写了个16进制转字节的方法供调用。
16进制文本转字节流方法如下
1 ///2 /// 16进制文本转字节流 3 /// 4 /// 16进制文本 5 /// 6 public byte[] hexStr2ByteArr(string src) 7 { 8 int l = src.Length / 2;//2个16进制文本等于一个字节,所以字节数组长度是16进制文本长度的一半 9 String str; 10 byte[] ret = new byte[l]; 11 12 for (int i = 0; i < l; i++) 13 { 14 str = src.Substring(i * 2, 2); 15 ret[i] = Convert.ToByte(str, 16); 16 } 17 return ret; 18 }
然后下一个坑又出来了,rsa解密一次只能128个字节,所以又写了个循环的方法,每次解密128个字节数组,最终再拼接起来,最终代码如下:
1 ///2 /// RSA的解密函数 3 /// 4 /// 私钥 5 /// 待解密的字符串 6 /// 7 public string RSADecrypt(string xmlPrivateKey, string decryptString) 8 { 9 try 10 { 11 byte[] PlainTextBArray; 12 byte[] DypherTextBArray; 13 string Result = ""; 14 System.Security.Cryptography.RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); 15 rsa.FromXmlString(xmlPrivateKey); 16 PlainTextBArray = hexStr2ByteArr(decryptString); 17 var outlength = PlainTextBArray.Length; 18 var i = 0; 19 while (true) { 20 if (outlength > 128) 21 { 22 var aa = new byte[128]; 23 Array.Copy(PlainTextBArray, i, aa, 0, 128); 24 DypherTextBArray = rsa.Decrypt(aa, false); 25 Result = Result + Encoding.UTF8.GetString(DypherTextBArray); 26 outlength = outlength - 128; 27 i = i + 128; 28 } 29 else { 30 var aa = new byte[outlength]; 31 Array.Copy(PlainTextBArray, i, aa, 0, outlength); 32 DypherTextBArray = rsa.Decrypt(aa, false); 33 Result = Result + Encoding.UTF8.GetString(DypherTextBArray); 34 break; 35 } 36 } 37 return Result; 38 } 39 catch (Exception ex) 40 { 41 throw ex; 42 } 43 }