日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > C# >内容正文

C#

c#中使用openssl

發布時間:2024/7/19 C# 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 c#中使用openssl 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

近期使用加密狗對軟件進行加密,但是軟件用的c#,這就比較坑了,因為c#自帶的System.Security.Cryptography只支持c#格式的公鑰加密私鑰解密,而現在需要私鑰加密公鑰解密。于是網上進行資料查找,一堆資料說的都是使用BouncyCastle,但是。。。。。。沒有使用不同格式的密文啊喂,你們有考慮過c#的孤獨嗎?

經過對結構體的分析,發現還是有些共通的地方,比如要有Modulus和Exponent,當然是對公鑰來說,然后各種調用,生成公鑰,一波操作猛如虎,回頭一看二百五啊。。。。。。沒人跟我說密文格式的不同啊喂。又是一頓搜索,知道了c#的rsa密文是xml格式的,不是簡單的base64的,需要轉換,咋辦?我想了個辦法,先用c#自帶的加解密生成一個公私鑰對,然后就知道格式了啊對不對,按照這個格式,我將openssl里的公鑰拼湊進去,得到一個xml格式的公鑰,再通過c#自帶的解xml公鑰,得到rsa信息,再生成base64格式的公鑰串,啊哈,竟然成功了,神奇不?

xml公鑰格式大概是這樣的:

string strPubKey = "<RSAKeyValue><Modulus>" + str64Modulus + "</Modulus><Exponent>" + str64Exponent + "</Exponent></RSAKeyValue>";

轉換xml格式公鑰:

public static string FromXmlPublicKey(string xmlPublicKey)
? ? ? ? {
? ? ? ? ? ? string result = string.Empty;
? ? ? ? ? ? using (RSACryptoServiceProvider rsa = new RSACryptoServiceProvider())
? ? ? ? ? ? {
? ? ? ? ? ? ? ? rsa.FromXmlString(xmlPublicKey);
? ? ? ? ? ? ? ? RSAParameters p = rsa.ExportParameters(false);
? ? ? ? ? ? ? ? RsaKeyParameters keyParams = new RsaKeyParameters(
? ? ? ? ? ? ? ? ? ? false, new BigInteger(1, p.Modulus), new BigInteger(1, p.Exponent));
? ? ? ? ? ? ? ? SubjectPublicKeyInfo publicKeyInfo = SubjectPublicKeyInfoFactory.CreateSubjectPublicKeyInfo(keyParams);
? ? ? ? ? ? ? ? result = Convert.ToBase64String(publicKeyInfo.ToAsn1Object().GetEncoded());
? ? ? ? ? ? }
? ? ? ? ? ? return result;
? ? ? ? }

用公鑰解密:

public static string DecryptPublicKey(string publicKey, string data)
? ? ? ? {
? ? ? ? ? ? RsaKeyParameters publicKeyParam = (RsaKeyParameters)PublicKeyFactory.CreateKey(Convert.FromBase64String(publicKey));
? ? ? ? ? ? byte[] cipherbytes = Convert.FromBase64String(data);
? ? ? ? ? ? RsaEngine rsa = new RsaEngine();
? ? ? ? ? ? rsa.Init(false, publicKeyParam);
? ? ? ? ? ? cipherbytes = rsa.ProcessBlock(cipherbytes, 0, cipherbytes.Length);
? ? ? ? ? ? return Convert.ToBase64String(cipherbytes, 0, cipherbytes.Length);
? ? ? ? }

以上,問題解決,如果大家有更好的辦法,希望留言,沒有人寫相關的東西,所以我粗略的寫了點,不對的請指出,謝謝!

總結

以上是生活随笔為你收集整理的c#中使用openssl的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。