是否将CRC值保存在文件中,而不更改实际的CRC校验和? [英] Save a CRC value in a file, without altering the actual CRC Checksum?

查看:218
本文介绍了是否将CRC值保存在文件中,而不更改实际的CRC校验和?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将自己在类中定义的一些对象保存到File。 (保存流数据)。

I am saving some Objects I have defined from my own classes, to File. (saving the stream data).

这很好,但是我希望能够在File中存储该文件的CRC校验和。

That is all fine, but I would like to be able to store in the File the CRC checksum of that File.

然后,每当我的应用程序尝试打开文件时,它都可以读取内部存储的CRC值。

Then, whenever my Application attemps to Open a File, it can read the internally stored CRC value.

然后执行检查在实际文件上,如果文件的CRC与内部存储的CRC值匹配,我可以正常处理该文件,否则显示一条错误消息,指出该文件无效。

Then perform a check on the actual File, if the CRC of the File matches the internally stored CRC value I can process the File normally, otherwise display an error message to say the File is not valid.

我需要一些有关如何执行此操作的建议,我想我可以做这样的事情:

I need some advice on how to do this though, I thought I could do something like this:


  • 从应用程序中保存文件

  • 计算已保存文件的CRC。

  • 编辑存储了CRC值的已保存文件。

  • 打开文件时,请检查CRC是否与内部CRC值匹配。

  • Save the File from my Application.
  • Calculate the CRC of the Saved File.
  • Edit the Saved File storing the CRC Value.
  • Whenever a File is Opened, Check the CRC matches internal CRC Value.

问题是,只有一个字节的数据在文件中更改,导致CRC校验和完全不同-例如

Problem is, as soon as a single Byte of Data is altered in the File, results in the CRC checksum being completely different - as expected.

推荐答案

简单地说,您需要从校验和计算中排除用于存储校验和的字节。

Simply put you need to exclude the bytes used to store the checksum from the checksum calculation.

将校验和写入文件中的最后一件事。除了校验和以外,还根据文件的内容进行计算。读取文件时,请根据校验和之前的内容计算校验和。或者,您可以将校验和写为具有随机访问权限的文件的第一个字节。只要您知道它在哪里。

Write the checksum as the last thing in the file. Calculate it based on the contents of the file apart from the checksum. When you come to read the file calculate the checksum based on the contents before the checksum. Or you could write the checksum as the first bytes of the file with random access. Just so long as you know where it is.

这篇关于是否将CRC值保存在文件中,而不更改实际的CRC校验和?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