110,566
社区成员
发帖
与我相关
我的任务
分享
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());
}
}
}
}
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;
}
}
}