如何将参数传递给static byte [] entropy = System.Text.Encoding.Unicode.GetBytes();哪个是运行时?

时间:2013-08-23 12:05:40

标签: c# installshield

我正在处理用户在安装应用程序期间提供的密码的加密和解密。当我硬编码密码并提供给下面的密码时,一切都很完美。

static byte[] entropy = System.Text.Encoding.Unicode.GetBytes("!23$cal1s");

有没有办法可以在这里代替硬编码传递运行时变量(比如参数)

static byte[] entropy = System.Text.Encoding.Unicode.GetBytes(variable);

我在这上面打了一个星期。请帮助,我不是开发人员,但我必须这样做

我的整个代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Security;

namespace EncryptionProg
{

  public class EncrptionHelper
    {

        #region Member variables
        public const string initVector = "!23$cal1s" ;
       #endregion

        public string AppSettingKey { get; set; }
        public string ConfileFilePath { get; set; }
        public string Password { get; set; }
        static readonly byte[] entropy = System.Text.Encoding.Unicode.GetBytes(initVector);

        #region Public Methods

        public void EncryptDecryptmethod(string configFilePathName, string appSettingKey, string appSettingValue)
        {
           SetSetting(appSettingKey, appSettingValue, configFilePathName);
        }

        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();
            }
        } 
        #endregion

        #region Private Methods
        static bool SetSetting(string Key, string Value, string configFilePath)
        {
            bool result = false;
            try
            {
                System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(configFilePath);
                //config.AppSettings.File = configFilePath;
                config.AppSettings.Settings.Remove(Key);
                var kvElem = new KeyValueConfigurationElement(Key, Value);
                config.AppSettings.Settings.Add(kvElem);

                config.Save(ConfigurationSaveMode.Modified);

                ConfigurationManager.RefreshSection("appSettings");

                result = true;
            }
            finally
            { }
            return result;
        }

        static string GetSetting(string Key, string configFilePath)
        {
            string result = null;
            try
            {
                System.Configuration.Configuration config = ConfigurationManager.OpenExeConfiguration(configFilePath);
                result = config.AppSettings.Settings[Key].Value.ToString();
            }
            finally
            { }
            return result;
        }

        static SecureString ToSecureString(string input)
        {
            SecureString secure = new SecureString();
            foreach (char c in input)
            {
                secure.AppendChar(c);
            }
            secure.MakeReadOnly();
            return secure;
        }

        static string ToInsecureString(SecureString input)
        {
            string returnValue = string.Empty;
            IntPtr ptr = System.Runtime.InteropServices.Marshal.SecureStringToBSTR(input);
            try
            {
                returnValue = System.Runtime.InteropServices.Marshal.PtrToStringBSTR(ptr);
            }
            finally
            {
                System.Runtime.InteropServices.Marshal.ZeroFreeBSTR(ptr);
            }
            return returnValue;
        } 
        #endregion

    }
}

0 个答案:

没有答案