文件压缩加密解密解压问题

aotian16 2013-05-15 04:01:44
lz新手,
写了个程序用来文件压缩加密解密解压,

流程:

file <-> compress file <-> secret file

问题是在运行中会生成俩个中间文件,
一个是压缩后的中间文件,
一个是解密后的中间文件

请问如果我不想生成中间文件,而是使用内存的话要如何实现?

我用memorystream试过貌似一直不行

以下是我的代码
使用了SharpZipLib

using System;
using System.Collections.Generic;
using System.Text;

using ICSharpCode.SharpZipLib.Zip;
using System.IO;
using System.Security.Cryptography;

namespace Compress2
{
class Program
{
static void Main(string[] args)
{
if (args.Length < 3)
{
Console.WriteLine("args < 3");
return;
}

RijndaelManaged rij = new RijndaelManaged();
rij.KeySize = 128;

string fp = args[0];
string sPhysicalFilePath = args[1];
string fw = args[2];
Console.WriteLine("Encrypting begin...");

compressFile(args[0], args[0] + ".zip");
encryption(rij, args[0] + ".zip", args[0] + ".zip.jiami");
decryption(rij, args[0] + ".zip.jiami", args[0] + ".zip.jiemi");
decompressFile(args[0] + ".zip.jiemi", "tmp");
}

private static void compressFile(string inputFileName, string outPutFileName)
{
FileStream compressFileStream = File.Create(outPutFileName);
ZipOutputStream zipOpStream = new ZipOutputStream(compressFileStream);

ZipEntry entry = new ZipEntry(inputFileName);
zipOpStream.PutNextEntry(entry);

FileStream fs = File.OpenRead(inputFileName);
int bytes = 0;
byte[] buffer = new byte[4096];
do
{
bytes = fs.Read(buffer, 0, buffer.Length);
zipOpStream.Write(buffer, 0, bytes);
} while (bytes > 0);

fs.Close();
zipOpStream.Close();
}

private static void decompressFile(string inputFileName, string outPutDirName)
{
ZipInputStream zipIpStream = new ZipInputStream(File.OpenRead(inputFileName));

ZipEntry theEntry = null;
while ((theEntry = zipIpStream.GetNextEntry()) != null)
{
string directoryName = Path.GetDirectoryName(theEntry.Name);
string fileName = Path.GetFileName(theEntry.Name);
//if (directoryName != string.Empty)
//{
Directory.CreateDirectory(outPutDirName + "\\" + directoryName);
//}

if (fileName != string.Empty)
{
FileStream fs = File.Create(outPutDirName + "\\" + theEntry.Name);

int bytes = 0;
byte[] buffer = new byte[2048];
while (true)
{
bytes = zipIpStream.Read(buffer, 0, buffer.Length);
if (bytes == 0)
{
break;

}
fs.Write(buffer, 0, bytes);
}
}
}
}

//用于加密的函数
public static void encryption(RijndaelManaged rij, string readfile, string writefile)
{
try
{
byte[] key = rij.Key;
byte[] iv = rij.IV;
byte[] buffer = new byte[4096];
Rijndael crypt = Rijndael.Create();
ICryptoTransform transform = crypt.CreateEncryptor(key, iv);
//写进文件
FileStream fswrite = new FileStream(writefile, FileMode.Create);
CryptoStream cs = new CryptoStream(fswrite, transform, CryptoStreamMode.Write);
//打开文件
FileStream fsread = new FileStream(readfile, FileMode.Open);
Console.WriteLine("fsread.Length=" + fsread.Length);
int length;
//while ((length = fsread.ReadByte()) != -1)
//cs.WriteByte((byte)length);
while ((length = fsread.Read(buffer, 0, 4096)) > 0)
cs.Write(buffer, 0, (int)length);

fsread.Close();
cs.Close();
fswrite.Close();
Console.WriteLine("Encrypt Success");
}
catch (Exception e)
{
Console.WriteLine("Encrypt Faile" + e.ToString());
}
}
//用于解密的函数
public static void decryption(RijndaelManaged rij, string readfile, string writefile)
{
try
{
byte[] key = rij.Key;
byte[] iv = rij.IV;
byte[] buffer = new byte[4096];
Rijndael crypt = Rijndael.Create();
ICryptoTransform transform = crypt.CreateDecryptor(key, iv);
//读取加密后的文件
FileStream fsopen = new FileStream(readfile, FileMode.Open);
CryptoStream cs = new CryptoStream(fsopen, transform, CryptoStreamMode.Read);
//把解密后的结果写进文件
FileStream fswrite = new FileStream(writefile, FileMode.OpenOrCreate);

int length;
//while ((length = cs.ReadByte()) != -1)
//fswrite.WriteByte((byte)length);
while ((length = cs.Read(buffer, 0, 4096)) > 0)
fswrite.Write(buffer, 0, (int)length);
fswrite.Close();
cs.Close();
fsopen.Close();
Console.WriteLine("Decrypt Success");
}
catch (Exception e)
{
Console.WriteLine("Decrypt Failed" + e.ToString());
}
}
}
}
...全文
165 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
aotian16 2013-05-15
  • 打赏
  • 举报
