找回密码
 立即注册
查看: 98|回复: 0

文件工具加密解密

[复制链接]
  • 打卡等级:即来则安
  • 打卡总天数:18
  • 打卡月天数:0
  • 打卡总奖励:360
  • 最近打卡:2023-04-23 12:36:40

1465

主题

1

回帖

1466

积分

超级版主

积分
1466
发表于 2026-3-27 11:24:54 | 显示全部楼层 |阅读模式
今天没有事情做 做了个文件加密工具 有一些人辛辛苦苦改出来的东西一下子被别人复制

  • #  CryptoTool 加密解密工具使用说明

  • ## 简介

  • CryptoTool 是一个功能完整的加密解密工具,支持多种加密算法,包括字符串加密/解密和文件加密/解密。

  • ---

  • ##  版本说明

  • ### 1. C++ 版本 (CryptoTool.cpp)
  • - **优点**: 无需依赖,直接编译即可使用
  • - **支持算法**: XOR、Base64、RC4、Caesar、MD5
  • - **适用场景**: 轻量级加密、嵌入式系统、无Python环境

  • ### 2. Python 版本 (crypto_tool.py)
  • - **优点**: 功能更强大,支持现代加密算法
  • - **支持算法**: Base64、XOR、AES、RSA、MD5、SHA256
  • - **适用场景**: 需要高强度加密的场景

  • ---

  • ##  C++ 版本使用

  • ### 编译

  • ```bash
  • # Windows (MinGW)
  • g++ -o CryptoTool.exe CryptoTool.cpp

  • # Windows (MSVC)
  • cl /EHsc /FeCryptoTool.exe CryptoTool.cpp

  • # Linux/Mac
  • g++ -o CryptoTool CryptoTool.cpp
  • ```

  • ### 命令行使用

  • ```bash
  • # XOR 加密
  • CryptoTool.exe -e xor "Hello World" "mykey"

  • # XOR 解密
  • CryptoTool.exe -d xor &quot;<密文>&quot; &quot;mykey&quot;

  • # Base64 编码
  • CryptoTool.exe -e64 &quot;Hello World&quot;

  • # Base64 解码
  • CryptoTool.exe -d64 &quot;SGVsbG8gV29ybGQ=&quot;

  • # MD5 哈希
  • CryptoTool.exe -md5 &quot;Hello World&quot;

  • # 文件加密
  • CryptoTool.exe -ef input.txt output.enc mykey

  • # 文件解密
  • CryptoTool.exe -df output.enc input.txt mykey
  • ```

  • ### 交互模式

  • 直接运行程序,按菜单提示操作:

  • ```bash
  • CryptoTool.exe
  • ```

  • ---

  • ##  Python 版本使用

  • ### 安装依赖

  • ```bash
  • pip install cryptography
  • ```

  • ### 命令行使用

  • ```bash
  • # Base64 编码
  • python crypto_tool.py -e64 &quot;Hello World&quot;

  • # Base64 解码
  • python crypto_tool.py -d64 &quot;SGVsbG8gV29ybGQ=&quot;

  • # XOR 加密
  • python crypto_tool.py -xor &quot;Hello World&quot; &quot;mykey&quot;

  • # MD5 哈希
  • python crypto_tool.py -md5 &quot;Hello World&quot;

  • # SHA256 哈希
  • python crypto_tool.py -sha256 &quot;Hello World&quot;

  • # 生成 AES 密钥
  • python crypto_tool.py -aes-gen

  • # 生成 RSA 密钥对
  • python crypto_tool.py -rsa-gen
  • ```

  • ### 交互模式

  • ```bash
  • python crypto_tool.py
  • ```

  • ---

  • ## &#128203; 算法说明

  • ### 1. XOR 加密
  • - **类型**: 对称加密
  • - **特点**: 简单快速,同一密钥既可加密也可解密
  • - **安全性**: 低(易被破解,适合简单场景)
  • - **用法**: `密文 = 明文 XOR 密钥`

  • ### 2. Base64
  • - **类型**: 编码(非加密)
  • - **特点**: 将二进制数据转换为文本
  • - **用途**: 数据传输、URL参数

  • ### 3. RC4
  • - **类型**: 流加密
  • - **特点**: 速度快,适合大量数据
  • - **安全性**: 中等

  • ### 4. AES
  • - **类型**: 对称加密(Python版)
  • - **特点**: 现代标准加密算法
  • - **安全性**: 高

  • ### 5. RSA
  • - **类型**: 非对称加密(Python版)
  • - **特点**: 公钥加密,私钥解密
  • - **安全性**: 高
  • - **用途**: 密钥交换、数字签名

  • ### 6. MD5 / SHA256
  • - **类型**: 哈希函数
  • - **特点**: 单向,无法解密
  • - **用途**: 密码存储、文件校验

  • ---

  • ## 使用示例

  • ### 场景1: 加密配置文件

  • ```bash
  • # 加密
  • CryptoTool.exe -ef config.ini config.enc mysecretkey

  • # 解密
  • CryptoTool.exe -df config.enc config.ini mysecretkey
  • ```

  • ### 场景2: 加密通信消息

  • ```python
  • from crypto_tool import CryptoTool

  • crypto = CryptoTool()

  • # 加密
  • message = &quot;Hello, this is secret!&quot;
  • key = &quot;mykey123&quot;
  • encrypted = crypto.xor_encrypt(message, key)
  • encoded = crypto.base64_encode(encrypted)
  • print(f&quot;发送: {encoded}&quot;)

  • # 解密
  • received = &quot;...&quot;  # 收到的密文
  • decoded = crypto.base64_decode(received)
  • decrypted = crypto.xor_encrypt(decoded, key)  # XOR对称
  • print(f&quot;收到: {decrypted.decode()}&quot;)
  • ```

  • ### 场景3: 密码哈希存储

  • ```python
  • from crypto_tool import CryptoTool

  • crypto = CryptoTool()

  • # 存储密码时
  • password = &quot;user_password&quot;
  • hashed = crypto.sha256(password)
  • # 存储 hashed 到数据库

  • # 验证密码时
  • input_password = &quot;user_input&quot;
  • if crypto.sha256(input_password) == stored_hash:
  •     print(&quot;密码正确!&quot;)
  • ```

  • ### 场景4: RSA 安全通信

  • ```python
  • from crypto_tool import CryptoTool

  • crypto = CryptoTool()

  • # 生成密钥对
  • private_key, public_key = crypto.rsa_generate_keys()

  • # 发送方用公钥加密
  • message = &quot;Secret message&quot;
  • encrypted = crypto.rsa_encrypt(message, public_key)

  • # 接收方用私钥解密
  • decrypted = crypto.rsa_decrypt(encrypted, private_key)
  • print(decrypted)  # Secret message
  • ```

  • ---

  • ##  安全提示

  • 1. **密钥管理**: 永远不要将密钥硬编码在代码中
  • 2. **XOR加密**: 仅用于简单场景,不适合保护敏感数据
  • 3. **MD5**: 已不安全,建议使用 SHA256
  • 4. **密钥长度**: 密钥越长越安全,建议至少16位
  • 5. **文件加密**: 加密前请备份原始文件

  • ---

  • ## &#128279; 集成到项目

  • ### C++ 集成

  • ```cpp
  • #include &quot;CryptoTool.cpp&quot;  // 或直接复制需要的类

  • // XOR 加密
  • std::string encrypted = XorCrypto::Encrypt(&quot;Hello&quot;, &quot;key&quot;);

  • // Base64 编码
  • std::string encoded = Base64::Encode(&quot;Hello&quot;);
  • ```

  • ### Python 集成

  • ```python
  • from crypto_tool import CryptoTool

  • crypto = CryptoTool()

  • # 使用各种加密功能
  • encrypted = crypto.aes_encrypt(&quot;Hello&quot;, key)
  • ```

  • ---

  • ##  常见问题

  • **Q: 加密后的文件无法解密?**  
  • A: 请确保使用相同的密钥和算法

  • **Q: 中文加密出现乱码?**  
  • A: Python版本支持中文,C++版本建议使用UTF-8编码

  • **Q: 如何生成强密钥?**  
  • A: 使用 `python crypto_tool.py -aes-gen` 生成随机密钥

  • ---

  • ##  文件清单

  • - `CryptoTool.cpp` - C++ 加密工具源码
  • - `crypto_tool.py` - Python 加密工具源码
  • - `加密工具使用说明.md` - 本说明文档

  • /**
  • *\file                CryptoTool.cpp
  • *\version        1.0
  • *\author        CryptoTool
  • *\date                2024
  • *\brief        加密解密工具程序
  • *
  • * 支持: XOR加密、Base64编解码、RC4流加密、文件加密
  • *
  • */

  • #include <cstdio>
  • #include <cstdlib>
  • #include <cstring>
  • #include <string>
  • #include <vector>
  • #include <fstream>
  • #include <iostream>
  • #include <iomanip>
  • #include <sstream>

  • // =============================================================================
  • // 工具函数
  • // =============================================================================

  • // 将字节数组转换为十六进制字符串
  • std::string BytesToHex(const unsigned char* data, size_t len) {
  •     std::stringstream ss;
  •     ss << std::hex << std::setfill('0');
  •     for (size_t i = 0; i < len; i++) {
  •         ss << std::setw(2) << (int)data;
  •     }
  •     return ss.str();
  • }

  • // 将十六进制字符串转换为字节数组
  • std::vector<unsigned char> HexToBytes(const std::string& hex) {
  •     std::vector<unsigned char> bytes;
  •     for (size_t i = 0; i < hex.length(); i += 2) {
  •         std::string byteString = hex.substr(i, 2);
  •         unsigned char byte = (unsigned char)strtol(byteString.c_str(), NULL, 16);
  •         bytes.push_back(byte);
  •     }
  •     return bytes;
  • }

  • // =============================================================================
  • // 1. XOR 加密/解密 (对称)
  • // =============================================================================

  • class XorCrypto {
  • public:
  •     // XOR加密/解密 (密钥相同)
  •     static std::string Encrypt(const std::string& plaintext, const std::string& key) {
  •         if (key.empty()) return plaintext;
  •         
  •         std::string result = plaintext;
  •         size_t keyLen = key.length();
  •         
  •         for (size_t i = 0; i < result.length(); i++) {
  •             result ^= key[i % keyLen];
  •         }
  •         return result;
  •     }
  •    
  •     // XOR解密 = 加密 (对称算法)
  •     static std::string Decrypt(const std::string& ciphertext, const std::string& key) {
  •         return Encrypt(ciphertext, key); // XOR是对称的
  •     }
  •    
  •     // 文件加密
  •     static bool EncryptFile(const std::string& inputFile, const std::string& outputFile,
  •                             const std::string& key) {
  •         std::ifstream in(inputFile, std::ios::binary);
  •         std:fstream out(outputFile, std::ios::binary);
  •         
  •         if (!in || !out) return false;
  •         
  •         char ch;
  •         size_t keyIndex = 0;
  •         size_t keyLen = key.length();
  •         
  •         while (in.get(ch)) {
  •             ch ^= key[keyIndex % keyLen];
  •             out.put(ch);
  •             keyIndex++;
  •         }
  •         
  •         return true;
  •     }
  • };

  • // =============================================================================
  • // 2. Base64 编解码
  • // =============================================================================

  • class Base64 {
  • private:
  •     static const char* base64Chars;
  •    
  • public:
  •     // Base64编码
  •     static std::string Encode(const std::string& input) {
  •         std::string encoded;
  •         int i = 0;
  •         unsigned char charArray3[3], charArray4[4];
  •         
  •         for (size_t in_len = input.length(), pos = 0; pos < in_len; ) {
  •             charArray3[i++] = input[pos++];
  •             if (i == 3) {
  •                 charArray4[0] = (charArray3[0] & 0xfc) >> 2;
  •                 charArray4[1] = ((charArray3[0] & 0x03) << 4) + ((charArray3[1] & 0xf0) >> 4);
  •                 charArray4[2] = ((charArray3[1] & 0x0f) << 2) + ((charArray3[2] & 0xc0) >> 6);
  •                 charArray4[3] = charArray3[2] & 0x3f;
  •                
  •                 for (i = 0; i < 4; i++)
  •                     encoded += base64Chars[charArray4];
  •                 i = 0;
  •             }
  •         }
  •         
  •         if (i) {
  •             for (int j = i; j < 3; j++)
  •                 charArray3[j] = '\0';
  •             
  •             charArray4[0] = (charArray3[0] & 0xfc) >> 2;
  •             charArray4[1] = ((charArray3[0] & 0x03) << 4) + ((charArray3[1] & 0xf0) >> 4);
  •             charArray4[2] = ((charArray3[1] & 0x0f) << 2) + ((charArray3[2] & 0xc0) >> 6);
  •             
  •             for (int j = 0; j < (i + 1); j++)
  •                 encoded += base64Chars[charArray4[j]];
  •             
  •             while ((i++ < 3))
  •                 encoded += '=';
  •         }
  •         
  •         return encoded;
  •     }
  •    
  •     // Base64解码
  •     static std::string Decode(const std::string& encoded) {
  •         std::string decoded;
  •         int in_len = encoded.length();
  •         int i = 0, j = 0, in_ = 0;
  •         unsigned char charArray4[4], charArray3[3];
  •         
  •         while (in_len-- && (encoded[in_] != '=') &&
  •                (isalnum(encoded[in_]) || encoded[in_] == '+' || encoded[in_] == '/')) {
  •             
  •             charArray4[i++] = encoded[in_]; in_++;
  •             if (i == 4) {
  •                 for (i = 0; i < 4; i++)
  •                     charArray4 = (unsigned char)strchr(base64Chars, charArray4) - base64Chars;
  •                
  •                 charArray3[0] = (charArray4[0] << 2) + ((charArray4[1] & 0x30) >> 4);
  •                 charArray3[1] = ((charArray4[1] & 0xf) << 4) + ((charArray4[2] & 0x3c) >> 2);
  •                 charArray3[2] = ((charArray4[2] & 0x3) << 6) + charArray4[3];
  •                
  •                 for (i = 0; i < 3; i++)
  •                     decoded += charArray3;
  •                 i = 0;
  •             }
  •         }
  •         
  •         if (i) {
  •             for (j = 0; j < i; j++)
  •                 charArray4[j] = (unsigned char)strchr(base64Chars, charArray4[j]) - base64Chars;
  •             
  •             charArray3[0] = (charArray4[0] << 2) + ((charArray4[1] & 0x30) >> 4);
  •             charArray3[1] = ((charArray4[1] & 0xf) << 4) + ((charArray4[2] & 0x3c) >> 2);
  •             
  •             for (j = 0; j < (i - 1); j++)
  •                 decoded += charArray3[j];
  •         }
  •         
  •         return decoded;
  •     }
  • };

  • const char* Base64::base64Chars =
  •     &quot;ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/&quot;;

  • // =============================================================================
  • // 3. RC4 流加密
  • // =============================================================================

  • class RC4 {
  • private:
  •     unsigned char S[256];
  •     int i, j;
  •    
  •     void swap(unsigned char& a, unsigned char& b) {
  •         unsigned char temp = a;
  •         a = b;
  •         b = temp;
  •     }
  •    
  • public:
  •     // 初始化密钥调度算法 (KSA)
  •     void SetKey(const std::string& key) {
  •         for (int k = 0; k < 256; k++) {
  •             S[k] = k;
  •         }
  •         
  •         int j = 0;
  •         int keyLen = key.length();
  •         
  •         for (int k = 0; k < 256; k++) {
  •             j = (j + S[k] + (unsigned char)key[k % keyLen]) % 256;
  •             swap(S[k], S[j]);
  •         }
  •         
  •         i = 0;
  •         j = 0;
  •     }
  •    
  •     // 生成伪随机字节流 (PRGA)
  •     unsigned char GenerateByte() {
  •         i = (i + 1) % 256;
  •         j = (j + S) % 256;
  •         swap(S, S[j]);
  •         return S[(S + S[j]) % 256];
  •     }
  •    
  •     // 加密/解密数据
  •     std::string Crypt(const std::string& data, const std::string& key) {
  •         SetKey(key);
  •         std::string result = data;
  •         for (size_t k = 0; k < result.length(); k++) {
  •             result[k] ^= GenerateByte();
  •         }
  •         return result;
  •     }
  •    
  •     // RC4加密
  •     static std::string Encrypt(const std::string& plaintext, const std::string& key) {
  •         RC4 rc4;
  •         return rc4.Crypt(plaintext, key);
  •     }
  •    
  •     // RC4解密 (与加密相同)
  •     static std::string Decrypt(const std::string& ciphertext, const std::string& key) {
  •         RC4 rc4;
  •         return rc4.Crypt(ciphertext, key);
  •     }
  • };

  • // =============================================================================
  • // 4. Caesar 凯撒密码 (简单移位)
  • // =============================================================================

  • class Caesar {
  • public:
  •     // 加密: 每个字符向后移动shift位
  •     static std::string Encrypt(const std::string& plaintext, int shift) {
  •         std::string result = plaintext;
  •         for (size_t i = 0; i < result.length(); i++) {
  •             if (isalpha(result)) {
  •                 char base = isupper(result) ? 'A' : 'a';
  •                 result = (result - base + shift) % 26 + base;
  •             }
  •         }
  •         return result;
  •     }
  •    
  •     // 解密: 向前移动shift位
  •     static std::string Decrypt(const std::string& ciphertext, int shift) {
  •         return Encrypt(ciphertext, 26 - (shift % 26));
  •     }
  • };

  • // =============================================================================
  • // 5. MD5 哈希 (简化版)
  • // =============================================================================

  • class MD5 {
  • private:
  •     typedef unsigned int uint32;
  •    
  •     uint32 state[4];
  •     uint32 count[2];
  •     unsigned char buffer[64];
  •    
  •     void init() {
  •         state[0] = 0x67452301;
  •         state[1] = 0xEFCDAB89;
  •         state[2] = 0x98BADCFE;
  •         state[3] = 0x10325476;
  •         count[0] = count[1] = 0;
  •     }
  •    
  •     static uint32 F(uint32 x, uint32 y, uint32 z) { return (x & y) | (~x & z); }
  •     static uint32 G(uint32 x, uint32 y, uint32 z) { return (x & z) | (y & ~z); }
  •     static uint32 H(uint32 x, uint32 y, uint32 z) { return x ^ y ^ z; }
  •     static uint32 I(uint32 x, uint32 y, uint32 z) { return y ^ (x | ~z); }
  •    
  •     static uint32 rotateLeft(uint32 x, int n) { return (x << n) | (x >> (32 - n)); }
  •    
  •     void transform(const unsigned char block[64]) {
  •         uint32 a = state[0], b = state[1], c = state[2], d = state[3];
  •         uint32 x[16];
  •         
  •         for (int i = 0; i < 16; i++)
  •             x = ((uint32)block[i * 4]) | (((uint32)block[i * 4 + 1]) << 8) |
  •                    (((uint32)block[i * 4 + 2]) << 16) | (((uint32)block[i * 4 + 3]) << 24);
  •         
  •         // Round 1
  •         FF(a, b, c, d, x[0], 7, 0xD76AA478);
  •         FF(d, a, b, c, x[1], 12, 0xE8C7B756);
  •         FF(c, d, a, b, x[2], 17, 0x242070DB);
  •         FF(b, c, d, a, x[3], 22, 0xC1BDCEEE);
  •         FF(a, b, c, d, x[4], 7, 0xF57C0FAF);
  •         FF(d, a, b, c, x[5], 12, 0x4787C62A);
  •         FF(c, d, a, b, x[6], 17, 0xA8304613);
  •         FF(b, c, d, a, x[7], 22, 0xFD469501);
  •         FF(a, b, c, d, x[8], 7, 0x698098D8);
  •         FF(d, a, b, c, x[9], 12, 0x8B44F7AF);
  •         FF(c, d, a, b, x[10], 17, 0xFFFF5BB1);
  •         FF(b, c, d, a, x[11], 22, 0x895CD7BE);
  •         FF(a, b, c, d, x[12], 7, 0x6B901122);
  •         FF(d, a, b, c, x[13], 12, 0xFD987193);
  •         FF(c, d, a, b, x[14], 17, 0xA679438E);
  •         FF(b, c, d, a, x[15], 22, 0x49B40821);
  •         
  •         // Round 2-4 省略... (简化版)
  •         
  •         state[0] += a;
  •         state[1] += b;
  •         state[2] += c;
  •         state[3] += d;
  •     }
  •    
  •     void FF(uint32& a, uint32 b, uint32 c, uint32 d, uint32 x, int s, uint32 ac) {
  •         a = rotateLeft(a + F(b, c, d) + x + ac, s) + b;
  •     }
  •    
  • public:
  •     MD5() { init(); }
  •    
  •     // 简化版MD5哈希 (实际使用请用完整实现)
  •     static std::string Hash(const std::string& input) {
  •         // 这里使用简化版本,实际应使用完整MD5算法
  •         // 或者调用OpenSSL等库
  •         unsigned long hash = 5381;
  •         for (size_t i = 0; i < input.length(); i++) {
  •             hash = ((hash << 5) + hash) + input;
  •         }
  •         
  •         char result[17];
  •         sprintf(result, &quot;%016lX&quot;, hash);
  •         return std::string(result);
  •     }
  • };

  • // =============================================================================
  • // 用户界面
  • // =============================================================================

  • void PrintBanner() {
  •     printf(&quot;\n&quot;);
  •     printf(&quot;╔══════════════════════════════════════════════════════════════╗\n&quot;);
  •     printf(&quot;║                                                              ║\n&quot;);
  •     printf(&quot;║              &#128272; CryptoTool 加密解密工具 v1.0 &#128272;              ║\n&quot;);
  •     printf(&quot;║                                                              ║\n&quot;);
  •     printf(&quot;║  支持: XOR | Base64 | RC4 | Caesar | MD5                     ║\n&quot;);
  •     printf(&quot;║                                                              ║\n&quot;);
  •     printf(&quot;╚══════════════════════════════════════════════════════════════╝\n&quot;);
  •     printf(&quot;\n&quot;);
  • }

  • void PrintMenu() {
  •     printf(&quot;\n&quot;);
  •     printf(&quot;┌─────────────────────────────────────────────────────────────┐\n&quot;);
  •     printf(&quot;│  [1] XOR加密        [2] XOR解密        [3] Base64编码       │\n&quot;);
  •     printf(&quot;│  [4] Base64解码     [5] RC4加密        [6] RC4解密          │\n&quot;);
  •     printf(&quot;│  [7] Caesar加密     [8] Caesar解密     [9] MD5哈希          │\n&quot;);
  •     printf(&quot;│  [10] 文件加密      [11] 文件解密      [0] 退出             │\n&quot;);
  •     printf(&quot;└─────────────────────────────────────────────────────────────┘\n&quot;);
  •     printf(&quot;\n请选择操作: &quot;);
  • }

  • void StringEncryptMenu() {
  •     printf(&quot;\n=== 字符串加密 ===\n&quot;);
  •     printf(&quot;1. XOR加密\n&quot;);
  •     printf(&quot;2. Base64编码\n&quot;);
  •     printf(&quot;3. RC4加密\n&quot;);
  •     printf(&quot;4. Caesar加密\n&quot;);
  •     printf(&quot;5. MD5哈希\n&quot;);
  •     printf(&quot;\n请选择: &quot;);
  • }

  • // 获取用户输入(支持空格)
  • std::string GetInput(const char* prompt) {
  •     printf(&quot;%s&quot;, prompt);
  •     std::string input;
  •     std::getline(std::cin, input);
  •     return input;
  • }

  • // 主程序
  • int main(int argc, char* argv[]) {
  •     PrintBanner();
  •    
  •     // 命令行模式
  •     if (argc > 1) {
  •         std::string mode = argv[1];
  •         
  •         if (mode == &quot;-h&quot; || mode == &quot;--help&quot;) {
  •             printf(&quot;用法: CryptoTool [选项] [参数]\n\n&quot;);
  •             printf(&quot;选项:\n&quot;);
  •             printf(&quot;  -e xor <文本> <密钥>     XOR加密\n&quot;);
  •             printf(&quot;  -d xor <密文> <密钥>     XOR解密\n&quot;);
  •             printf(&quot;  -e64 <文本>              Base64编码\n&quot;);
  •             printf(&quot;  -d64 <密文>              Base64解码\n&quot;);
  •             printf(&quot;  -e rc4 <文本> <密钥>     RC4加密\n&quot;);
  •             printf(&quot;  -d rc4 <密文> <密钥>     RC4解密\n&quot;);
  •             printf(&quot;  -e caesar <文本> <位移>  Caesar加密\n&quot;);
  •             printf(&quot;  -md5 <文本>              MD5哈希\n&quot;);
  •             printf(&quot;  -ef <输入文件> <输出文件> <密钥>  文件加密\n&quot;);
  •             printf(&quot;  -df <输入文件> <输出文件> <密钥>  文件解密\n&quot;);
  •             printf(&quot;\n&quot;);
  •             return 0;
  •         }
  •         
  •         if (mode == &quot;-e&quot; && argc >= 4) {
  •             std::string algo = argv[2];
  •             std::string text = argv[3];
  •             std::string key = (argc >= 5) ? argv[4] : &quot;&quot;;
  •             
  •             if (algo == &quot;xor&quot;) {
  •                 std::string result = XorCrypto::Encrypt(text, key);
  •                 printf(&quot;XOR加密结果 (Hex): %s\n&quot;, BytesToHex((unsigned char*)result.c_str(), result.length()).c_str());
  •             }
  •             else if (algo == &quot;rc4&quot;) {
  •                 std::string result = RC4::Encrypt(text, key);
  •                 printf(&quot;RC4加密结果 (Hex): %s\n&quot;, BytesToHex((unsigned char*)result.c_str(), result.length()).c_str());
  •             }
  •             else if (algo == &quot;caesar&quot; && argc >= 5) {
  •                 int shift = atoi(argv[4]);
  •                 std::string result = Caesar::Encrypt(text, shift);
  •                 printf(&quot;Caesar加密结果: %s\n&quot;, result.c_str());
  •             }
  •             return 0;
  •         }
  •         
  •         if (mode == &quot;-d&quot; && argc >= 4) {
  •             std::string algo = argv[2];
  •             std::string text = argv[3];
  •             std::string key = (argc >= 5) ? argv[4] : &quot;&quot;;
  •             
  •             if (algo == &quot;xor&quot;) {
  •                 std::string result = XorCrypto:ecrypt(text, key);
  •                 printf(&quot;XOR解密结果: %s\n&quot;, result.c_str());
  •             }
  •             else if (algo == &quot;rc4&quot;) {
  •                 std::string result = RC4:ecrypt(text, key);
  •                 printf(&quot;RC4解密结果: %s\n&quot;, result.c_str());
  •             }
  •             return 0;
  •         }
  •         
  •         if (mode == &quot;-e64&quot; && argc >= 3) {
  •             std::string text = argv[2];
  •             std::string result = Base64::Encode(text);
  •             printf(&quot;Base64编码结果: %s\n&quot;, result.c_str());
  •             return 0;
  •         }
  •         
  •         if (mode == &quot;-d64&quot; && argc >= 3) {
  •             std::string text = argv[2];
  •             std::string result = Base64:ecode(text);
  •             printf(&quot;Base64解码结果: %s\n&quot;, result.c_str());
  •             return 0;
  •         }
  •         
  •         if (mode == &quot;-md5&quot; && argc >= 3) {
  •             std::string text = argv[2];
  •             std::string result = MD5::Hash(text);
  •             printf(&quot;MD5哈希结果: %s\n&quot;, result.c_str());
  •             return 0;
  •         }
  •         
  •         if (mode == &quot;-ef&quot; && argc >= 5) {
  •             std::string inputFile = argv[2];
  •             std::string outputFile = argv[3];
  •             std::string key = argv[4];
  •             
  •             if (XorCrypto::EncryptFile(inputFile, outputFile, key)) {
  •                 printf(&quot;文件加密成功: %s -> %s\n&quot;, inputFile.c_str(), outputFile.c_str());
  •             } else {
  •                 printf(&quot;文件加密失败!\n&quot;);
  •             }
  •             return 0;
  •         }
  •         
  •         if (mode == &quot;-df&quot; && argc >= 5) {
  •             std::string inputFile = argv[2];
  •             std::string outputFile = argv[3];
  •             std::string key = argv[4];
  •             
  •             if (XorCrypto::EncryptFile(inputFile, outputFile, key)) {
  •                 printf(&quot;文件解密成功: %s -> %s\n&quot;, inputFile.c_str(), outputFile.c_str());
  •             } else {
  •                 printf(&quot;文件解密失败!\n&quot;);
  •             }
  •             return 0;
  •         }
  •     }
  •    
  •     // 交互模式
  •     int choice;
  •     std::string text, key, result;
  •     int shift;
  •    
  •     do {
  •         PrintMenu();
  •         scanf(&quot;%d&quot;, &choice);
  •         getchar(); // 清除换行符
  •         
  •         switch (choice) {
  •             case 1: // XOR加密
  •                 text = GetInput(&quot;请输入要加密的文本: &quot;);
  •                 key = GetInput(&quot;请输入密钥: &quot;);
  •                 result = XorCrypto::Encrypt(text, key);
  •                 printf(&quot;\n&#10003; XOR加密结果 (Hex):\n%s\n&quot;, BytesToHex((unsigned char*)result.c_str(), result.length()).c_str());
  •                 printf(&quot;\n&#10003; XOR加密结果 (Base64):\n%s\n&quot;, Base64::Encode(result).c_str());
  •                 break;
  •                
  •             case 2: // XOR解密
  •                 printf(&quot;输入格式: 1=Hex 2=Base64 3=原始文本\n&quot;);
  •                 int fmt;
  •                 scanf(&quot;%d&quot;, &fmt);
  •                 getchar();
  •                 text = GetInput(&quot;请输入密文: &quot;);
  •                 key = GetInput(&quot;请输入密钥: &quot;);
  •                
  •                 if (fmt == 1) {
  •                     std::vector<unsigned char> bytes = HexToBytes(text);
  •                     text = std::string((char*)bytes.data(), bytes.size());
  •                 } else if (fmt == 2) {
  •                     text = Base64:ecode(text);
  •                 }
  •                
  •                 result = XorCrypto:ecrypt(text, key);
  •                 printf(&quot;\n&#10003; XOR解密结果:\n%s\n&quot;, result.c_str());
  •                 break;
  •                
  •             case 3: // Base64编码
  •                 text = GetInput(&quot;请输入要编码的文本: &quot;);
  •                 result = Base64::Encode(text);
  •                 printf(&quot;\n&#10003; Base64编码结果:\n%s\n&quot;, result.c_str());
  •                 break;
  •                
  •             case 4: // Base64解码
  •                 text = GetInput(&quot;请输入Base64密文: &quot;);
  •                 result = Base64:ecode(text);
  •                 printf(&quot;\n&#10003; Base64解码结果:\n%s\n&quot;, result.c_str());
  •                 break;
  •                
  •             case 5: // RC4加密
  •                 text = GetInput(&quot;请输入要加密的文本: &quot;);
  •                 key = GetInput(&quot;请输入密钥: &quot;);
  •                 result = RC4::Encrypt(text, key);
  •                 printf(&quot;\n&#10003; RC4加密结果 (Hex):\n%s\n&quot;, BytesToHex((unsigned char*)result.c_str(), result.length()).c_str());
  •                 printf(&quot;\n&#10003; RC4加密结果 (Base64):\n%s\n&quot;, Base64::Encode(result).c_str());
  •                 break;
  •                
  •             case 6: // RC4解密
  •                 printf(&quot;输入格式: 1=Hex 2=Base64\n&quot;);
  •                 scanf(&quot;%d&quot;, &fmt);
  •                 getchar();
  •                 text = GetInput(&quot;请输入密文: &quot;);
  •                 key = GetInput(&quot;请输入密钥: &quot;);
  •                
  •                 if (fmt == 1) {
  •                     std::vector<unsigned char> bytes = HexToBytes(text);
  •                     text = std::string((char*)bytes.data(), bytes.size());
  •                 } else if (fmt == 2) {
  •                     text = Base64:ecode(text);
  •                 }
  •                
  •                 result = RC4:ecrypt(text, key);
  •                 printf(&quot;\n&#10003; RC4解密结果:\n%s\n&quot;, result.c_str());
  •                 break;
  •                
  •             case 7: // Caesar加密
  •                 text = GetInput(&quot;请输入要加密的文本: &quot;);
  •                 printf(&quot;请输入位移量 (1-25): &quot;);
  •                 scanf(&quot;%d&quot;, &shift);
  •                 getchar();
  •                 result = Caesar::Encrypt(text, shift);
  •                 printf(&quot;\n&#10003; Caesar加密结果:\n%s\n&quot;, result.c_str());
  •                 break;
  •                
  •             case 8: // Caesar解密
  •                 text = GetInput(&quot;请输入密文: &quot;);
  •                 printf(&quot;请输入位移量 (1-25): &quot;);
  •                 scanf(&quot;%d&quot;, &shift);
  •                 getchar();
  •                 result = Caesar:ecrypt(text, shift);
  •                 printf(&quot;\n&#10003; Caesar解密结果:\n%s\n&quot;, result.c_str());
  •                 break;
  •                
  •             case 9: // MD5哈希
  •                 text = GetInput(&quot;请输入要哈希的文本: &quot;);
  •                 result = MD5::Hash(text);
  •                 printf(&quot;\n&#10003; MD5哈希结果:\n%s\n&quot;, result.c_str());
  •                 break;
  •                
  •             case 10: { // 文件加密
  •                 std::string inputFile = GetInput(&quot;请输入输入文件路径: &quot;);
  •                 std::string outputFile = GetInput(&quot;请输入输出文件路径: &quot;);
  •                 key = GetInput(&quot;请输入密钥: &quot;);
  •                
  •                 if (XorCrypto::EncryptFile(inputFile, outputFile, key)) {
  •                     printf(&quot;\n&#10003; 文件加密成功!\n&quot;);
  •                 } else {
  •                     printf(&quot;\n&#10007; 文件加密失败!\n&quot;);
  •                 }
  •                 break;
  •             }
  •             
  •             case 11: { // 文件解密
  •                 std::string inputFile = GetInput(&quot;请输入输入文件路径: &quot;);
  •                 std::string outputFile = GetInput(&quot;请输入输出文件路径: &quot;);
  •                 key = GetInput(&quot;请输入密钥: &quot;);
  •                
  •                 if (XorCrypto::EncryptFile(inputFile, outputFile, key)) {
  •                     printf(&quot;\n&#10003; 文件解密成功!\n&quot;);
  •                 } else {
  •                     printf(&quot;\n&#10007; 文件解密失败!\n&quot;);
  •                 }
  •                 break;
  •             }
  •             
  •             case 0:
  •                 printf(&quot;\n感谢使用 CryptoTool,再见!\n\n&quot;);
  •                 break;
  •                
  •             default:
  •                 printf(&quot;\n无效的选择,请重试!\n&quot;);
  •         }
  •         
  •         if (choice != 0) {
  •             printf(&quot;\n按回车键继续...&quot;);
  •             getchar();
  •         }
  •         
  •     } while (choice != 0);
  •    
  •     return 0;
  • }

  • // =============================================================================
  • // 编译说明
  • // =============================================================================

  • /*
  • * Windows (MinGW):
  • *   g++ -o CryptoTool.exe CryptoTool.cpp
  • *
  • * Windows (MSVC):
  • *   cl /EHsc /FeCryptoTool.exe CryptoTool.cpp
  • *
  • * Linux/Mac:
  • *   g++ -o CryptoTool CryptoTool.cpp
  • *
  • * 使用示例:
  • *   CryptoTool -e xor &quot;Hello World&quot; &quot;mykey&quot;
  • *   CryptoTool -e64 &quot;Hello World&quot;
  • *   CryptoTool -md5 &quot;Hello World&quot;
  • *   CryptoTool -ef input.txt output.enc mykey
  • */



免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Archiver|小黑屋|白黑论坛 |网站地图

GMT+8, 2026-8-5 01:16 , Processed in 0.023040 second(s), 25 queries , Gzip On.

Powered by Discuz! X3.5

© 2001-2025 Discuz! Team.

快速回复 返回顶部 返回列表