2010-07-19 45 views
3

我想嵌入一些C#.Net代碼,在網頁中執行一些簡單的加密/解密函數。這將是一個內部網頁,因此用戶將被隱式信任。有沒有辦法做到這一點?我將需要擊中用戶的Windows-MY密鑰存儲區(通過CAPI)以提取解密密鑰,並且擊中LDAP服務器以獲取用於加密的公鑰。嵌入.Net C#在網頁?

回答

1

我最終用C#僞造了一個COM對象,然後使用JavaScript調用該COM對象,並且能夠通過瀏覽器與CAPI進行交互。

的JavaScript:

<html> 
<head> 
<script language="javascript"> 
var keystore = new ActiveXObject("RBCrypto.KeyStore"); 

function getCertList() 
{ 
    try { 
    keystore.openKeyStore("MY", true, false); 
    var size = keystore.getStoreSize(); 

    var list = document.getElementById('list'); 
    list.size = size; 

    for(var i = 0; i < size; i++) 
    { 
     var fname = keystore.getFriendlyName(i, true); 
     var opt = new Option(fname, fname); 
     list.options.add(opt); 
    } 
    } 
    catch(err) 
    { 
    alert(err.description); 
    } 
} 
</script> 
</head> 
<body onload="getCertList()"> 
    <center> 
    <h2>KeyStore Test</h2> 
    <hr /> 
    <br /> 
    <select id="list"></select> 
    </center> 
</body> 
</html> 

C#:

using System; 
using System.Runtime.InteropServices; 
using System.Security; 
using System.Security.Cryptography; 
using System.Security.Cryptography.X509Certificates; 

namespace RBCrypto 
{ 
    public interface AXInterface 
    { 
     void openKeyStore(string storeName, bool currentUser, bool readOnly); 
     int getStoreSize(); 
     string getFriendlyName(int index, bool subjectNameIfEmpty); 
    } 

    [ClassInterface(ClassInterfaceType.AutoDual)] 
    public class KeyStore :AXInterface 
    { 
     public void openKeyStore(string storeName, bool currentUser, bool readOnly) 
     { 
      if (keystoreInitialized) 
       throw new Exception("Key Store must be closed before re-initialization"); 

      try 
      { 
       if (currentUser) //user wants to open store used by the current user 
        certificateStore = new X509Store(storeName, StoreLocation.CurrentUser); 
       else    //user wants to open store used by local machine 
        certificateStore = new X509Store(storeName, StoreLocation.LocalMachine); 

       if (readOnly) 
        certificateStore.Open(OpenFlags.ReadOnly); 
       else 
        certificateStore.Open(OpenFlags.ReadWrite); 

       allCertificates = certificateStore.Certificates; 

       if (allCertificates == null) 
       { 
        certificateStore.Close(); 
        throw new NullReferenceException("Certificates could not be gathered"); 
       } 

       keystoreInitialized = true; 
      } 
      catch (ArgumentException ae) 
      { 
       throw ae; 
      } 
      catch (SecurityException se) 
      { 
       throw se; 
      } 
      catch (CryptographicException ce) 
      { 
       throw ce; 
      } 
      catch (NullReferenceException ne) 
      { 
       throw ne; 
      } 
     } 

     .... 
    } 
} 

C#程序集信息:

// Setting ComVisible to false makes the types in this assembly not visible 
// to COM components. If you need to access a type in this assembly from 
// COM, set the ComVisible attribute to true on that type. 
[assembly: ComVisible(true)] 

爲了這個工作,用戶必須6.在安裝他們的機器上您的.dll(確保你指定在您的安裝程序中將.dll註冊爲vsdraCOM),並且他們必須將您的網站添加到他們的truste d站點。

4

您可以使用Silverlight。

但是請注意,你可以在Javascript中做加密,以及:

+0

我想使用已存在於用戶的Windows-MY密鑰存儲區中的密鑰,以及從哪裏讀取Siverlight和Javascript都不能訪問它們 – 2010-07-19 19:07:18

+0

@Petey:那麼您需要編寫一個瀏覽器插件。 – SLaks 2010-07-19 19:42:40

+0

感謝SLaks(這應該是Ex Lax上的遊戲?),我應該從哪裏開始尋找關於瀏覽器插件的信息? – 2010-07-19 19:56:30

4

定義「進入網頁」的含義?網頁由瀏覽器運行,通常只有Javascript(和Java)。

您可以將它作爲Silverlight應用程序來執行。

+0

Silverlight沒有我需要的加密功能。除非有辦法擊中我發現的Windows-MY密鑰庫? – 2010-07-19 19:10:35

1

考慮編寫一個新的ASP.NET應用程序,其中加密/解密邏輯位於應用程序中。也許創建一個新的webforms應用程序,其中有一個專門用於填充這些請求的頁面。

考慮在單獨的.NET程序集中編寫加密邏輯,然後從ASP.NET應用程序中引用該程序集。

目前尚不清楚您是否希望將其作爲服務,或者用戶是否希望在文本框中輸入文本,並讓它在訪問時執行加密。

+0

這將工作,但我需要一種方法來從他們的Windows-MY密鑰庫中獲取用戶的私鑰以解密。其他一切都可以在服務器端完成。 – 2010-07-19 19:13:52

0

您可以使用AJAX並調用您在網絡上使用的加密函數。