回复
刚试了下,果然可以 多谢ls 贴上代码
using System;
using System.Collections.Generic;
using System.Text;

using ICSharpCode.SharpZipLib.Zip;
using System.IO;
using System.Security.Cryptography;

namespace Compress3
{
    class Program
    {
        static void Main(string[] args)
        {
            if (args.Length < 3)
            {
                Console.WriteLine("args < 3");
                return;
            }

            RijndaelManaged rij = new RijndaelManaged();
            rij.KeySize = 128;

            string fp = args[0];
            string sPhysicalFilePath = args[1];
            string fw = args[2];
            Console.WriteLine("Encrypting begin...");

            compressFile(args[0], args[0] + ".zip");
            decompressFile(args[0] + ".zip", "tmp");
        }

        private static void compressFile(string inputFileName, string outPutFileName)
        {
            FileStream compressFileStream = File.Create(outPutFileName);
            ZipOutputStream zipOpStream = new ZipOutputStream(compressFileStream);

            zipOpStream.SetLevel(6);
            zipOpStream.Password = encryptPassword("pass");

            ZipEntry entry = new ZipEntry(inputFileName);
            zipOpStream.PutNextEntry(entry);

            FileStream fs = File.OpenRead(inputFileName);
            int bytes = 0;
            byte[] buffer = new byte[4096];
            do
            {
                bytes = fs.Read(buffer, 0, buffer.Length);
                zipOpStream.Write(buffer, 0, bytes);
            } while (bytes > 0);

            fs.Close();
            zipOpStream.Close();
        }

        private static void decompressFile(string inputFileName, string outPutDirName)
        {
            ZipInputStream zipIpStream = new ZipInputStream(File.OpenRead(inputFileName));
            zipIpStream.Password = encryptPassword("pass");

            ZipEntry theEntry = null;
            while ((theEntry = zipIpStream.GetNextEntry()) != null)
            {
                string directoryName = Path.GetDirectoryName(theEntry.Name);
                string fileName = Path.GetFileName(theEntry.Name);
                //if (directoryName != string.Empty)
                //{
                Directory.CreateDirectory(outPutDirName + "\\" + directoryName);
                //}

                if (fileName != string.Empty)
                {
                    FileStream fs = File.Create(outPutDirName + "\\" + theEntry.Name);

                    int bytes = 0;
                    byte[] buffer = new byte[2048];
                    while (true)
                    {
                        bytes = zipIpStream.Read(buffer, 0, buffer.Length);
                        if (bytes == 0)
                        {
                            break;

                        }
                        fs.Write(buffer, 0, bytes);
                    }
                }
            }
        }

        public static string encryptPassword(string password)
        {
            string cl = password;
            string pwd = "";
            MD5 md5 = MD5.Create();
            byte[] s = md5.ComputeHash(Encoding.UTF8.GetBytes(cl));
            for (int i = 0; i < s.Length; i++)
            {
                pwd = pwd + s[i].ToString("X");
            }
            return pwd;
        }
    }
}
aotian16 2013-05-15
  • 打赏
  • 举报
回复
引用 1 楼 gomoku 的回复:
SharpZipLib本身就支持加密(比如256bit AES),何必多此一举。
是吗,多谢指教 我再研究研究 我刚学c#的,2天
gomoku 2013-05-15
  • 打赏
  • 举报
回复
SharpZipLib本身就支持加密(比如256bit AES),何必多此一举。

110,566

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