標籤:技術 相關 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 加密