繁体   English   中英

抛出异常:“System.PlatformNotSupportedException”。 如何在 dotnet core 3.1 中加密和解密安全字符串?

[英]Exception thrown: 'System.PlatformNotSupportedException' . How to encrypt and decrypt securestring in dotnet core 3.1?

操作系统: Ubuntu

平台: dotnet core 3.1

我试图使用以下方法对安全字符串进行加密和解密。但得到以下异常。

Exception thrown: 'System.PlatformNotSupportedException' in System.Security.Cryptography.ProtectedData.dll: 'Windows Data Protection API (DPAPI) is not supported on this platform.'

加密方法

        public static string EncryptString(System.Security.SecureString input)
        {
            byte[] encryptedData = System.Security.Cryptography.ProtectedData.Protect(
                System.Text.Encoding.Unicode.GetBytes(ToInsecureString(input)),
                entropy,
                System.Security.Cryptography.DataProtectionScope.CurrentUser);
            return Convert.ToBase64String(encryptedData);
        }

解密方法

        public static SecureString DecryptString(string encryptedData)
        {
            try
            {
                byte[] decryptedData = System.Security.Cryptography.ProtectedData.Unprotect(
                    Convert.FromBase64String(encryptedData),
                    entropy,
                    System.Security.Cryptography.DataProtectionScope.CurrentUser);
                return ToSecureString(System.Text.Encoding.Unicode.GetString(decryptedData));
            }
            catch
            {
                return new SecureString();
            }
        }

如何在 dotnet core 3.1 中加密和解密安全字符串

很抱歉,这个 API 仅在 Windows 上受支持。 请参阅: https://docs.microsoft.com/en-us/dotnet/core/compatibility/unsupported-apis

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM