.NET 對接JAVA 使用Modulus,Exponent RSA 加密

來源:互聯網
上載者:User

標籤:技術   相關   Google   encoding   人生   nts   text   for   info   

最近有一個工作是需要把資料用RSA發送給Java

雖然一開始標準公開金鑰 net和Java  RSA填充的一些演算法不一樣 但是後來這個坑也補的差不多了

具體可以參考

http://www.cnblogs.com/dudu/p/csharp-openssl-encrypt-decrypt.html

但是別人給我直接一串10進位的數字

然後我又查了一些.net 有一個RSAParameters的類, 我嘗試把 modulus 轉成Base64 然後產生 RSAParameters對象 然後

使用RSACryptoServiceProvider.ImportParameters 雖然能加密成功 但是加密出來的密文 達到了400多位(正常的能被伺服器解析的密文是172位).

 

之後想自己寫一個RSA加密類出來在網上找了一下加密的公式

    BigInteger e = new BigInteger("65537", 10);      BigInteger n = new BigInteger(myModule, 10);       BigInteger m = new BigInteger(Encoding.Default.GetBytes("123456"));       var str = m.modPow(e, n).ToString();

這一次加密後的長度是172 但是伺服器還是解密失敗了

嘗試很多種辦法時候都不行的時候我開始懷疑我的程式員人生了,因為除了我還有其他部門對接,安卓部門對接是成功的.

被逼無奈我就去找了Java的代碼看能不能加密成功

最後用Java的方式加密成功了 Java用到了一個類

Cipher ciphe

然後通過百度去搜.net 有沒有這個類.並沒有找到相關的文檔

最後試了一下Google.果然找到了.

原文地址
http://stackoverflow.com/questions/22825663/cipher-selection-for-sslstream-in-net-4-5
下面貼出我修改過後的代碼

                byte[] publicKeyByte = Encoding.Default.GetBytes(Modulus);                byte[] exponentByte = Encoding.Default.GetBytes(Exponent);                UTF8Encoding ByteConverter = new UTF8Encoding();                string publicKeyString = System.Text.Encoding.Default.GetString(publicKeyByte);                string exponentString = System.Text.Encoding.Default.GetString(exponentByte);                BigInteger publicKeyBI = BigInteger.Parse(publicKeyString);                BigInteger exponentBI = BigInteger.Parse(exponentString);                byte[] publicKeyByte2 = publicKeyBI.ToByteArray();                byte[] exponentByte2 = exponentBI.ToByteArray();                if (publicKeyByte2.Length == 129) Array.Resize(ref publicKeyByte2, 128);                RSACryptoServiceProvider RSA = new RSACryptoServiceProvider();                RSAParameters RSAKeyInfo = new RSAParameters();                RSAKeyInfo.Modulus = publicKeyByte2.Reverse().ToArray();                RSAKeyInfo.Exponent = exponentByte2.Reverse().ToArray();                RSA.ImportParameters(RSAKeyInfo);                byte[] passwordByte = ByteConverter.GetBytes(EncryptString);             var   Ciphertext = Convert.ToBase64String(RSA.Encrypt(passwordByte, false));

完結 撒花 如果這篇文章協助到你了,希望你能在下方留言告訴我 謝謝

.NET 對接JAVA 使用Modulus,Exponent RSA 加密

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.