编码&在WPF C#中解码字符串

时间:2016-02-25 11:37:52

标签: c# decode encode

我正在尝试编码&解码保存到WPF应用程序的.ini设置文件的字符串。

编码&解码类

class EncryptDecrypt
{

    public static string Base64Encode(string plainText)
    {
        var plainTextBytes = System.Text.Encoding.UTF8.GetBytes(plainText);
        return System.Convert.ToBase64String(plainTextBytes);
    }

    public static string Base64Decode(string base64EncodedData)
    {
        var base64EncodedBytes = System.Convert.FromBase64String(base64EncodedData);
        return System.Text.Encoding.UTF8.GetString(base64EncodedBytes);
    }

}

编码似乎正确地保存到.ini文件中,但是从.ini文件中解码条目似乎输出了不正确的字符串,如下所示;

来自密码字段的

编码工作正常(在save_click上)

inif.Write("APISettings", "HashKey", EncryptDecrypt.Base64Encode(hash_key.Password.ToString()));

但是,重新加载app然后导航到相同的字段/表单解码不正确输出条目(因为它用于检查页面的http状态)..

hash_key.Password = EncryptDecrypt.Base64Decode(inif.Read("APISettings", "HashKey"));

我可以看到输出的字符串是奇怪的翼状字符。

更新

解决方法是保存到xml文件,而不是保存到.ini文件,见下文;

        private void Save_Click(object sender, RoutedEventArgs e)
    {

        //Settings Data
        string appfolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
        string ApplicationPath = System.IO.Path.Combine(appfolder, "MyApp/Application");
        if (!Directory.Exists(ApplicationPath)) Directory.CreateDirectory(ApplicationPath);

        //Encryption

        if (!System.IO.File.Exists(ApplicationPath + @"\credentials.xml"))
        {
            XmlDocument doc = new XmlDocument();
            //(1) the xml declaration is recommended, but not mandatory
            XmlDeclaration xmlDeclaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
            XmlElement root = doc.DocumentElement;
            doc.InsertBefore(xmlDeclaration, root);

            //(2) string.Empty makes cleaner code
            XmlElement elementNode = doc.CreateElement(string.Empty, "Credentials", string.Empty);
            doc.AppendChild(elementNode);

            //Base URL
            XmlElement elementNodeBaseUrl = doc.CreateElement(string.Empty, "BaseUrl", string.Empty);
            elementNode.AppendChild(elementNodeBaseUrl);
            elementNodeBaseUrl.InnerText = base_url.Text.ToString();

            XmlElement elementNodeAdminPath = doc.CreateElement(string.Empty, "AdminPath", string.Empty);
            elementNode.AppendChild(elementNodeAdminPath);
            elementNodeAdminPath.InnerText = EncryptDecrypt.Base64Encode(admin_path.Password.ToString());

            XmlElement elementNodeAdminName = doc.CreateElement(string.Empty, "AdminName", string.Empty);
            elementNode.AppendChild(elementNodeAdminName);
            elementNodeAdminName.InnerText = EncryptDecrypt.Base64Encode(admin_name.Password.ToString());

            XmlElement elementNodeAdminPassword = doc.CreateElement(string.Empty, "AdminPassword", string.Empty);
            elementNode.AppendChild(elementNodeAdminPassword);
            elementNodeAdminPassword.InnerText = EncryptDecrypt.Base64Encode(admin_password.Password.ToString());

            XmlElement elementNodeHashKey = doc.CreateElement(string.Empty, "HashKey", string.Empty);
            elementNode.AppendChild(elementNodeHashKey);
            elementNodeHashKey.InnerText = EncryptDecrypt.Base64Encode(hash_key.Password.ToString());

            XmlElement elementNodeUpdated = doc.CreateElement(string.Empty, "Updated", string.Empty);
            elementNode.AppendChild(elementNodeUpdated);
            elementNodeUpdated.InnerText = DateTime.Now.ToString();

            doc.Save(ApplicationPath + @"\credentials.xml");

        }else{

            XmlDocument doc = new XmlDocument();
            //(1) the xml declaration is recommended, but not mandatory
            XmlDeclaration xmlDeclaration = doc.CreateXmlDeclaration("1.0", "UTF-8", null);
            XmlElement root = doc.DocumentElement;
            doc.InsertBefore(xmlDeclaration, root);

            //(2) string.Empty makes cleaner code
            XmlElement elementNode = doc.CreateElement(string.Empty, "Credentials", string.Empty);
            doc.AppendChild(elementNode);

            //Base URL
            XmlElement elementNodeBaseUrl = doc.CreateElement(string.Empty, "BaseUrl", string.Empty);
            elementNode.AppendChild(elementNodeBaseUrl);
            elementNodeBaseUrl.InnerText = base_url.Text.ToString();

            XmlElement elementNodeAdminPath = doc.CreateElement(string.Empty, "AdminPath", string.Empty);
            elementNode.AppendChild(elementNodeAdminPath);
            elementNodeAdminPath.InnerText = EncryptDecrypt.Base64Encode(admin_path.Password.ToString());

            XmlElement elementNodeAdminName = doc.CreateElement(string.Empty, "AdminName", string.Empty);
            elementNode.AppendChild(elementNodeAdminName);
            elementNodeAdminName.InnerText = EncryptDecrypt.Base64Encode(admin_name.Password.ToString());

            XmlElement elementNodeAdminPassword = doc.CreateElement(string.Empty, "AdminPassword", string.Empty);
            elementNode.AppendChild(elementNodeAdminPassword);
            elementNodeAdminPassword.InnerText = EncryptDecrypt.Base64Encode(admin_password.Password.ToString());

            XmlElement elementNodeHashKey = doc.CreateElement(string.Empty, "HashKey", string.Empty);
            elementNode.AppendChild(elementNodeHashKey);
            elementNodeHashKey.InnerText = EncryptDecrypt.Base64Encode(hash_key.Password.ToString());

            XmlElement elementNodeUpdated = doc.CreateElement(string.Empty, "Updated", string.Empty);
            elementNode.AppendChild(elementNodeUpdated);
            elementNodeUpdated.InnerText = DateTime.Now.ToString();

            doc.Save(ApplicationPath + @"\credentials.xml");
        }

        Thread.Sleep(1000);


        SaveButton.Visibility = Visibility.Hidden;
        onclick_progress.Visibility = Visibility.Visible;

        showProgress();

    }

0 个答案:

没有答案