| 功能 | 說明 |
|---|---|
| 任意長度字串加解密 | 自動分塊處理,無 8 位元組限制 | 金鑰長度彈性 | 不足 8 位元組自動補齊 |
| 特殊字元支援 | 換行、Tab 等控制字元完整保留 |
| 密文顯示模式 | Hex / Base64 / Binary 三選一 |
| 三重 DES (3DES) | E-D-E 模式,雙金鑰加強安全性 |
| 檔案加解密 | 串流式處理,大檔案不佔記憶體,含進度條 |
// 金鑰初始化(產生 16 個 28-bit 子金鑰)
void InitializeKey(char* srcBytes, unsigned int keyN); // keyN: 0 或 1 (3DES 用)
// 8 位元組區塊加密
void EncryptData(char* _srcBytes, unsigned int keyN);
// 16 位十六進制字串解密
void DecryptData(char* _srcBytes, unsigned int keyN);
// 任意長度字串加密
void EncryptAnyLength(char* _srcBytes, unsigned int _bytesLength, unsigned int keyN);
// 任意長度字串解密
void DecryptAnyLength(char* _srcBytes, unsigned int _bytesLength, unsigned int keyN);
// 編碼轉換輔助函數
void Bytes2Bits(char *srcBytes, char* dstBits, unsigned int sizeBits);
void Bits2Bytes(char *dstBytes, char* srcBits, unsigned int sizeBits);
void Int2Bits(unsigned int srcByte, char* dstBits);
void Bits2Hex(char *dstHex, char* srcBits, unsigned int sizeBits);
void Hex2Bits(char *srcHex, char* dstBits, unsigned int sizeBits);
// 結果取得
char* GetCiphertextInBinary();
char* GetCiphertextInHex();
char* GetCiphertextInBytes();
char* GetPlaintext();
char* GetCiphertextAnyLength();
char* GetPlaintextAnyLength();
// 初始化雙金鑰
myDES->InitializeKey(szSourceKey1, 0);
if (bIs3DES)
{
myDES->InitializeKey(szSourceKey2, 1);
}
// 3DES 解密流程:D(key0) - E(key1) - D(key0)
myDES->DecryptAnyLength(szCiphertextData, ConvertOtherFormat2Ciphertext(strCiphertext), 0);
myDES->EncryptAnyLength(myDES->GetPlaintextAnyLength(), strlen(myDES->GetPlaintextAnyLength()), 1);
myDES->DecryptAnyLength(myDES->GetCiphertextAnyLength(), strlen(myDES->GetCiphertextAnyLength()), 0);
若需使用 16 進制金鑰 或 二進制明文,呼叫轉換函數統一轉為 char* (Byte 陣列) 再餵入核心函數:
// 範例:16 進制金鑰 → Byte 陣列
Hex2Bits(hexKey, byteKey, keyBitLength);
InitializeKey(byteKey, 0);
void CDESToolDlg::OnBnClickedButtonFileGoE()
{
FILE *fpSrc, *fpDst;
CString szSrcPath, szDstPath, szKey1, szKey2;
char buff[8] = {0};
long fileSize = 0, hasDone = 0;
// 取得 UI 輸入路徑與金鑰
edtFileSrc.GetWindowText(szSrcPath);
edtFileDst.GetWindowText(szDstPath);
edtFileKey1.GetWindowText(szKey1);
edtFileKey2.GetWindowText(szKey2);
if ((fpSrc = fopen(szSrcPath, "rb")) == NULL) { MessageBox("開啟來源檔失敗"); return; }
if ((fpDst = fopen(szDstPath, "wb")) == NULL) { MessageBox("開啟目的檔失敗"); return; }
fseek(fpSrc, 0, SEEK_END);
fileSize = ftell(fpSrc);
rewind(fpSrc);
prcsbar.SetRange(0, 100);
prcsbar.ShowWindow(SW_SHOW);
myDES->InitializeKey(szKey1.GetBuffer(), 0);
if (chbFileTDES.GetCheck()) // 3DES
{
myDES->InitializeKey(szKey2.GetBuffer(), 1);
while (!feof(fpSrc))
{
memset(buff, 0, 8);
fread(buff, sizeof(char), 8, fpSrc);
// E(key0) - D(key1) - E(key0)
myDES->EncryptData(buff, 0);
myDES->DecryptData(myDES->GetCiphertextInBytes(), 1);
myDES->EncryptData(myDES->GetPlaintext(), 0);
fwrite(myDES->GetCiphertextInBytes(), sizeof(char), 8, fpDst);
hasDone += 8;
prcsbar.SetPos((int)(hasDone * 100 / fileSize));
}
}
else // 單 DES
{
while (!feof(fpSrc))
{
memset(buff, 0, 8);
fread(buff, sizeof(char), 8, fpSrc);
myDES->EncryptData(buff, 0);
fwrite(myDES->GetCiphertextInBytes(), sizeof(char), 8, fpDst);
hasDone += 8;
prcsbar.SetPos((int)(hasDone * 100 / fileSize));
}
}
fclose(fpSrc);
fclose(fpDst);
prcsbar.SetPos(100);
MessageBox("加密完成!");
prcsbar.ShowWindow(SW_HIDE);
}
DES 2.1 提供完整、易用的加密工具與原始碼,適合學習區塊加密原理、維護舊系統或快速原型開發。新專案請直接採用 System.Security.Cryptography.Aes 等現代演算法。