Changeset 15 for Common/SecureApi.cs


Ignore:
Timestamp:
Jun 18, 2024, 12:15:33 PM (5 months ago)
Author:
chronos
Message:
  • Modified: Updated files.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • Common/SecureApi.cs

    r1 r15  
    11using System;
    2 using System.Collections.Generic;
    3 using System.Linq;
    42using System.Text;
    53using System.Security;
    64using System.Security.Cryptography;
    7 
    85
    96namespace Common
     
    118    static class SecureApi
    129    {
    13         static byte[] entropy = System.Text.Encoding.Unicode.GetBytes("Salt Is Not A Password");
     10        private static readonly byte[] Entropy = Encoding.Unicode.GetBytes("Salt Is Not A Password");
    1411
    15         public static string EncryptString(System.Security.SecureString input)
     12        public static string EncryptString(SecureString input)
    1613        {
    17             byte[] encryptedData = System.Security.Cryptography.ProtectedData.Protect(
    18                 System.Text.Encoding.Unicode.GetBytes(ToInsecureString(input)),
    19                 entropy,
    20                 System.Security.Cryptography.DataProtectionScope.CurrentUser);
     14            byte[] encryptedData = ProtectedData.Protect(Encoding.Unicode.GetBytes(ToInsecureString(input)),
     15                Entropy, DataProtectionScope.CurrentUser);
    2116            return Convert.ToBase64String(encryptedData);
    2217        }
     
    2621            try
    2722            {
    28                 byte[] decryptedData = System.Security.Cryptography.ProtectedData.Unprotect(
    29                     Convert.FromBase64String(encryptedData),
    30                     entropy,
    31                     System.Security.Cryptography.DataProtectionScope.CurrentUser);
    32                 return ToSecureString(System.Text.Encoding.Unicode.GetString(decryptedData));
     23                byte[] decryptedData = ProtectedData.Unprotect(Convert.FromBase64String(encryptedData), Entropy,
     24                    DataProtectionScope.CurrentUser);
     25                return ToSecureString(Encoding.Unicode.GetString(decryptedData));
    3326            }
    3427            catch
     
    5144        public static string ToInsecureString(SecureString input)
    5245        {
    53             string returnValue = string.Empty;
     46            string returnValue;
    5447            IntPtr ptr = System.Runtime.InteropServices.Marshal.SecureStringToBSTR(input);
    5548            try
Note: See TracChangeset for help on using the changeset viewer.