何解密文件附加信息?

时间:2018-05-18 17:39:36

标签: c# encryption 3des

我需要在我的加密文件中添加一个xml标题,使其看起来像:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
 <EncryptedFileHeader>
  <Algorithm>3DES</Algorithm>
  <KeySize>192</KeySize>
  <BlockSize>64</BlockSize>
  <CipherMode>ECB</CipherMode>
  <IV>some IV</IV>
  <ApprovedUsers>
    <User>
      <Email>mark</Email>
      <SessionKey>sss</SessionKey>
    </User>
  </ApprovedUsers>
</EncryptedFileHeader>encrypted data...
more encrypted data....

要使用我在MSDN上找到的代码,请执行此操作:

public void EncryptData(String inName, String outName, byte[] tdesKey, byte[] tdesIV)
        {
            //Create the file streams to handle the input and output files.
            FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read);
            FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
            fout.SetLength(0);

            setHeader(fout , tdesIV);

            //Create variables to help with read and write.
            byte[] bin = new byte[100]; //This is intermediate storage for the encryption.
            long rdlen = 0;              //This is the total number of bytes written.
            long totlen = fin.Length;    //This is the total length of the input file.
            int len;                     //This is the number of bytes to be written at a time.

            TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();

            tdes.Padding = PaddingMode.ANSIX923;
            CryptoStream encStream = new CryptoStream(fout, tdes.CreateEncryptor(tdesKey, tdesIV), CryptoStreamMode.Write);

            //Read from the input file, then encrypt and write to the output file.
            while (rdlen < totlen)
            {
                len = fin.Read(bin, 0, 100);
                encStream.Write(bin, 0, len);
                rdlen = rdlen + len;
            }

            encStream.Close();
        }

添加xml标题:

private void setHeader(FileStream output, byte[] sal)
        {
            XmlDocument doc = new XmlDocument();
            XmlNode docNode = doc.CreateXmlDeclaration("1.0", "UTF-8", "yes");
            doc.AppendChild(docNode);

            XmlNode header = doc.CreateElement("EncryptedFileHeader");
            doc.AppendChild(header);

            XmlNode algorithm = doc.CreateElement("Algorithm");
            algorithm.InnerText = "3DES " + mw.cmb_algorithm.Text;
            header.AppendChild(algorithm);
            XmlNode keySize = doc.CreateElement("KeySize");
            keySize.InnerText = 192.ToString();
            header.AppendChild(keySize);
            XmlNode blockSize = doc.CreateElement("BlockSize");
            blockSize.InnerText = 64.ToString();
            header.AppendChild(blockSize);
            XmlNode cipherMode = doc.CreateElement("CipherMode");
            cipherMode.InnerText = mw.cmb_encryption_mode.Text;
            header.AppendChild(cipherMode);
            XmlNode salt = doc.CreateElement("IV");
            salt.InnerText = System.Text.Encoding.UTF8.GetString(sal);
            header.AppendChild(salt);
            XmlNode approvedUsers = doc.CreateElement("ApprovedUsers");
            header.AppendChild(approvedUsers);

            XmlNode user1 = doc.CreateElement("User");
            approvedUsers.AppendChild(user1);

            XmlNode mail = doc.CreateElement("Email");
            mail.InnerText = "mark";
            user1.AppendChild(mail);
            XmlNode sessionKey = doc.CreateElement("SessionKey");
            sessionKey.InnerText = "sss";
            user1.AppendChild(sessionKey);

            doc.Save(output);            
        }

它做我需要的,但是当涉及到解码时,我有一个问题。 由于某种原因,输入FileStream(编码文件)的长度增加了3,因此我得到System.Security.Cryptography.CryptographicException“要解密的数据长度无效”,同时关闭Cryptostream。

解密代码:

public void DecryptData(String inName, String outName, byte[] tdesKey, byte[] tdesIV)
        {
            //Create the file streams to handle the input and output files.
            FileStream fin = new FileStream(inName, FileMode.Open, FileAccess.Read);
            FileStream fout = new FileStream(outName, FileMode.OpenOrCreate, FileAccess.Write);
            fout.SetLength(0);

            int position;
            Header header = new Header();
            readHeader(inName,out position, header);
            fin.Position = position;
            //Create variables to help with read and write.
            byte[] bin = new byte[100]; //This is intermediate storage for the encryption.
            long rdlen = 0;              //This is the total number of bytes written.
            long totlen = fin.Length - position;    //This is the total length of the input file.
            int len;                     //This is the number of bytes to be written at a time.

            TripleDESCryptoServiceProvider tdes = new TripleDESCryptoServiceProvider();


            tdes.Padding = PaddingMode.ANSIX923;
            CryptoStream encStream = new CryptoStream(fout, tdes.CreateDecryptor(tdesKey, tdesIV), CryptoStreamMode.Write);


            //Read from the input file, then encrypt and write to the output file.
            while (rdlen < totlen)
            {
                len = fin.Read(bin, 0, 100);
                encStream.Write(bin, 0, len);
                rdlen = rdlen + len;            
            }
            encStream.Close(); //<- exception
        }

我需要将此xml标头和加密数据放在一个文件中,并且需要解密数据。不要看看放入xml的数据,它只是一些空白信息我需要测试加密是否有效。 当我不添加标题加密和解密工作正常。 知道怎么做这个工作吗?

0 个答案:

没有答案