解决打不开 RSA 密钥容器 即:加密web.config中的内容

简单的解决方法:

WebConfig 加解密,未能使用提供程序“RsaProtectedConfigurationProvider”进行解密。提供程序返回错误消息为: 打不开 RSA 密钥容器。
问题:未添加用于访问 RSA 密钥容器
命令:aspnet_regiis -pa "NetFrameworkConfigurationKey" "NT AUTHORITY/NETWORK SERVICE"
注意事项:XP下:aspnet_regiis -pa "NetFrameworkConfigurationKey" "aspnet"
加密:aspnet_regiis -pe "appSettings" -app "/应用程序名"
解密:aspnet_regiis -pd "appSettings" -app "/应用程序名"  如(/PetShop/web)

更灵活的解决方法:
1、创建一个密钥容器 
   aspnet_regiis -pc "ConnectionStringsKey" -exp
   ConnectionStringsKey为密钥容器的名称 
   可以使用aspnet_regiis /?查看该命令的用法

2、在web.config中加入如下内容<textarea cols="90" rows="11" name="code" class="c-sharp:nogutter">&lt;configProtectedData&gt; &lt;providers&gt; &lt;clear /&gt; &lt;add name="ConnectionStringsKeyProvider"       type="System.Configuration.RsaProtectedConfigurationProvider, System.Configuration, Version=2.0.0.0,Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a, processorArchitecture=MSIL"       keyContainerName="ConnectionStringsKey"       useMachineContainer="true"/&gt; &lt;/providers&gt; &lt;/configProtectedData&gt;</textarea> 
3、通过命令行:用指定的密钥加密指定目录下的web.config文件的指定的配置节
     aspnet_regiis -pef "connectionStrings" "d:/testproj/websitetest" -prov "ConnectionStringsKeyProvider"
    对于子配置节用/分隔表示, 如identity配置节 需要写成 "system.web/identity"
4、如果访问web程序,页面提示 Error message from the provider: The RSA key container could not be opened.
     是由于network service帐户无法访问密钥文件造成的。 找到密钥文件, 赋予network service读权限。该密钥文件位于(可按时间排序,找到自己产生的那个密钥文件)
vista: c:/ProgramData/Microsoft/Crypto/RSA/MachineKeys/
xp或其他:C:/Documents and Settings/All Users/Application Data/Microsoft/Crypto/RSA/MachineKeys

至此:查看被加密的标记, 内容就已经是被加密过的了。

5.通过.aspx页面:加密连接字符串:界面如图:

截图06

后台代码: <textarea cols="88" rows="13" name="code" class="c-sharp:nogutter">//加密按钮 protected void Button1_Click(object sender, EventArgs e) { //①需要加密的节点: string name = @"connectionStrings"; //②当前路径; string appPath = "/loginContral"; Configuration config = WebConfigurationManager.OpenWebConfiguration(appPath); //③提供加密的方式:(这里使用自定义的加密方式) // string provider = "RsaProtectConfigurationProvider"; string provider = "ConnectionStringsKeyProvider"; config.GetSection(name).SectionInformation.ProtectSection(provider); //⑤保存web.config文件 try { config.Save(); } catch (Exception ex) { Response.Write(ex.Message); } if (config.GetSection(name).SectionInformation.IsProtected) { Button1.Enabled = false; Response.Write("加密成功!"); } else { Response.Write("加密失败!"); } } //解密按钮: protected void Button2_Click(object sender, EventArgs e) { //①需要节密的节点: string name = @"connectionStrings"; //②当前路径; string appPath = "/loginContral"; Configuration config = WebConfigurationManager.OpenWebConfiguration(appPath); //③使用UnprotectSection方法进行解密; config.GetSection(name).SectionInformation.UnprotectSection(); //④保存web.config文件 config.Save(); if (config.GetSection(name).SectionInformation.IsProtected==false) { Button2.Enabled = false; Response.Write("解密成功!"); } else { Response.Write("解密失败!"); } } </textarea>

注意:string appPath = "/loginContral" 为当前项目路径;

截图07

加密前的连接字符串:<textarea cols="89" rows="5" name="code" class="c-sharp:nogutter"> &lt;connectionStrings&gt; &lt;add name="connection" connectionString="data source=.;database=aspnetdb;user id=sa;pwd=123;" /&gt; &lt;/connectionStrings&gt; </textarea>     
加密后的连接字符串:

