【问题标题】:System.ArgumentOutOfRangeException C#System.ArgumentOutOfRangeException C#
【发布时间】:2016-04-25 12:12:35
【问题描述】:

我的一个游戏更新程序在这里遇到了一点问题。基本上,它应该做的是解密已经加密的文件,读取它,再次加密并运行游戏。有时它会下载更新,为此它需要下载加密更新,解密游戏文件夹中已有的文件,添加更新,再次加密文件,运行游戏。通常我必须制作一个新的解密文件并在更新时将其放入文件夹中,我想避免这种情况并让 exe 自己完成所有操作,而不需要制作任何额外的文件。除了我来到需要读取解密数据的部分之外,我已经走了很远,但我不知道如果那里没有文件应该如何读取它(我想保持这种方式)

这是我得到 System.ArgumentOutOfRange 异常的地方,非负数是必需的。

System.ArgumentOutOfRangeException: Non-negative number required. Parameter name: value at System.IO.FileStream.set_Position(Int64 value) at Update.SAH.Write_File(FILE f, SAH patch) in C:\........SAH.cs:line322


Line 322:
BinaryReader br = new BinaryReader(File.OpenRead(patch.SAF_Path));
            br.BaseStream.Position = (long)f.Start;
            byte[] file = br.ReadBytes((int)f.Length);
            br.Dispose();
            BinaryWriter bw = new BinaryWriter(File.OpenWrite(SAF_Path));
            bw.BaseStream.Position = (int)new FileInfo(SAF_Path).Length;
            f.Start = (ulong)bw.BaseStream.Position;
            bw.Write(file);
            bw.Dispose();
            current_folder.Files.Add(f);

                f.Start = BitConverter.ToUInt64(file, Offset);
                Offset += 8;

                f.Length = BitConverter.ToUInt64(file, Offset);
                Offset += 8;

任何帮助将不胜感激。

【问题讨论】:

  • 看起来 f.Start
  • 加密/解密在哪里?你的意思是编码/解码吗?加密/解密是一种使用密钥来提供机密性的加密操作。
  • 这是加密/解密 public static byte[] Encrypt(byte[] bytes, string key) { return new TripleDESCryptoServiceProvider { Key = new MD5CryptoServiceProvider().ComputeHash(Encoding.ASCII.GetBytes(key) )), 模式 = CipherMode.ECB, 填充 = PaddingMode.PKCS7 }.CreateEncryptor().TransformFinalBlock(bytes, 0, bytes.Length); }

标签: c# encryption cryptography md5


【解决方案1】:

所以你想检查文件是否存在?

那么你可以使用简单的IF

  if(File.Exist(patch.SAF_Path))
{
BinaryReader br = new BinaryReader(File.OpenRead(patch.SAF_Path));
            br.BaseStream.Position = (long)f.Start;
            byte[] file = br.ReadBytes((int)f.Length);
            br.Dispose();
            BinaryWriter bw = new BinaryWriter(File.OpenWrite(SAF_Path));
            bw.BaseStream.Position = (int)new FileInfo(SAF_Path).Length;
            f.Start = (ulong)bw.BaseStream.Position;
            bw.Write(file);
            bw.Dispose();
            current_folder.Files.Add(f);

            f.Start = BitConverter.ToUInt64(file, Offset);
            Offset += 8;

            f.Length = BitConverter.ToUInt64(file, Offset);
            Offset += 8;
}
else
{
//do somethings else downloand this file maybe and try read it
}

【讨论】:

  • 嗯,文件存在。我遇到的问题是它必须阅读的代码。
猜你喜欢
  • 1970-01-01
  • 2018-12-19
  • 1970-01-01
  • 1970-01-01
  • 2013-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多