.net認證Rsa加密

來源:互聯網
上載者:User

標籤:

參考:基於RSA演算法的ios用戶端加密和C#服務端解密的解決方案  

       NET和java的RSA互連,僅此而已

 1.產生認證

  開啟vs開發人員工具使用如下命令產生認證:

  makecert -ss My -n CN=Theoservice -sky exchange -pe

  Makecert命令的詳細說明請參看微軟Makecert.exe工具的文檔:https://msdn.microsoft.com/zh-cn/library/bfsktky3(v=vs.110).aspx

  然後,開始->運行->MMC,開啟MMC控制台。檔案->添加/刪除嵌入式管理單元->添加按鈕->選”認證”->添加->選”電腦賬戶”->關閉->確定,然後你就可以在 “個人->認證” 裡看到剛才產生的認證了。

  你可以匯出一份不帶私密金鑰的cer格式的認證。這張認證只含有公開金鑰,是用來和用戶端一起發布出去用來加密數資料的。

2.擷取:RSACryptoServiceProvider

 1             X509Store store = new X509Store(StoreLocation.CurrentUser); 2             store.Open(OpenFlags.ReadOnly); 3             X509Certificate2Collection certCollection = store.Certificates; 4             X509Certificate2 cert = null; 5  6             // Loop through each certificate and find the certificate  7             // with the appropriate name. 8             foreach (X509Certificate2 c in certCollection) 9             {10                 if (c.Subject == "CN=Theoservice")11                 {12                     cert = c;13 14                     break;15                 }16             }17             store.Close();18 19             //從認證中獲得含私密金鑰的RSACryptoServiceProvider20 21             RSACryptoServiceProvider provider = (RSACryptoServiceProvider)cert.PrivateKey;22             byte[] buff=Encoding.UTF8.GetBytes(source);23             /* 24              * 一次加密長度為 (KeySize/8)個byte。25              * 而C#預設採用#PKSC1的padding模式,每次最多可以加密 (KeySize/8-11) 個byte。26              * RSA區塊編碼器演算法每次從明文裡取<=(KeySize/8-11)個byte,然後加密成(KeySize/8)個byte的密文;27              * 解密的時候,每次就取(KeySize/8)個byte的密文,將其解密成<=(KeySize/8-11)個byte的明文。28              * 因為#PKSC1的padding模式每次padding的內容是隨機的,所以即使是加密同一段明文,每次的結果也不一樣,這大大的增加了資料安全性。29              */30             //加密31             provider.Encrypt(buff, false);32             //解密33             provider.Decrypt(buff, false);

註:1. 每次加密最大(KeySize/8-11)個byte,解密(KeySize/8)個byte

      2.加密後固定(KeySize/8)個byte

      3.KeySize=密鑰長度

.net認證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.