<textarea cols="89" rows="15" name="code" class="c-sharp:nogutter"> &lt;connectionStrings configProtectionProvider="ConnectionStringsKeyProvider"&gt; &lt;EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element" xmlns="http://www.w3.org/2001/04/xmlenc#"&gt; &lt;EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" /&gt; &lt;KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#"&gt; &lt;EncryptedKey xmlns="http://www.w3.org/2001/04/xmlenc#"&gt; &lt;EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" /&gt; &lt;KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#"&gt; &lt;KeyName&gt;Rsa Key&lt;/KeyName&gt; &lt;/KeyInfo&gt; &lt;CipherData&gt; &lt;CipherValue&gt;AepogG4vVhd8K6NVhVmdO8FAGFMopOdDvnBN5vPV0mxP8NgrImnZFvflrhhvooiu56McmMr6n5cUnixzimGB/zTgCNMsIkU8Sr6YtX8iUh64U9IVujwaOAbtZp4AhLhMiH6YwkHXjmqrjYyS2ecsocquZQ0ndkKC3OMg/UcOIk0=&lt;/CipherValue&gt; &lt;/CipherData&gt; &lt;/EncryptedKey&gt; &lt;/KeyInfo&gt; &lt;CipherData&gt; &lt;CipherValue&gt;biMAH/6vwvi0FKvqijpSZzKhk+a6QNi0Aa794yxi1X+sffKdtsUR15hVcByOlborcKPRhX94MpOm2eKoBqYVyCf24PdYAkIFFAzO1sluzmUtcXFVU/lTBqn83bnJDgBgo6eVtDg4m7DSAVR6qWyEP8wySqWWuBkWSLzsMynqPOyGhVB9bTVJbSCWiUZ4ynFhvUTziGISJQA=&lt;/CipherValue&gt; &lt;/CipherData&gt; &lt;/EncryptedData&gt; &lt;/connectionStrings&gt;</textarea> 
其他备用操作:
1、解密web.config 
    aspnet_regiis -pdf "connectionStrings" "d:/testproj/websitetest"
2、把密钥容器导出为xml文件 
    aspnet_regiis -px "ConnectionStringsKey" "c:/Key.xml"这个命令只导出公钥,因此以后只能用于加密,而无法解密。
   
aspnet_regiis -px "ConnectionStringsKey" "c:/Keys.xml" -pri  这个则连私钥一起导出了,所以我们要用这个
3、把密钥容器删除  
   aspnet_regiis -pz "LixinKey"   删除后再运行程序,会提示出错: 
    分析器错误信息: 未能使用提供程序“LixinKeyProvider”进行解密。提供程序返回错误信息为: 打不开 RSA 密钥容器。 
    同理可以证明,在任何一台未安装正确的密钥容器LixinKey的机器上,程序都无法对connectionStrings节进行解密,因此也就无 法正常运行。
4、导入key.xml文件 
     aspnet_regiis -pi "LixinKey" "c:/Keys.xml"

     此时,再运行程序会发现又可以解密了。证明加密与解密机制运行正常。
最后说一下这个机制所提供的安全性保障可以运用在什么方面:
1. 对winform程序的app.config进行加密实际意义并不大,因为无论如何,客户机都可以通过运行aspnet_regiis -pdf 来对配置文件进行解密,从而暴露敏感信息。
2. 对于web.config进行加密的意义也仅限于,当web.config文件不小心泄露时,不会同时泄露敏感信息,如果恶意攻击者已经取得了在服务器上运行程序的权限,那么同app.config一样,可以很容易通过通过运行aspnet_regiis -pdf 获取明文了。
3. 还有,通过aspnet_regiis -pa "Key" "NT AUTHORITY/NETWORK SERVICE"控制对不同用户对密钥容器的访问权限,应该还可以进一步获取一些安全性,比如可以控制某些用户即使登录到服务器上,也无法用aspnet_regiis -pdf对配置文件进行解密。

 

转载于:https://my.oschina.net/zhangqs008/blog/712747

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
JMeter支持RSA加密算法,可以使用Java Cryptography Extension (JCE)提供的RSA算法。以下是使用JMeter进行RSA加密的步骤: 1. 首先,需要将公文件导入到JMeter。在JMeter的“Test Plan”,右键单击“Add”->“Config Element”->“Keystore Configuration”,然后在“Keystore Configuration”选择“JKS”作为“Keystore Type”,并指定公文件的路径和码。 2. 接下来,在JMeter的“Test Plan”,右键单击“Add”->“Sampler”->“Debug Sampler”,然后在“Debug Sampler”输入要加密的明文。 3. 在“Debug Sampler”,添加一个“JSR223 Sampler”,并在“Script Language”选择“groovy”。然后在“Script”输入以下代码: ```groovy import java.security.KeyFactory; import java.security.spec.RSAPublicKeySpec; import javax.crypto.Cipher; import java.nio.charset.StandardCharsets; import java.util.Base64; String publicKey = vars.get("publicKey"); // 从变量获取公 String plainText = vars.get("plainText"); // 从变量获取明文 byte[] publicKeyBytes = Base64.getDecoder().decode(publicKey); // 将公字符串解码为字节数组 RSAPublicKeySpec keySpec = new RSAPublicKeySpec(publicKeyBytes, new byte[]{1,0,1}); // 构造公规范 KeyFactory keyFactory = KeyFactory.getInstance("RSA"); // 获取RSA工厂 Cipher cipher = Cipher.getInstance("RSA/ECB/PKCS1Padding"); // 获取RSA加密器 cipher.init(Cipher.ENCRYPT_MODE, keyFactory.generatePublic(keySpec)); // 初始化加密器 byte[] encryptedBytes = cipher.doFinal(plainText.getBytes(StandardCharsets.UTF_8)); // 加密明文 String encryptedText = Base64.getEncoder().encodeToString(encryptedBytes); // 将加密后的字节数组编码为字符串 vars.put("encryptedText", encryptedText); // 将加密后的文保存到变量 ``` 4. 在“JSR223 Sampler”,添加一个“View Results Tree”,并运行测试计划。在“View Results Tree”,可以查看加密后的文。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值