您好,登錄后才能下訂單哦!
這篇文章主要為大家展示了“C#RSA對(duì)接JAVA中RSA方式的示例分析”,內(nèi)容簡(jiǎn)而易懂,條理清晰,希望能夠幫助大家解決疑惑,下面讓小編帶領(lǐng)大家一起研究并學(xué)習(xí)一下“C#RSA對(duì)接JAVA中RSA方式的示例分析”這篇文章吧。
C#中通過FromXmlString屬性加載的是XML形式,而JAVA中用到的是解析后的PEM格式的字符串,總之讀取證書中信息無(wú)非是轉(zhuǎn)換方式問題
/// <summary> /// c# 使用 java 的公鑰進(jìn)行rsa加密 utf8編碼 通過解析公鑰加密數(shù)據(jù) /// </summary> /// <param name="publickey"></param> /// <returns></returns> public static string RSAEncrypts(string content) { RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); byte[] cipherbytes; X509Certificate2 x509Certificate2 = new X509Certificate2("D:\\Config\\dsptest.cer"); //創(chuàng)建并返回當(dāng)前算法對(duì)象的xml字符串表示形式 string publicKeyString = x509Certificate2.PublicKey.Key.ToXmlString(false); rsa.FromXmlString(publicKeyString); //rsa.FromXmlString(RSAPublicKeyJava2DotNet(publicKeyString)); cipherbytes = rsa.Encrypt(Encoding.UTF8.GetBytes(content), false); return Convert.ToBase64String(cipherbytes); } /// <summary> /// 私鑰解密 通過解析私鑰來(lái)解析數(shù)據(jù) X509KeyStorageFlags.Exportable這個(gè)屬性很關(guān)鍵是獲取私鑰信息的 /// </summary> /// <param name="privatekey"></param> /// <param name="content"></param> /// <returns></returns> public static string RSADecrypt( string content) { byte[] rgb = Convert.FromBase64String(content); RSACryptoServiceProvider rsa = new RSACryptoServiceProvider(); byte[] cipherbytes; X509Certificate2 x509Certificate2 = new X509Certificate2("D:\\Config\\test.pfx", "cfca1234", X509KeyStorageFlags.Exportable); //創(chuàng)建并返回當(dāng)前算法對(duì)象的xml字符串表示形式 //rsa.FromXmlString(RSAPrivateKeyJava2DotNet(Const.privatekey)); rsa.FromXmlString(x509Certificate2.PrivateKey.ToXmlString(true)); cipherbytes = rsa.Decrypt(Convert.FromBase64String(content), false); return Encoding.UTF8.GetString(cipherbytes); }
/***************以下是java提供的公私鑰轉(zhuǎn)成.net公私鑰XML的方式*************************************/ /// <summary> /// RSA公鑰格式轉(zhuǎn)換,java->.net /// </summary> /// <param name="publicKey">java生成的公鑰</param> /// <returns></returns> public static string RSAPublicKeyJava2DotNet(string publicKey) { RsaKeyParameters publicKeyParam = (RsaKeyParameters)PublicKeyFactory.CreateKey(Convert.FromBase64String(publicKey)); return string.Format("<RSAKeyValue><Modulus>{0}</Modulus><Exponent>{1}</Exponent></RSAKeyValue>", Convert.ToBase64String(publicKeyParam.Modulus.ToByteArrayUnsigned()), Convert.ToBase64String(publicKeyParam.Exponent.ToByteArrayUnsigned())); } /// <summary> /// RSA私鑰格式轉(zhuǎn)換,java->.net /// </summary> /// <param name="privateKey">java生成的RSA私鑰</param> /// <returns></returns> public static string RSAPrivateKeyJava2DotNet(string privateKey) { RsaPrivateCrtKeyParameters privateKeyParam = (RsaPrivateCrtKeyParameters)PrivateKeyFactory.CreateKey(Convert.FromBase64String(privateKey)); return string.Format("<RSAKeyValue><Modulus>{0}</Modulus><Exponent>{1}</Exponent><P>{2}</P><Q>{3}</Q><DP>{4}</DP><DQ>{5}</DQ><InverseQ>{6}</InverseQ><D>{7}</D></RSAKeyValue>", Convert.ToBase64String(privateKeyParam.Modulus.ToByteArrayUnsigned()), Convert.ToBase64String(privateKeyParam.PublicExponent.ToByteArrayUnsigned()), Convert.ToBase64String(privateKeyParam.P.ToByteArrayUnsigned()), Convert.ToBase64String(privateKeyParam.Q.ToByteArrayUnsigned()), Convert.ToBase64String(privateKeyParam.DP.ToByteArrayUnsigned()), Convert.ToBase64String(privateKeyParam.DQ.ToByteArrayUnsigned()), Convert.ToBase64String(privateKeyParam.QInv.ToByteArrayUnsigned()), Convert.ToBase64String(privateKeyParam.Exponent.ToByteArrayUnsigned())); }
以上是“C#RSA對(duì)接JAVA中RSA方式的示例分析”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注億速云行業(yè)資訊頻道!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如果涉及侵權(quán)請(qǐng)聯(lián)系站長(zhǎng)郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。