App007 加密解釋

App007 加密解釋

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
public bool DoEncryption(string inputFilename, string outputFileName, ref StringBuilder errorMessage)
{ //取得來源檔案路徑
WebAPI_Example_Common.Model.Sys.FileUpload.Settings uploadFileSetting = ConfigurationHelper.GetConfiguration<WebAPI_Example_Common.Model.Sys.FileUpload.Settings>(CommonEnum.CONFIGURATION_TYPE.FILE_UPLOAD_SETTING);
string uploadLocalPath = uploadFileSetting.FileUploadPaths.First(x => x.FileUploadId == uploadFileSetting.FilePath).FileUploadPath;

//取得加密檔案路徑
Settings fileSetting = ConfigurationHelper.GetConfiguration<Settings>(CommonEnum.CONFIGURATION_TYPE.ENCRYPT_SETTING);
string localPath = fileSetting.EncryptPaths.First(x => x.EncryptId == fileSetting.FilePath).EncryptPath;

bool status = true;

try
{
// 建立一個 ProcessStartInfo 物件,用於指定要執行的外部程式和參數
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = $"{localPath}encrypt.exe"; // 指定外部程式的路徑
startInfo.Arguments = $"{uploadLocalPath}{inputFilename} {uploadLocalPath}{outputFileName} {"00963112"}"; // 傳遞給外部程式的參數
startInfo.WorkingDirectory = localPath;
startInfo.UseShellExecute = false; // 不使用操作系統外殼啟動外部程式
startInfo.RedirectStandardOutput = true; // 重定向標準輸出

if (!File.Exists(startInfo.FileName))
{
errorMessage.AppendLine("加密檔案不存在"+ startInfo.FileName + ",加密失敗");
status = false;
}
if (!File.Exists($"{uploadLocalPath}{inputFilename}"))
{
errorMessage.AppendLine("來源檔案不存在" + $"{uploadLocalPath}{inputFilename}" + ",加密失敗" );
status = false;
}
//檔案路徑正確再加密
if (status)
{
// 建立 Process 物件並設定啟動資訊
Process process = new Process();
process.StartInfo = startInfo;

// 啟動外部程式
process.Start();

// 等待外部程式執行完畢
process.WaitForExit();

// 讀取外部程式的輸出
string output = process.StandardOutput.ReadToEnd();
//string[] sArray = output.Split(" 回應 ");

// 關閉 Process 物件
process.Close();

if (!File.Exists($"{uploadLocalPath}{outputFileName}"))
{
errorMessage.AppendLine("目的檔案不存在" + $"{uploadLocalPath}{outputFileName}" + ",加密失敗");
}
}
}
catch (Exception ex)
{
// 處理例外狀況
errorMessage.AppendLine("執行外部程式時發生錯誤:" + ex.Message);
}
if (errorMessage.Length > 0)
{
return false;
}

return true;
}

這段是如何作 encrypt 加密的?

這段程式碼的目的是通過呼叫外部的加密程式來對檔案進行加密。以下是這段程式碼的工作原理:

  1. 取得設定檔案路徑

    • 程式碼使用 ConfigurationHelper.GetConfiguration 方法從設定檔案中取得上傳檔案和加密檔案的路徑設定。
    • uploadLocalPathlocalPath 分別是上傳檔案的路徑和加密檔案的路徑。
  2. 初始化 ProcessStartInfo

    • ProcessStartInfo 用來指定要執行的外部程式及其參數。
    • startInfo.FileName 設定為加密程式 encrypt.exe 的路徑。
    • startInfo.Arguments 設定為加密程式的參數,包括來源檔案路徑 (inputFilename)、目的檔案路徑 (outputFileName)、以及一個硬編碼的密鑰或其他必要參數(這裡是 "00963112")。
  3. 檢查檔案是否存在

    • 檢查加密程式 (encrypt.exe) 是否存在。
    • 檢查來源檔案 (inputFilename) 是否存在。
    • 如果其中任何檔案不存在,將錯誤信息添加到 errorMessage 並設置 statusfalse
  4. 執行加密程式

    • 如果檔案檢查通過,建立 Process 物件並設定啟動資訊 (startInfo)。
    • 啟動外部程式 (process.Start()),並等待外部程式完成執行 (process.WaitForExit()),確保加密處理已經完成。
    • 讀取外部程式的標準輸出(如果有的話)。
  5. 檢查加密結果

    • 檢查加密後的目的檔案 (outputFileName) 是否存在。
    • 如果目的檔案不存在,將錯誤信息添加到 errorMessage
  6. 例外處理

    • 捕獲並處理任何在執行外部程式時發生的異常,將錯誤信息添加到 errorMessage
  7. 返回結果

    • 如果 errorMessage 中有錯誤信息,返回 false,否則返回 true,表示加密成功。

總結來說,這段程式碼的加密過程依賴於外部的加密程式 encrypt.exe,並通過檢查檔案存在與否和捕獲錯誤來確保加密操作的成功。如果所有檔案和操作都正確,程式碼會執行加密並檢查結果,最終返回操作結果。

沒有看到執行 encrypt.exe 的程式碼

在您提供的程式碼中,encrypt.exe 的執行是通過 Process 類來完成的,下面是具體的步驟和程式碼片段來說明如何執行 encrypt.exe

  1. **設定 ProcessStartInfo**:

    1
    2
    3
    4
    5
    6
    ProcessStartInfo startInfo = new ProcessStartInfo();
    startInfo.FileName = $"{localPath}encrypt.exe"; // 指定外部程式的路徑
    startInfo.Arguments = $"{uploadLocalPath}{inputFilename} {uploadLocalPath}{outputFileName} {"00963112"}"; // 傳遞給外部程式的參數
    startInfo.WorkingDirectory = localPath;
    startInfo.UseShellExecute = false; // 不使用操作系統外殼啟動外部程式
    startInfo.RedirectStandardOutput = true; // 重定向標準輸出
    • startInfo.FileName:設置為加密程式 encrypt.exe 的路徑,這個程式是用來進行加密操作的。
    • startInfo.Arguments:設置為傳遞給 encrypt.exe 的參數,包括來源檔案、目的檔案以及密鑰或其他參數。
    • startInfo.WorkingDirectory:設置為 encrypt.exe 的工作目錄。
    • startInfo.UseShellExecute:設置為 false,表示不使用操作系統外殼來啟動外部程式。
    • startInfo.RedirectStandardOutput:設置為 true,表示要重定向外部程式的標準輸出,以便可以讀取其輸出內容。
  2. **建立並啟動 Process**:

    1
    2
    3
    4
    5
    6
    7
    8
    Process process = new Process();
    process.StartInfo = startInfo;

    // 啟動外部程式
    process.Start();

    // 等待外部程式執行完畢
    process.WaitForExit();
    • process.Start():啟動 encrypt.exe 程式。
    • process.WaitForExit():等待 encrypt.exe 執行完成,直到外部程式結束執行。
  3. 讀取外部程式的輸出

    1
    string output = process.StandardOutput.ReadToEnd();
    • process.StandardOutput.ReadToEnd():讀取 encrypt.exe 執行過程中的標準輸出,以便獲取任何錯誤信息或其他輸出內容。
  4. **關閉 Process**:

    1
    process.Close();
    • 關閉 Process 物件以釋放資源。

整體流程

  1. 建立 ProcessStartInfo 物件來配置外部程式的路徑和參數。
  2. 檢查檔案存在性,確保 encrypt.exe 和來源檔案都存在。
  3. 建立 Process 物件,並設置啟動資訊。
  4. **啟動 encrypt.exe**,並等待其執行完成。
  5. 讀取標準輸出,以檢查任何錯誤或結果。
  6. 關閉 Process 物件,完成操作。

這段程式碼的主要作用是通過呼叫外部的加密程式 encrypt.exe,進行檔案的加密處理,並檢查加密是否成功。

那要加密的檔案名稱是放在startInfo.Arguments 這裡嗎?

是的,您完全正確。要加密的檔案名稱和其他相關參數都是通過 startInfo.Arguments 這個屬性來傳遞給 encrypt.exe 程式的。下面是如何在 startInfo.Arguments 中設置這些參數的詳細說明:

startInfo.Arguments 的設置

在您的程式碼中,這一行負責設置要傳遞給外部程式的參數:

1
startInfo.Arguments = $"{uploadLocalPath}{inputFilename} {uploadLocalPath}{outputFileName} {"00963112"}";

這一行的作用是組合外部程式 encrypt.exe 的命令行參數,這些參數將被傳遞給 encrypt.exe 執行。具體的參數包括:

  1. 來源檔案 ({uploadLocalPath}{inputFilename}):這是要被加密的原始檔案的路徑。uploadLocalPath 是來源檔案的路徑前綴,inputFilename 是來源檔案的檔名,兩者組合起來形成完整的檔案路徑。

  2. 目的檔案 ({uploadLocalPath}{outputFileName}):這是加密後產生的檔案的路徑。uploadLocalPath 是目的檔案的路徑前綴,outputFileName 是目的檔案的檔名,兩者組合起來形成完整的檔案路徑。

  3. 密鑰或其他參數 ("00963112"):這是傳遞給 encrypt.exe 的其他參數,例如密鑰或識別碼。這個參數的具體含義取決於 encrypt.exe 程式的實現。

完整的命令行參數示例

假設:

  • uploadLocalPath = "C:\\Uploads\\"
  • inputFilename = "data.txt"
  • outputFileName = "data_encrypted.txt"
  • 密鑰為 "00963112"

那麼 startInfo.Arguments 的值將會是:

1
"C:\\Uploads\\data.txt C:\\Uploads\\data_encrypted.txt 00963112"

這樣,當 encrypt.exe 被執行時,它將會接收這些參數並進行相應的加密操作。

總結

startInfo.Arguments 是用來指定要傳遞給 encrypt.exe 程式的參數。這些參數包括來源檔案、目的檔案和其他必要的加密參數。程式將這些參數組合成一個字串,並在啟動 encrypt.exe 時傳遞給它,以便進行加密處理。