第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

加密文件未正確解密

加密文件未正確解密

C#
富國滬深 2022-06-12 10:30:25
我有以下問題:有一個名為 的類UserConfigStorage,其中有 8 個字符串。程序第一次運行時,會要求用戶輸入他們的個人信息,這些信息存儲在上述 8 個字符串中。一旦用戶點擊一個按鈕,這個類就會被序列化和加密,以防止用戶修改它。然后,當程序第二次運行時,根據(jù)Form_Load事件,文件被解密,并使用反序列化方法將其中的信息加載到文本框。但是,System.Runtime.Serialization.SerializationException: End of Stream encountered before parsing was completed拋出異常,并且無法讀取文件。在查看代碼和生成的文件時,我發(fā)現(xiàn)生成的解密文件沒有所有信息。它實際上只存儲了 8 個字符串中的 6 個。方法如下:序列化方法    public void SerializeUserConfig(string fileName)    {        try        {            FileStream fileStream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.ReadWrite);            BinaryFormatter binForm = new BinaryFormatter();            binForm.Serialize(fileStream, userconfigstorage);            fileStream.Close();            encryptor.Encrypt(fileName, perfilAcesso.GetUserConfigPath() + "Encrypted", "syndra15OP");            File.Delete(fileName);            MessageBox.Show("Dados salvos com sucesso!");        }        catch (Exception exception)        {            errorlog.SetError(exception.ToString());            SerializeError(perfilAcesso.GetUserErrorLogPath());            MessageBox.Show("Houve um erro ao salvar as configura??es!\nPor favor, contate o desenvolvedor.\n\nEID: 002");        }    }反序列化方法    public UserConfigStorage DeserializeUserConfig(string fileName)    {        encryptor.Decrypt(perfilAcesso.GetUserConfigPath() + "Encrypted", fileName, "syndra15OP");        FileStream fileStream = new FileStream(fileName, FileMode.Open, FileAccess.Read);        BinaryFormatter binForm = new BinaryFormatter();        UserConfigStorage userconfigstorage = (UserConfigStorage)binForm.Deserialize(fileStream);        fileStream.Close();        return userconfigstorage;    }任何人都可以幫助我了解導致文件未完全解密的原因嗎?提前致謝!
查看完整描述

1 回答

?
白衣非少年

TA貢獻1155條經(jīng)驗 獲得超0個贊

是的,在這里使用絕對是您的朋友!:)


為什么要將對象序列化到一個文件中,只是為了在之后將其加密到另一個文件中?


我已經(jīng)清理了您的代碼,這應該可以按預期工作:


public void SerializeUserConfig(string fileName)

{

    try

    {

        Encrypt(userconfigstorage, Path.Combine(perfilAcesso.GetUserConfigPath(), fileName), "syndra15OP");

        MessageBox.Show("Dados salvos com sucesso!");

    }

    catch (Exception exception)

    {

        errorlog.SetError(exception.ToString());

        SerializeError(perfilAcesso.GetUserErrorLogPath());

        MessageBox.Show("Houve um erro ao salvar as configura??es!\nPor favor, contate o desenvolvedor.\n\nEID: 002");

    }

}


public UserConfigStorage DeserializeUserConfig(string fileName)

{

    return Decrypt(Path.Combine(perfilAcesso.GetUserConfigPath(), fileName), "syndra15OP");

}


public void Encrypt(UserConfigStorage input, string filePath, string strHash)

{

    using (TripleDESCryptoServiceProvider tdc = new TripleDESCryptoServiceProvider())

    {

        using (FileStream outStream = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write))

        {


            using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider())

            {

                tdc.Key = md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(strHash));

                md5.Clear();

            }


            tdc.Mode = CipherMode.ECB;


            using (CryptoStream cryStream = new CryptoStream(outStream, tdc.CreateEncryptor(), CryptoStreamMode.Write))

            {

                BinaryFormatter binForm = new BinaryFormatter();

                binForm.Serialize(cryStream, input);

            }

        }

    }

}


public UserConfigStorage Decrypt(string filePath, string strHash)

{

    UserConfigStorage output;


    using (TripleDESCryptoServiceProvider tdc = new TripleDESCryptoServiceProvider())

    {

        using (FileStream outStream = new FileStream(filePath, FileMode.Open, FileAccess.Read))

        {


            using (MD5CryptoServiceProvider md5 = new MD5CryptoServiceProvider())

            {

                tdc.Key = md5.ComputeHash(ASCIIEncoding.ASCII.GetBytes(strHash));

                md5.Clear();

            }


            tdc.Mode = CipherMode.ECB;


            using (CryptoStream cryStream = new CryptoStream(outStream, tdc.CreateDecryptor(), CryptoStreamMode.Read))

            {

                BinaryFormatter binForm = new BinaryFormatter();

                output = binForm.Deserialize(cryStream) as UserConfigStorage;

            }

        }

    }


    return output;

}

問候


查看完整回答
反對 回復 2022-06-12
  • 1 回答
  • 0 關注
  • 190 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號