SoFunction
Updated on 2025-03-06

Methods of encryption and decryption in C#

Some of the configurations are as follows:

Copy the codeThe code is as follows:

  <appSettings>
    <add key="EricTest" value="EricTest"/>
    <add key="Encrypt" value="Encrypt value"/>
  <appSettings>

  <connectionStrings >
    <add name="EncryptConnection" connectionString="Data Source=.\SQL2000;Initial Catalog=Northwind;user id=sa;password=test"/>
    <add name="SQLExpress" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=myDB;user id=sa;password=test"
     providerName="" />
  <connectionStrings>
 

Before encrypting, make some preparations.

First reference the usage space

Copy the codeThe code is as follows:

using ;
using ;
//Define the encryption method. Mainly for ease of use.

        ///
/// Encryption method
        ///
        public enum EncryptType
        {
            DataProtectionConfigurationProvider,
            RSAProtectedConfigurationProvider
        }
 


Encryption using DPAPI
Copy the codeThe code is as follows:

        ///
/// Encrypt Config in DPAPI
        ///
        private void EncryptWebConfigByDPAPI()
        {
            Configuration configuration = null;
            ConfigurationSection connectionSection = null;

//Open the file on the website where the Request is located
            configuration = ();
//Get connectionStrings settings block
            connectionSection = ("connectionStrings");
//When not encrypted
            if (!)
            {
                (());
                ();
            }
        }


Comparison of data before and after encryption
Copy the codeThe code is as follows:

  <connectionStrings >
    <add name="EncryptConnection" connectionString="Data Source=.\SQL2000;Initial Catalog=Northwind;user id=sa;password=test"/>
    <add name="SQLExpress" connectionString="Data Source=.\SQLEXPRESS;Initial Catalog=myDB;user id=sa;password=test"
     providerName="" />
  <connectionStrings>

Copy the codeThe code is as follows:

  <connectionStrings configProtectionProvider="DataProtectionConfigurationProvider">
    <EncryptedData>
      <CipherData>
        <CipherValue>AQAAANCMnd8BFdERjHoAwE/Cl+sBAAAAPCENeNbVhU6C+bx4E8qcPAQAAAACAAAAAAAQZgAAAAEAACAAAADiE56Y0pGCoKEpOvxMVmMYO3tMqI/2W89HUIq0LeJAegAAAAAOgAAAAAIAACAAAACYuFOjNtk1iprbV91mmP8aCIULLZvRHAPwbAvoHvtXpKACAABP0/YOt/B8IG/eLnaxrDVCXPq6l8McVOvpL0hV4507VEpJb6FyRoM9c5TI6iIF6Jz8GQfnfQiF4P27RLyvvvu/R9KpuwDsZ0IKjpe47Nt/q/qOLlQx6vhQVE8yAjJ64DrujH6wjS2XdZSC03Co4u9OG/YdJX9zkpjVMNW8cx5FFklYmIzHxWx+b1ZFtZMu0CA8lzU4slkTBFmE3JMMa4KqC6EGedDXD3z53QkBt3KISWt1lM5ulPeQ8rfR7qrzUEWQsgaGLuNTJvCDwlPJWbZVzQaOHo71/epQRPHgvmNAkK1/hRqwXr0uMF9K6HNKCr0NDLFECLHcjCC4zR6QhhWdWT8FHPm2Zg2yucekeHQsrbiWtjZqg/DdyVPLWqmEdj82T1Gm9u9xhDHuNLpOT1FXy7bGjjok9TW1MxbWIXQ7bBih0mUwmvESD8aZGdxoH0XEFyy3flY2hjwszG4Opg3Qmz1/S0x6Sbz1vJJL7rk7FTdG3PwMDFvcvKlmmDZQTkM3SqplazwrjYI4IJBnIAL/uBxwMdxO515lWS55dDkdnx5r8HtGoeCN+cw5qFW8xxRPRsQKg6Sgti1GF2KzezZ5WJegN0hqUs18XoEpzCuuALbzHqRNBswwn0/GfdadxfwdNxoTHdJ+cQC3vSLk5f02pTW9CFZWDn30AFjIpMtArNltppLvWAP1YxtKMtyzjmv7iiIOsMtHFICTJAzO7FeTc+YToifu/wddPESZQB2MlrefnUK+cBkoSzAusfhtqUWfhblv6JnEq5A/PdohEkSu0dn2pC6AeqoG/Yngb6BJzpRFxssDfIkDH6LfXdo4s5WJXJx7VQNqUo4mmTKoUcp6DGmoogZqbHODL3MbgKFQyjdvXV9+4Aa9qOlHbcKDL5tAAAAAChj0UAPAO59pmMZ7gJ67ho1Mxjg9NTuAh/lG5XI+phDRzWcNRmjv2ZrUhz8eWIgCMoIG7NviBnbmCeT4K8pXUw==CipherValue>
      <CipherData>
    <EncryptedData>
  <connectionStrings>
 

 

Decrypt data encrypted using DPAPI

Copy the codeThe code is as follows:

        ///
/// Decrypt DPAPI
        ///
        private void DecryptWebConfigByDPAPI()
        {
            Configuration configuration = null;
            ConfigurationSection connectionSection = null;

//Open the file on the website where the Request is located
            configuration = ();
//Get connectionStrings settings block
            connectionSection = ("connectionStrings");
            if ()
            {
                ();
                ();
            }
        }
 


Call DPAPI to encrypt data (no decryption required)
Copy the codeThe code is as follows:

        ///
/// Obtain encrypted data
        ///
        private void GetEncryptWebConfigByDPAPI()
        {
            string cncryptConnection = ["EncryptConnection"].ConnectionString;
            string sqlExpressConnection = ["SQLExpress"].ConnectionString;
        }

Using RSA encryption
Copy the codeThe code is as follows:

        ///
/// Encrypt Config in RSA
        ///
        private void EncryptWebConfigByRsa()
        {
            Configuration configuration = null;
            ConfigurationSection connectionSection = null;

//Open the file on the website where the Request is located
            configuration = ();
//Get connectionStrings settings block
            connectionSection = ("appSettings");
//When not encrypted
            if (!)
            {
                (());
                ();
            }
        }


Comparison of data before and after encryption:
Copy the codeThe code is as follows:

    <appSettings>
    <add key="EricTest" value="EricTest"/>
    <add key="Encrypt" value="Encrypt value"/>
  <appSettings>

Copy the codeThe code is as follows:

  <appSettings configProtectionProvider="RsaProtectedConfigurationProvider">
    <EncryptedData Type="http:///2001/04/xmlenc#Element"
      xmlns="http:///2001/04/xmlenc#">
      <EncryptionMethod Algorithm="http:///2001/04/xmlenc#tripledes-cbc" />
      <KeyInfo xmlns="http:///2000/09/xmldsig#">
        <EncryptedKey xmlns="http:///2001/04/xmlenc#">
          <EncryptionMethod Algorithm="http:///2001/04/xmlenc#rsa-1_5" />
          <KeyInfo xmlns="http:///2000/09/xmldsig#">
            <KeyName>Rsa <KeyKeyName>
          <KeyInfo>
          <CipherData>
            <CipherValue>CJIkulw6qBtLeY5MJ9bs1ROpF1l3f4ulRzKnd6ZXN6XyG9O+b6Hr52ijK1AL9/+nsBseAPfdKDGaX/SKlJYwgzHhhi9sBrDBJ10dJcSnuGuWpI5zSLc+QHdpV0Z4iJTw83jmRDb9eFCX7aG60qWl52ofeqlI/ps1HsOjlKPSv8M=CipherValue>
          <CipherData>
        <EncryptedKey>
      <KeyInfo>
      <CipherData>
        <CipherValue>y1aEM/BRwcwZXWeuLe9mbakU8AuI7CpElrjoJgQEfzaoZXq7uEJspQAxJyDIYmCF4EgjKhE7pY6WBRAjRaBBODxxEQHGJ8I1+T554H8zosZ2InO43h5X0ZjCmvAWxNbEq1rP9DnuTcHEYqrw70nNShf79W6e2fmUF1DoVpwYNWMLeHJCP7ZkZg==CipherValue>
      <CipherData>
    <EncryptedData>
  <appSettings>

Decrypt RSA encrypted data

Copy the codeThe code is as follows:

        ///
/// Decrypt Rsa
        ///
        private void DecryptWebConfigByRsa()
        {
            Configuration configuration = null;
            ConfigurationSection connectionSection = null;

//Open the file on the website where the Request is located
            configuration = ();
//Get connectionStrings settings block
            connectionSection = ("appSettings");
            if ()
            {
                ();
                ();
            }
        }

Call to encrypt data using RSA (no decryption required)

Copy the codeThe code is as follows:

        ///
/// Obtain encrypted data
        ///
        private void GetEncryptWebConfigByRsa()
        {
            string cncryptConnection = ["EricTest"];
            string sqlExpressConnection = ["Encrypt"];
        }