Encryption and Decryption of Database Connection String in ASP. NET web. config

  • 2021-07-22 09:36:06
  • OfStack

Although it is not very new, I believe there are still many people who don't know, well, don't talk nonsense, give the method directly: start-- > Run, enter cmd, and then enter the following

Encryption:

C:\ WINDOWS\ Microsoft.NET\ Framework\ v2.0. 50727\ aspnet_regiis.exe-pef "connectionStrings" "Your Web Project Path"

Decryption:

C:\ WINDOWS\ Microsoft. NET\ Framework\ v2.0. 50727\ aspnet_regiis. exe-pdf "connectionStrings" "Your Web Project Path"

. NET is a self-modified version of the path, where the name of the connectionStrings connection string.

It should be noted that a native-based key is used in the encryption process, which means that the decryption process must be completed on the same computer. If the encrypted Web. config file is moved to another computer, the connection string in the Web. config file will not be decrypted properly.

Comparison before and after adding secret:

1. Before encryption


<configuration>
  <connectionStrings>
    <add name="ConnectionName" connectionString="Server=127.0.0.1;Database=TestDB;User ID=sa;Password=ok"
     providerName="System.Data.SqlClient" />
  </connectionStrings>
</configuration>

2. Encrypted


<configuration>
  <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>fDfW3bnVt21RF3N39vDoPphEmDYbUX4cmciD/3+LMY0yRLHckyulnnyBLoflB7DUjyXXms0V33e7MOKt+u2TAocn6x+QHo9Z4Onf1fV0nEq6uTprWZ04M8SLbKp+Vg63JLtYQUft6xF+Bi/aN/ZJ3PYal93bdfNJjtXA2xsb82k=</CipherValue>
          </CipherData>
        </EncryptedKey>
      </KeyInfo>
      <CipherData>
        <CipherValue>92WEStBHBh0zeu705wseRUajhAHumV9uCrmFJWII8SrhQpjEDrSl0OAfhwYFENr4xpHSfkNDTEFVV5D4MXr3meMsCcp+oYEQxQ/mg1QYLe9mGD+NEaBnv95WzaDcdDyE1SkNKkq01pX94OUV1OygsQtEx1fCZd6le8fd7kx4PAFKDD0he6ajzNFmCoFxg1Dd1+MD3mukgFef64NbjYovTNW8v2G67wLE8vnrokxIvs6+0+rnpLepDAyiEDaL2D3jJWNcQrl+UXI=</CipherValue>
      </CipherData>
    </EncryptedData>
  </connectionStrings>
</configuration>

Related articles: