【发布时间】:2011-03-18 01:21:23
【问题描述】:
我想在网页中嵌入一些执行一些简单加密/解密功能的 C# .Net 代码。这将是一个内部网页,因此用户将被隐式信任。有没有办法做到这一点?我将需要访问用户的 Windows-MY 密钥存储(通过 CAPI)以提取密钥进行解密并访问 LDAP 服务器以获取公钥进行加密。
【问题讨论】:
标签: c# web-applications embed
我想在网页中嵌入一些执行一些简单加密/解密功能的 C# .Net 代码。这将是一个内部网页,因此用户将被隐式信任。有没有办法做到这一点?我将需要访问用户的 Windows-MY 密钥存储(通过 CAPI)以提取密钥进行解密并访问 LDAP 服务器以获取公钥进行加密。
【问题讨论】:
标签: c# web-applications embed
您可以使用 Silverlight。
但是请注意,您也可以在 Javascript 中进行加密:
【讨论】:
定义“进入网页”是什么意思?网页由浏览器运行,这些浏览器通常只知道 Javascript(和 Java)。
您可以将其作为 Silverlight 应用程序来实现。
【讨论】:
Silverlight 或者可能是 c# 到 JavaScript 的编译器,例如 Script#。
【讨论】:
考虑编写一个新的 ASP.NET 应用程序,您的加密/解密逻辑位于应用程序中。或许可以创建一个新的网络表单应用程序,其中包含一个专门用于处理这些请求的页面。
考虑在单独的 .NET 程序集中编写该加密逻辑,然后从您的 ASP.NET 应用程序中引用该程序集。
尚不清楚您是否希望将此作为一项服务,或者用户是否希望在文本框中输入文本,并在访问时让它执行加密。
【讨论】:
我最终使用 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)]
为了使其工作,用户必须在他们的机器上安装您的 .dll(确保您在安装程序中指定将您的 .dll 注册为 vsdraCOM),并且他们必须将您的站点添加到他们信任的站点。
【讨论】:
您可以使用 AJAX 并通过网络调用您使用过的加密函数。
【讨论】: