C# 使用web.config文件中的machinekey加密和解密.net core中的字符串

C# 使用web.config文件中的machinekey加密和解密.net core中的字符串,c#,.net-core,cryptography,C#,.net Core,Cryptography,我有一段旧代码,它使用AES和存储在web.config文件中的machinekey对字符串进行加密和解密。这是一个Framework4应用程序。下面是执行加密和解密的类的一些代码: private static readonly MachineKeySection MachineKeyConfig = (MachineKeySection)ConfigurationManager .GetSection("system.web/machineKey"

我有一段旧代码,它使用AES和存储在web.config文件中的machinekey对字符串进行加密和解密。这是一个Framework4应用程序。下面是执行加密和解密的类的一些代码:

 private static readonly MachineKeySection MachineKeyConfig =
    (MachineKeySection)ConfigurationManager
        .GetSection("system.web/machineKey");

    private readonly byte[] _key;
    private readonly byte[] _iv;


    public AESEncryption()
    {
        Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(MachineKeyConfig.DecryptionKey, new byte[] { byte values removed });
        _key = pdb.GetBytes(32);
        _iv = pdb.GetBytes(16);
    }

    public AESEncryption(string key)
    {
        Rfc2898DeriveBytes pdb = new Rfc2898DeriveBytes(key, new byte[] { byte value removed });
        _key = pdb.GetBytes(32);
        _iv = pdb.GetBytes(16);
    }

    
    public string Encrypt(string value)
    {
        
        if (string.IsNullOrWhiteSpace(value))
        {
            return value;
        }

        byte[] clearBytes = Encoding.Unicode.GetBytes(value);

        using (Aes encryptor = Aes.Create())
        {
            if (encryptor != null)
            {
                encryptor.Padding = PaddingMode.PKCS7;
                using (MemoryStream ms = new MemoryStream())
                using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateEncryptor(_key, _iv), CryptoStreamMode.Write))
                {
                    cs.Write(clearBytes, 0, clearBytes.Length);
                    cs.FlushFinalBlock();
                    value = Convert.ToBase64String(ms.ToArray());
                }
            }
        }
        return value;
    }

    public string Decrypt(string value)
    {
        
        if (string.IsNullOrWhiteSpace(value))
        {
            return value;
        }
        byte[] cipherBytes = Convert.FromBase64String(value);

        using (Aes encryptor = Aes.Create())
        {
            if (encryptor != null)
            {
                encryptor.Padding = PaddingMode.PKCS7;
                using (MemoryStream ms = new MemoryStream())
                using (CryptoStream cs = new CryptoStream(ms, encryptor.CreateDecryptor(_key, _iv), CryptoStreamMode.Write))
                {
                    cs.Write(cipherBytes, 0, cipherBytes.Length);
                    cs.FlushFinalBlock();
                    value = Encoding.Unicode.GetString(ms.ToArray());
                }
            }
        }
       
        return value;
    }
对于加密来说非常简单。我需要使用与.net core 3.1控制台应用程序中用于此加密/解密的machinekey相同的machinekey,将一些数据提供给使用相同machinekey加密的系统。我添加了一个App.config文件,并将machinekey从Framework应用程序复制到.net核心应用程序。以下是配置:

   <?xml version="1.0" encoding="utf-8" ?>
<configuration>
    <machineKey decryption="AES" decryptionKey="[decryptionkey]" validation="AES" validationKey="[validation key]" />
</configuration>

它不起作用了。我需要在.net core应用程序上使用相同的machinekey,以便能够在较旧的Framework应用程序中读取从该应用程序流入系统的信息,反之亦然。

我将machinekey值从旧的web.config文件中移走,并将其作为单个键值添加到app.config的appSettings部分。完成后,我导入System.Configuration,并使用Configuration manager提取所需的值

private readonly string decryptionKey = ConfigurationManager.AppSettings.Get("decryptionKey");
然后我可以像以前一样使用这个值,并验证解密值和加密值确实相同。我以前遇到的问题是,如果我在应用程序配置中包含machineKey,我会得到一个错误,即这是一个无法识别的部分:

ConfigurationErrorsException:无法识别的配置节machineKey


因此,我将这些值移到Appsettings中,并将它们拉到Appsettings中。

因此,您的问题根本不是关于加密/解密,而是如何访问app.config?另外,请更具体一些;“它不工作”太含糊了。值得注意的是,.Net使用app.config/web.config。Net Core使用appsettings.json:如何更具体?我问的是如何在涉及加密/解密的.net核心应用程序中使用web.config文件中的machinekey。你认为什么标题更合适@KlausGütter?我指的是“它不起作用”。您是否得到异常、空值、错误值等。。。?
private readonly string decryptionKey = ConfigurationManager.AppSettings.Get("decryptionKey");