对象加密成文件,解密成对象 (读取字节法,不用序列化 反序列化)
2012-11-12 17:30
295 查看
/// <summary>
/// 保存文件
/// </summary>
public void GO()
{
using (FileStream stream = new FileStream(@"D:\\a.c", FileMode.Create, FileAccess.ReadWrite, FileShare.None))
{
DO对象 dx = new DO对象();
using (MemoryStream ms = new MemoryStream())
{
//将对象转换成json字符串(此处转换方法省略)
string str = Helper.JSONSerializeObject(dx);
//取字节
GZipStream zip = new GZipStream(stream, CompressionMode.Compress);
BinaryFormatter formater = new BinaryFormatter();
formater.Serialize(ms, str);
byte[] buffer = ms.ToArray();
zip.Write(buffer, 0, buffer.Length);
zip.Close();
}
}
}
/// <summary>
/// 读取文件
/// </summary>
/// <returns></returns>
public DO对象 GO1()
{
DO对象 dx = null;
using (FileStream stream = new FileStream(@"D:\\a.c", FileMode.Open, FileAccess.ReadWrite, FileShare.None))
{
GZipStream zip = new GZipStream(stream, CompressionMode.Decompress);
{
BinaryFormatter formater = new BinaryFormatter();
object obj = formater.Deserialize(zip);
//将json字符串转换成对象(此处转换方法省略)
dx = Helper.JSONDeserializeObject(obj.ToString());
zip.Close();
}
}
return dx;
}
/// 保存文件
/// </summary>
public void GO()
{
using (FileStream stream = new FileStream(@"D:\\a.c", FileMode.Create, FileAccess.ReadWrite, FileShare.None))
{
DO对象 dx = new DO对象();
using (MemoryStream ms = new MemoryStream())
{
//将对象转换成json字符串(此处转换方法省略)
string str = Helper.JSONSerializeObject(dx);
//取字节
GZipStream zip = new GZipStream(stream, CompressionMode.Compress);
BinaryFormatter formater = new BinaryFormatter();
formater.Serialize(ms, str);
byte[] buffer = ms.ToArray();
zip.Write(buffer, 0, buffer.Length);
zip.Close();
}
}
}
/// <summary>
/// 读取文件
/// </summary>
/// <returns></returns>
public DO对象 GO1()
{
DO对象 dx = null;
using (FileStream stream = new FileStream(@"D:\\a.c", FileMode.Open, FileAccess.ReadWrite, FileShare.None))
{
GZipStream zip = new GZipStream(stream, CompressionMode.Decompress);
{
BinaryFormatter formater = new BinaryFormatter();
object obj = formater.Deserialize(zip);
//将json字符串转换成对象(此处转换方法省略)
dx = Helper.JSONDeserializeObject(obj.ToString());
zip.Close();
}
}
return dx;
}
相关文章推荐
- 关于对象实例序列化加密解密并写入文件的技巧
- 一个经过改良的XMLHelper(包含了序列化,反序列化,创建xml文件,读取节点,C#对象与xml文件的相互转换等等。)
- [分享][原创] C#序列化/反序列化对象.将对象写入文件,从文件读取对象.
- C#序列化/反序列化对象.将对象写入文件,从文件读取对象.
- c# 通过修改图片对象的文件的字节 变相"加密""解密"图片在磁盘的存储
- c# 通过修改图片对象的文件的字节 变相"加密""解密"图片在磁盘的存储
- C#DES对较大文件和文件字节数组对象加密解密
- C#对文件的字节加密/解密-可逆
- 生成RSA密钥、保存到文件、从文件读取、加密、解密等操作。
- JAVA基础 day23 IO流的其他类 (对象的序列化 管道流 RandomAccessFile类(随机读取文件) DateStream(操作基本数据类型的流) 操作数组的流 )字符编码问题
- Java对象序列化文件追加对象的问题,以及Java的读取多个对象的问题解决方法。
- XStream java对象和json文件进行序列化和反序列化
- 读取文件,解密,加密,写入文件,存储过程的应用
- Unity JSON文件的写入(序列化)与读取(反序列化)、以及自定义JSON编辑器
- 谁能在同一文件序列化多个对象并随机读写(反序列化)?BinaryFormatter、SoapFormatter、XmlSerializer还是BinaryReader
- 黑马程序员_<<IO扩展对象和编码(序列化,管道流,字节(符)数组),随机访问文件>>
- 二进制BinaryFormatter 泛型 序列化与反序列化 (保存文件到本地和读取)
- 文件写入和读取,加密解密,xml例
- Java 对象直接写入文件 --序列化和反序列化
- java对象序列化、gzip压缩解压缩、加密解密