简体   繁体   中英

Xml encrypted data error

I made the following code to insert data to an encrypted xml file, I used a memory stream for the decrypted file and load it in a XmlDocument to add the required data, then encrypted it again back to the same file.

public static void IN_EncryptFile(MemoryStream FLin,string Path)
{
    UnicodeEncoding UE = new UnicodeEncoding();
    byte[] key = UE.GetBytes(sKey);

    string cryptFile = Path;
    FileStream fsCrypt = new FileStream(cryptFile, FileMode.Open);

    RijndaelManaged RMCrypto = new RijndaelManaged();

    CryptoStream cs = new CryptoStream(fsCrypt,
        RMCrypto.CreateEncryptor(key, key),
        CryptoStreamMode.Write);

    FLin.Position = 0;

    int data;
    while ((data = FLin.ReadByte()) != -1)
    {
        cs.WriteByte((byte)data);
    }

    FLin.Close();
    FLin.Flush();
    cs.Close();
    fsCrypt.Close();
}


public static MemoryStream OUT_DecryptFile(string Path)
{
    UnicodeEncoding UE = new UnicodeEncoding();
    byte[] key = UE.GetBytes(sKey);

    FileStream fsCrypt = new FileStream(Path, FileMode.Open);

    RijndaelManaged RMCrypto = new RijndaelManaged();

    CryptoStream cs = new CryptoStream(fsCrypt,
        RMCrypto.CreateDecryptor(key, key),
        CryptoStreamMode.Read);

    MemoryStream fsOut = new MemoryStream();

    int data;
    while ((data = cs.ReadByte()) != -1)
        fsOut.WriteByte((byte)data);
    cs.Close();
    fsCrypt.Close();
    fsOut.Position = 0;
    return fsOut;    
}

public static void Add_Data_XML()
{
    string XML_Pt = @"C:\Test.Trr";
    XmlDocument XDt = new XmlDocument();

    XDt.Load(Cryption.OUT_DecryptFile(XML_Pt));

   /// Adding XMl data

    MemoryStream Fnl = new MemoryStream();
    XDt.Save(Fnl);
    Cryption.IN_EncryptFile(Fnl, XML_Pt);
    Fnl.Flush();
    Fnl.Close();
}

I need an opinion about the code as it works fine for me but sometimes it generates some errors in the encrypted file which I still don't fully understand why, but when I dencrypt the xml file I find the added data are in the wrong format and placed after the main XMl node as follow :

<Main_Node>

</Main_Node>”ÇÛÏ”ö8—´Ú·…ï/1Ž"‹ÓÃåõ¶—QÝUŸy…¤Êç‹íîzR߆ô
nÃFçiŽÌm–FÆzÍW9 úv¤ï_øVO,ÈvÄ

Your issue is that you use MemoryStream to store the temporary data and you read from it while you can. MemoryStream has an internal buffer that is larger than the data you write to it usually. So if you keep reading from it, you may get garbage data out from it. I would recommend not reading byte by byte from it, or at least get the actual length of the data and read that many bytes.

You also first close the MemoryStream and then flush it. This is pointless.

Another problem could be because you use FileMode.Open and write less than the previously existed in the file. This will leave existing data after what you write and that will show as garbage. Using FileMode.Create would be the right way here, since it means "if there is no file with this name, create a new one. If there is a file, truncate it."

FileMode documentation

But if you surely always write more than you read, then this shouldn't be an issue.

Also I wouldn't say "it works fine for me" if it actually has issues.

I think you have an encoding issue. Try this

            MemoryStream ms = new MemoryStream();            
            XDt.Save(ms);
            StreamReader Fnl = new StreamReader(ms, Encoding.UTF8);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM