fetion Decrypt password

來源:互聯網
上載者:User
private void button1_Click(object sender, EventArgs e){    XmlDocument doc = new XmlDocument();    doc.Load(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + @"/Fetion/Configuration.dat");    doc.InnerXml = XMLDecodeHelper.DecodeXML(doc);    XmlNode node = doc.SelectSingleNode("/ImpsConfiguration/AccountsData/Accounts");    XmlNode node2 = doc.SelectSingleNode("/ImpsConfiguration/AccountsData/Mappings");    this.listView1.Items.Clear();    foreach (XmlNode node3 in node.ChildNodes)    {        ListViewItem item = new ListViewItem();        string str = node3.Attributes["id"].Value;        item.Text = str;        this.listView1.Items.Add(item);        foreach (XmlNode node4 in node2.ChildNodes)        {            if (node4.Attributes["mobile-no"].Value == str)            {                item.SubItems.Add(node4.Attributes["sid"].Value);                break;            }        }        if (node3.Attributes["password"] != null)        {            item.SubItems.Add(SecurityHelper.DecryptPassword(node3.Attributes["password"].Value));            continue;        }        item.SubItems.Add("(password not saved)");    }}

 

public static class SecurityHelper{    // Fields    private static string _secToken = WindowsIdentity.GetCurrent().User.Value;    public static readonly byte[] RgbIV = new byte[] { 0x1f, 0x4e, 0x81, 12, 0xa8, 0x20, 0xfe, 0x42 };    // Methods    public static string DecryptPassword(string text)    {        if (text.Length <= 0)        {            return string.Empty;        }        return InnerDecrypt(Convert.FromBase64String(text), _secToken);    }    public static string EncryptPass(string sid, string domain, string oldPass, string newPass)    {        string s = sid + ":" + domain + ":" + oldPass;        byte[] bytes = Encoding.UTF8.GetBytes(s);        byte[] buffer2 = new MD5CryptoServiceProvider().ComputeHash(bytes);        string str2 = StringHelper.Hex2Str(buffer2) + ":" + newPass;        TripleDESCryptoServiceProvider provider = new TripleDESCryptoServiceProvider();        provider.Key = buffer2;        MemoryStream stream = new MemoryStream();        using (CryptoStream stream2 = new CryptoStream(stream, new TripleDESCryptoServiceProvider().CreateEncryptor(buffer2, RgbIV), CryptoStreamMode.Write))        {            byte[] buffer = Encoding.UTF8.GetBytes(str2);            stream2.Write(buffer, 0, buffer.Length);            stream2.FlushFinalBlock();            stream2.Close();        }        byte[] buffer4 = stream.ToArray();        stream.Close();        return StringHelper.Hex2Str(buffer4);    }    public static string EncryptPassword(string plain)    {        if (plain.Length <= 0)        {            return string.Empty;        }        return Convert.ToBase64String(InnerEncrypt(plain, _secToken));    }    private static string InnerDecrypt(byte[] buffer, string keyToken)    {        byte[] bytes = Encoding.UTF8.GetBytes(keyToken);        byte[] rgbKey = new MD5CryptoServiceProvider().ComputeHash(bytes);        MemoryStream stream = new MemoryStream(buffer, false);        using (CryptoStream stream2 = new CryptoStream(stream, new TripleDESCryptoServiceProvider().CreateDecryptor(rgbKey, RgbIV), CryptoStreamMode.Read))        {            StreamReader reader = new StreamReader(stream2);            return reader.ReadToEnd();        }    }    private static byte[] InnerEncrypt(string text, string keyToken)    {        byte[] bytes = Encoding.UTF8.GetBytes(keyToken);        byte[] rgbKey = new MD5CryptoServiceProvider().ComputeHash(bytes);        MemoryStream stream = new MemoryStream();        using (CryptoStream stream2 = new CryptoStream(stream, new TripleDESCryptoServiceProvider().CreateEncryptor(rgbKey, RgbIV), CryptoStreamMode.Write))        {            byte[] buffer = Encoding.UTF8.GetBytes(text);            stream2.Write(buffer, 0, buffer.Length);            stream2.FlushFinalBlock();            stream2.Close();        }        return stream.ToArray();    }}
Collapse Methods

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.