通过我的VIP算法在web.config中加密和解密ConnectionSting

时间:2018-09-19 07:04:31

标签: asp.net algorithm encryption database-connection

我需要使用我的算法在web.config中对ConnectionString进行加密/解密的任何方法,或者在connectionstring中需要对受保护的用户名和密码进行加密的任何方法

但是,不要像

那样使用加密/解密的旧习惯。
ASPNET_REGIIS -PEF "connectionStrings" "ProjectPath"

无需使用工具:开发人员命令提示符

我有一种使用加密和解密的算法:

var strEncrypt = EncryptionHelper.Encrypt("myConnectionString");
var strDecrypt = EncryptionHelper.Decrypt("myConnectionString");

1 个答案:

答案 0 :(得分:0)

要开始该过程,必须使用admin特权打开命令窗口。然后键入以下命令。

cd C:\Windows\Microsoft.NET\Framework\v4.0.30319

此命令将向您介绍给定的框架版本文件夹。现在,右键单击您的项目,然后在文件资源管理器中单击打开文件夹,然后复制位置。对我来说是F:\ Visual Studio \ EncryptConnectionString \ EncryptConnectionString。现在,请返回命令提示符,然后按如下所示键入命令。

ASPNET_REGIIS -PEF "connectionStrings" "F:\Visual Studio\EncryptConnectionString\EncryptConnectionString"

单击回车键。您将获得输出

加密ConnectionString输出

请注意,文本connectionStrings区分大小写。如果您不按原样提供,则会出现如下错误。

C:\Windows\Microsoft.NET\Framework\v4.0.30319>ASPNET_REGIIS -PEF "connectionstrings" "F:\Visual Studio\EncryptConnectionString\EncryptConnectionString"
Microsoft (R) ASP.NET RegIIS version 4.0.30319.0
Administration utility to install and uninstall ASP.NET on the local machine.
Copyright (C) Microsoft Corporation.  All rights reserved.
Encrypting configuration section...
The configuration section 'connectionstrings' was not found.
Failed!

因此,键入命令时请小心。现在,我将返回到我们的应用程序,并查看配置文件。我们可以?您可以看到连接字符串已加密,如下所示。

<connectionStrings configProtectionProvider="RsaProtectedConfigurationProvider">
    <EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element"
      xmlns="http://www.w3.org/2001/04/xmlenc#">
      <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" />
      <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
        <EncryptedKey xmlns="http://www.w3.org/2001/04/xmlenc#">
          <EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#rsa-1_5" />
          <KeyInfo xmlns="http://www.w3.org/2000/09/xmldsig#">
            <KeyName>Rsa Key</KeyName>
          </KeyInfo>
          <CipherData>
            <CipherValue>B4B3oZrbpQsYM7Eaq5smukqDj9XUYUCwygBYRG1iasN4ll5W4wAKVCIFCRfvOJGoIXzgqpyjAI30IKf5pnZ/xWqmo3p/wGfOKdMrzd041dt9llLGbxFpLJs0Nkm583PJ1FppXLAy7FOD0YoBVhG/PBtBgLjTQqcXRNbVcgufzuArlv/EH+7lzSNRclXSTMOPMtISF65hPI9ICj9qLx7RBGhVZ6uFZVFteyyuRd2i3D2r7wJfr6KflFkakdxp1OWE2JK4Ldb8kZSwAy3bNaI/qaV9EgIWt9wM6RZO/IrI3kI/bX8JuvirPw3j/+TLDB3MoIgKjSbLpR3GYTm9csPu8g==</CipherValue>
          </CipherData>
        </EncryptedKey>
      </KeyInfo>
      <CipherData>
        <CipherValue>0n1Y6ScSNZDR4x1sXfK05w9h+pp2OrAEQFQsoAUP5Y/hPsfpJS/7jv21PbPlkYmdCzycM4PGGb0+fuffR3RuL1x0tn7rfyUdA9llTfkyRQKwS9xOmkMsVFXgQDr8P4aXGef1fZPE2gjhcjm/JQToLwsfQZK1gNr4d6cIPFNqKD6wt24F7fuySJPX3OgLb8wXfQMd7ij+JcZzNlnyNHbq/DIjxSpPOnMrC52t06Jj8F8+MsSud9GcijcFB2UhvLVXQwyZ51nEj6Tf36Zbca8bgw==</CipherValue>
      </CipherData>
    </EncryptedData>
  </connectionStrings>

Click Here阅读有关加密/解密的完整说明

相关问题