C# 關於壓縮、加密、解壓問題
本文探討的是關於C#TXT檔案的壓縮、加密以及解壓問題,採用的是金鑰方式,可以先進行加密再進行壓縮包匯入到桌面。
介面如下:
原始檔:想要壓縮的TXT檔案,裡邊必須有東西,不然程式碼會報錯
壓縮檔案:要壓縮到桌面的路徑
程式碼如下:
public partial class 壓縮檔案 : Form
{
public 壓縮檔案()
{
InitializeComponent();
}
string str = "";
string a = "";
private void btnYasuo_Click(object sender, EventArgs e)
{
if(String.IsNullOrEmpty(textBox1.Text))
{
MessageBox.Show("請選擇原始檔","資訊提示");
return;
}
if (String.IsNullOrEmpty(textBox2.Text))
{
MessageBox.Show("請輸入壓縮檔名", "資訊提示");
return;
}
string str1 = textBox1.Text;
string str2 = textBox2.Text.Trim() + ".zip";
str = str2;
byte[] myByte = null;
FileStream myStream = null;
FileStream myDesStream = null;
GZipStream myComStream = null;
try
{
myStream = new FileStream(str1, FileMode.Open, FileAccess.Read, FileShare.Read);
myByte = new byte[myStream.Length];
myStream.Read(myByte, 0, myByte.Length);
myDesStream = new FileStream(str2, FileMode.OpenOrCreate, FileAccess.Write);
myComStream = new GZipStream(myDesStream, CompressionMode.Compress, true);
myComStream.Write(myByte, 0, myByte.Length);
MessageBox.Show("壓縮檔案完成");
}
catch(Exception )
{
}
finally
{
myStream.Close();
myComStream.Close();
myDesStream.Close();
}
}
private void btnJiami_Click(object sender, EventArgs e)
{
if(textBox1 .Text =="")
{
MessageBox.Show("請選擇要加密的檔案");
}
else
{
try
{
string strPath = textBox1.Text;
int intLent = strPath.LastIndexOf("\\") + 1;
int intLong = strPath.Length;
string strName = strPath.Substring(intLent, intLong - intLent);
int intTxt = strName.LastIndexOf(".");
int intTextLeng = strName.Length;
string strTxt = strName.Substring(intTxt, intTextLeng - intTxt);
strName = strName.Substring(0, intTxt);
string strOutName = strPath.Substring(0, strPath.LastIndexOf("\\") + 1) + strName +"Out"+ strTxt;
a = strOutName;
str = strPath.Substring(0, strPath.LastIndexOf("\\") + 1) + strName + "Out";
byte[] key = { 24, 55, 102, 24, 98, 26, 67, 29, 84, 19, 37, 118, 104, 85, 121, 27, 93, 86, 24, 55, 102, 27, 98, 26, 67, 29, 9, 2, 49, 69, 73, 92 };
byte[] IV = { 22, 56, 82, 77, 84, 31, 74, 24, 55, 102, 27, 98, 26, 67, 29, 99 };
RijndaelManaged myRijndael = new RijndaelManaged();
FileStream fsOut = File.Open(strOutName, FileMode.Create, FileAccess.Write);
FileStream fsIn = File.Open(strPath, FileMode.Open, FileAccess.Read);
CryptoStream csDexrypt = new CryptoStream(fsOut, myRijndael.CreateEncryptor(key, IV), CryptoStreamMode.Write);
BinaryReader br = new BinaryReader(fsIn);
csDexrypt.Write(br.ReadBytes((int)fsIn.Length), 0, (int)fsIn.Length);
csDexrypt.FlushFinalBlock();
csDexrypt.Close();
fsIn.Close();
fsOut.Close();
if(MessageBox.Show ("加密成功!加密後的檔名及路徑為:\n"+strOutName +",是否刪除原始檔","資訊提示",MessageBoxButtons.YesNo)==DialogResult .Yes )
{
File.Delete(strPath);
//textBox1.Text = "";
}
else
{
//textBox1.Text = "";
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
finally
{
yasuo();
}
}
}
private void btnJiemi_Click(object sender, EventArgs e)
{
if(textBox1 .Text =="")
{
MessageBox.Show("請選擇解密的檔案路徑");
}
else
{
string strPath = textBox1.Text;
int intLent = strPath.LastIndexOf("\\") + 1;
int intLong = strPath.Length;
string strName = strPath.Substring(intLent, intLong - intLent);
int intTxt = strName.LastIndexOf(".");
int intTextLeng = strName.Length;
string strTxt = strName.Substring(intTxt, intTextLeng - intTxt);
strName = strName.Substring(0, intTxt);
if(strName .LastIndexOf ("Out")!=-1)
{
strName = strName.Substring(0, strName.LastIndexOf("Out"));
}
else
{
strName = strName + "In";
}
string strInName = strPath.Substring(0, strPath.LastIndexOf("\\") + 1) + strName + ".txt";
byte[] key = { 24, 55, 102, 24, 98, 26, 67, 29, 84, 19, 37, 118, 104, 85, 121, 27, 93, 86, 24, 55, 102, 27, 98, 26, 67, 29, 9, 2, 49, 69, 73, 92 };
byte[] IV = { 22, 56, 82, 77, 84, 31, 74, 24, 55, 102, 27, 98, 26, 67, 29, 99 };
RijndaelManaged myRijndael = new RijndaelManaged();
FileStream fsOut = File.Open(strPath, FileMode.Open, FileAccess.Read);
CryptoStream csDexrypt = new CryptoStream(fsOut, myRijndael.CreateEncryptor(key, IV), CryptoStreamMode.Read);
StreamReader sr = new StreamReader(csDexrypt);
StreamWriter sw = new StreamWriter(strInName);
sw.Write(sr.ReadToEnd());
sw.Flush();
sw.Close();
sr.Close();
fsOut.Close();
if (MessageBox.Show("解密成功!解密後的檔名及路徑為:\n" + strInName + ",是否刪除原始檔", "資訊提示", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
File.Delete(strPath);
textBox1.Text = "";
}
else
{
textBox1.Text = "";
}
}
}
private void yasuo()
{
if (String.IsNullOrEmpty(textBox1.Text))
{
MessageBox.Show("請選擇原始檔", "資訊提示");
return;
}
if (String.IsNullOrEmpty(textBox2.Text))
{
MessageBox.Show("請輸入壓縮檔名", "資訊提示");
return;
}
string str1 = a;
string str2 = str+ ".zip";
byte[] myByte = null;
FileStream myStream = null;
FileStream myDesStream = null;
GZipStream myComStream = null;
try
{
myStream = new FileStream(str1, FileMode.Open, FileAccess.Read, FileShare.Read);
myByte = new byte[myStream.Length];
myStream.Read(myByte, 0, myByte.Length);
myDesStream = new FileStream(str2, FileMode.OpenOrCreate, FileAccess.Write);
myComStream = new GZipStream(myDesStream, CompressionMode.Compress, true);
myComStream.Write(myByte, 0, myByte.Length);
MessageBox.Show("壓縮檔案完成");
}
catch (Exception)
{
}
finally
{
myStream.Close();
myComStream.Close();
myDesStream.Close();
}
}
本文程式碼中可能對於上述按鈕實現的功能不太對應,可能點選壓縮或者加密按鈕,直接就會實現兩個功能,因為兩個方法之間會有呼叫關係,我寫的是加密完成之後直接進行壓縮到桌面,而不是說壓縮、加密分開實現,當然你也可以把方法中呼叫的另一個方法剝離開即可實現按鈕對應的功能。
謝謝大家!
相關文章
- 關於azkaban上傳job壓縮包報錯問題的解決方案
- Linux壓縮解壓Linux
- CentOS 壓縮解壓CentOS
- 加密的壓縮包加密
- linux下壓縮解壓縮命令Linux
- linuxtar解壓和壓縮Linux
- linux分卷壓縮解壓Linux
- Mac壓縮檔案怎麼加密?BetterZip加密Word壓縮檔案教程Mac加密
- Linux tar分卷壓縮與解壓縮Linux
- c#壓縮檔案C#
- Redshift關於列的壓縮格式
- 分卷壓縮怎麼解壓 快速解壓電腦分卷壓縮檔案方法
- linux 高效壓縮工具之xz的壓縮解壓使用Linux
- Linux打包壓縮解壓工具Linux
- .NET 壓縮/解壓檔案
- Keka for Mac(壓縮解壓工具)Mac
- Keka for Mac壓縮解壓工具Mac
- MyZip for mac解壓壓縮工具Mac
- 淺談在c#中使用Zlib壓縮與解壓的方法C#
- Linux下的tar壓縮解壓縮命令詳解Linux
- Linux 常用的壓縮與解壓縮命令詳解Linux
- Nginx網路壓縮 CSS壓縮 圖片壓縮 JSON壓縮NginxCSSJSON
- Linux基於tar與openssl加密解密壓縮包Linux加密解密
- node ~ zip壓縮 && 檔案加密加密
- C# 壓縮PDF檔案C#
- android 關於Bitmap壓縮處理解析Android
- Mac壓縮解壓工具:Keka for MacMac
- A-Zippr for Mac 壓縮解壓工具Mac
- linux下壓縮、解壓命令大全Linux
- CentOS中zip壓縮和unzip解壓縮命令詳解CentOS
- webpack4 css打包壓縮問題WebCSS
- c# 上傳壓縮包 解壓,遍歷資料夾和檔案C#
- linux 下面壓縮、解壓.rar檔案Linux
- java中檔案如何加密壓縮?Java加密
- 檔案壓縮和解壓縮
- FastZip for Mac_Mac解壓軟體_Mac壓縮_Mac解壓工具ASTMac
- 壓縮解壓軟體:解壓專家Oka for Mac中文版Mac
- Flutter asset檔案被壓縮的問題Flutter