1、關於Aquiliagit
由於以前遇到過一個問題:就是有一個二進制文件,要把它保存到一個只支持ASCII字符的文本文件中,而且要作到可恢復成與以前如出一轍的二進制文件。今天正好有時間,就用VB.NET寫了一個程序,取名叫Aquilia,用來實現這個功能。函數
這個程序的源碼已經上傳到GitAtOSC上,地址爲:http://git.oschina.net/Tsybius2014/Aquilia_Transcoderui
2、製做一個樣本文件HelloWorld.exe加密
創建一個C#控制檯應用程序,手動添加引用「System.Windows.Forms」,而後輸入代碼以下:spa
namespace HelloWorld { class Program { static void Main(string[] args) { System.Windows.Forms.MessageBox.Show("Hello World!"); } } }
編譯好的exe文件即爲HelloWorld.exe,這個exe在運行後會彈出一個MessageBox上面寫着「Hello World!」.net
3、Aquilia代碼命令行
1)ModuleMain.vb:主函數code
Imports System.IO Module ModuleMain ''' <summary> ''' 主函數 ''' </summary> ''' <remarks></remarks> Sub Main() Dim Args As String() = System.Environment.GetCommandLineArgs() If Args.Count <> 4 Then PrintUsage() Pause() Exit Sub End If If (Args(1) <> "-e" And Args(1) <> "--encode" And _ Args(1) <> "-d" And Args(1) <> "--decode") Then PrintUsage() Pause() Exit Sub End If If Not File.Exists(Args(2)) Then Console.WriteLine("文件:{0} 不存在", Args(2)) End If If File.Exists(Args(3)) Then Console.Write("文件:{0} 已存在,要覆蓋嗎 (y/n)", Args(3)) While True Dim temp As String = Console.ReadLine If temp = "Y" Or temp = "y" Then Exit While ElseIf temp = "N" Or temp = "n" Then Exit Sub Else Console.Write("文件:{0} 已存在,要覆蓋嗎 (y/n)", Args(3)) Continue While End If End While End If If Args(1) = "-e" Or Args(1) = "--encode" Then FileConverHelper.Encode(Args(2), Args(3)) ElseIf Args(1) = "-d" Or Args(1) = "--decode" Then FileConverHelper.Decode(Args(2), Args(3)) End If Console.WriteLine("轉換完畢!") Console.WriteLine("新生成的文件已被保存到: {0}", Args(3)) Pause() End Sub ''' <summary> ''' 打印程序用法 ''' </summary> ''' <remarks></remarks> Sub PrintUsage() Dim Usage(6) As String Usage(0) = "Aquilia程序使用方法" Usage(1) = "Aquilia [-e|--encode|-d|--decode] 文件1 文件2" Usage(2) = "第二個參數爲-e或--encode時,Aquilia將文件1加密後寫入到文件2中" Usage(3) = "第二個參數爲-d或--decode時,Aquilia將文件1解密後寫入到文件2中" Usage(4) = "示例參數1: -e HelloWorld.exe output.txt" Usage(5) = "示例參數2: -d output.txt HelloWorld2.exe" Console.WriteLine(Join(Usage, vbCrLf)) End Sub ''' <summary> ''' 按任意鍵繼續 ''' </summary> ''' <remarks></remarks> Sub Pause() Console.WriteLine("按任意鍵繼續") System.Console.ReadKey() System.Console.Write(Chr(8) + " ") '刪除按下的「任意鍵」字符 End Sub End Module
2)FileConverHelper.vb:orm
Imports System.IO Imports System.Text Public Class FileConverHelper ''' <summary> ''' 將file1內容的以文本的形式輸出到file2中 ''' </summary> ''' <param name="file1"></param> ''' <param name="file2"></param> ''' <remarks></remarks> Public Shared Sub Encode(file1 As String, file2 As String) Dim fi As FileInfo = New FileInfo(file1) Dim fs1 As New FileStream(file1, FileMode.Open, FileAccess.Read, FileShare.Read) Dim buffer(fi.Length) As Byte fs1.Read(buffer, 0, CInt(fi.Length)) fs1.Close() Using sw As New StreamWriter(file2) For i As Integer = 0 To buffer.Length - 1 If i <> buffer.Length - 1 Then sw.Write(buffer(i) & ",") Else sw.Write(buffer(i)) End If Next End Using 'Dim sb As New StringBuilder 'For Each byt In buffer ' sb.Append(byt + ",") 'Next End Sub ''' <summary> ''' 將file1文本以二進制的形式輸出到file2中 ''' </summary> ''' <param name="file1"></param> ''' <param name="file2"></param> ''' <remarks></remarks> Public Shared Sub Decode(file1 As String, file2 As String) Dim s() As String Using sr As New StreamReader(file1) s = sr.ReadToEnd().Split(",") End Using Dim buffer(s.Length - 1) As Byte For i As Integer = 0 To buffer.Length - 1 buffer(i) = CInt(s(i)) Next Dim fs2 As New FileStream(file2, FileMode.Create, FileAccess.Write, FileShare.None) fs2.Write(buffer, 0, buffer.Length) fs2.Close() End Sub End Class
4、運行結果xml
將HelloWorld.exe複製到Aquilia.exe同一個目錄下,而後輸入下面兩條命令
Aquilia.exe -e HelloWorld.exe output.txt Aquilia.exe -d output.txt HelloWorld2.exe
先將HelloWorld.exe轉碼後輸出到output.txt下,再把output.txt中的內容重寫到HelloWorld2.exe中
生成的output.txt內容以下:
重寫成的HelloWorld2.exe,雙擊後,也會輸出內容爲「Hello World!」的MessageBox
附:VB程序集中的「啓動選項→命令行參數」,保存在文件Aquilia.vbproj.user中(Aquilia爲本程序集的名稱)
<?xml version="1.0" encoding="utf-8"?> <Project ToolsVersion="4.0" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <PropertyGroup Condition="'$(Configuration)|$(Platform)' == 'Debug|AnyCPU'"> <StartArguments>-e HelloWorld.exe output.txt</StartArguments> </PropertyGroup> </Project>
提交Git的時候不要忘了這個文件
END