- 精华
- 阅读权限
- 90
- 贡献
- 人
- 好友
- 相册
- 分享
- 听众
- 收听
- 注册时间
- 2010-1-8
- 在线时间
- 小时
- 最后登录
- 1970-1-1
|
今天没有事情做 做了个文件加密工具 有一些人辛辛苦苦改出来的东西一下子被别人复制
- /**
- *\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[i];
- }
- 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[i] ^= 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::ofstream 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]];
- 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[i] = (unsigned char)strchr(base64Chars, charArray4[i]) - 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];
- 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 =
- "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
- // =============================================================================
- // 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[i]) % 256;
- swap(S[i], S[j]);
- return S[(S[i] + 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[i])) {
- char base = isupper(result[i]) ? 'A' : 'a';
- result[i] = (result[i] - 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[i] = ((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[i];
- }
-
- char result[17];
- sprintf(result, "%016lX", hash);
- return std::string(result);
- }
- };
- // =============================================================================
- // 用户界面
- // =============================================================================
- void PrintBanner() {
- printf("\n");
- printf("╔══════════════════════════════════════════════════════════════╗\n");
- printf("║ ║\n");
- printf("║ 🔐 CryptoTool 加密解密工具 v1.0 🔐 ║\n");
- printf("║ ║\n");
- printf("║ 支持: XOR | Base64 | RC4 | Caesar | MD5 ║\n");
- printf("║ ║\n");
- printf("╚══════════════════════════════════════════════════════════════╝\n");
- printf("\n");
- }
- void PrintMenu() {
- printf("\n");
- printf("┌─────────────────────────────────────────────────────────────┐\n");
- printf("│ [1] XOR加密 [2] XOR解密 [3] Base64编码 │\n");
- printf("│ [4] Base64解码 [5] RC4加密 [6] RC4解密 │\n");
- printf("│ [7] Caesar加密 [8] Caesar解密 [9] MD5哈希 │\n");
- printf("│ [10] 文件加密 [11] 文件解密 [0] 退出 │\n");
- printf("└─────────────────────────────────────────────────────────────┘\n");
- printf("\n请选择操作: ");
- }
- void StringEncryptMenu() {
- printf("\n=== 字符串加密 ===\n");
- printf("1. XOR加密\n");
- printf("2. Base64编码\n");
- printf("3. RC4加密\n");
- printf("4. Caesar加密\n");
- printf("5. MD5哈希\n");
- printf("\n请选择: ");
- }
- // 获取用户输入(支持空格)
- std::string GetInput(const char* prompt) {
- printf("%s", 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 == "-h" || mode == "--help") {
- printf("用法: CryptoTool [选项] [参数]\n\n");
- printf("选项:\n");
- printf(" -e xor <文本> <密钥> XOR加密\n");
- printf(" -d xor <密文> <密钥> XOR解密\n");
- printf(" -e64 <文本> Base64编码\n");
- printf(" -d64 <密文> Base64解码\n");
- printf(" -e rc4 <文本> <密钥> RC4加密\n");
- printf(" -d rc4 <密文> <密钥> RC4解密\n");
- printf(" -e caesar <文本> <位移> Caesar加密\n");
- printf(" -md5 <文本> MD5哈希\n");
- printf(" -ef <输入文件> <输出文件> <密钥> 文件加密\n");
- printf(" -df <输入文件> <输出文件> <密钥> 文件解密\n");
- printf("\n");
- return 0;
- }
-
- if (mode == "-e" && argc >= 4) {
- std::string algo = argv[2];
- std::string text = argv[3];
- std::string key = (argc >= 5) ? argv[4] : "";
-
- if (algo == "xor") {
- std::string result = XorCrypto::Encrypt(text, key);
- printf("XOR加密结果 (Hex): %s\n", BytesToHex((unsigned char*)result.c_str(), result.length()).c_str());
- }
- else if (algo == "rc4") {
- std::string result = RC4::Encrypt(text, key);
- printf("RC4加密结果 (Hex): %s\n", BytesToHex((unsigned char*)result.c_str(), result.length()).c_str());
- }
- else if (algo == "caesar" && argc >= 5) {
- int shift = atoi(argv[4]);
- std::string result = Caesar::Encrypt(text, shift);
- printf("Caesar加密结果: %s\n", result.c_str());
- }
- return 0;
- }
-
- if (mode == "-d" && argc >= 4) {
- std::string algo = argv[2];
- std::string text = argv[3];
- std::string key = (argc >= 5) ? argv[4] : "";
-
- if (algo == "xor") {
- std::string result = XorCrypto::Decrypt(text, key);
- printf("XOR解密结果: %s\n", result.c_str());
- }
- else if (algo == "rc4") {
- std::string result = RC4::Decrypt(text, key);
- printf("RC4解密结果: %s\n", result.c_str());
- }
- return 0;
- }
-
- if (mode == "-e64" && argc >= 3) {
- std::string text = argv[2];
- std::string result = Base64::Encode(text);
- printf("Base64编码结果: %s\n", result.c_str());
- return 0;
- }
-
- if (mode == "-d64" && argc >= 3) {
- std::string text = argv[2];
- std::string result = Base64::Decode(text);
- printf("Base64解码结果: %s\n", result.c_str());
- return 0;
- }
-
- if (mode == "-md5" && argc >= 3) {
- std::string text = argv[2];
- std::string result = MD5::Hash(text);
- printf("MD5哈希结果: %s\n", result.c_str());
- return 0;
- }
-
- if (mode == "-ef" && argc >= 5) {
- std::string inputFile = argv[2];
- std::string outputFile = argv[3];
- std::string key = argv[4];
-
- if (XorCrypto::EncryptFile(inputFile, outputFile, key)) {
- printf("文件加密成功: %s -> %s\n", inputFile.c_str(), outputFile.c_str());
- } else {
- printf("文件加密失败!\n");
- }
- return 0;
- }
-
- if (mode == "-df" && argc >= 5) {
- std::string inputFile = argv[2];
- std::string outputFile = argv[3];
- std::string key = argv[4];
-
- if (XorCrypto::EncryptFile(inputFile, outputFile, key)) {
- printf("文件解密成功: %s -> %s\n", inputFile.c_str(), outputFile.c_str());
- } else {
- printf("文件解密失败!\n");
- }
- return 0;
- }
- }
-
- // 交互模式
- int choice;
- std::string text, key, result;
- int shift;
-
- do {
- PrintMenu();
- scanf("%d", &choice);
- getchar(); // 清除换行符
-
- switch (choice) {
- case 1: // XOR加密
- text = GetInput("请输入要加密的文本: ");
- key = GetInput("请输入密钥: ");
- result = XorCrypto::Encrypt(text, key);
- printf("\n✓ XOR加密结果 (Hex):\n%s\n", BytesToHex((unsigned char*)result.c_str(), result.length()).c_str());
- printf("\n✓ XOR加密结果 (Base64):\n%s\n", Base64::Encode(result).c_str());
- break;
-
- case 2: // XOR解密
- printf("输入格式: 1=Hex 2=Base64 3=原始文本\n");
- int fmt;
- scanf("%d", &fmt);
- getchar();
- text = GetInput("请输入密文: ");
- key = GetInput("请输入密钥: ");
-
- if (fmt == 1) {
- std::vector<unsigned char> bytes = HexToBytes(text);
- text = std::string((char*)bytes.data(), bytes.size());
- } else if (fmt == 2) {
- text = Base64::Decode(text);
- }
-
- result = XorCrypto::Decrypt(text, key);
- printf("\n✓ XOR解密结果:\n%s\n", result.c_str());
- break;
-
- case 3: // Base64编码
- text = GetInput("请输入要编码的文本: ");
- result = Base64::Encode(text);
- printf("\n✓ Base64编码结果:\n%s\n", result.c_str());
- break;
-
- case 4: // Base64解码
- text = GetInput("请输入Base64密文: ");
- result = Base64::Decode(text);
- printf("\n✓ Base64解码结果:\n%s\n", result.c_str());
- break;
-
- case 5: // RC4加密
- text = GetInput("请输入要加密的文本: ");
- key = GetInput("请输入密钥: ");
- result = RC4::Encrypt(text, key);
- printf("\n✓ RC4加密结果 (Hex):\n%s\n", BytesToHex((unsigned char*)result.c_str(), result.length()).c_str());
- printf("\n✓ RC4加密结果 (Base64):\n%s\n", Base64::Encode(result).c_str());
- break;
-
- case 6: // RC4解密
- printf("输入格式: 1=Hex 2=Base64\n");
- scanf("%d", &fmt);
- getchar();
- text = GetInput("请输入密文: ");
- key = GetInput("请输入密钥: ");
-
- if (fmt == 1) {
- std::vector<unsigned char> bytes = HexToBytes(text);
- text = std::string((char*)bytes.data(), bytes.size());
- } else if (fmt == 2) {
- text = Base64::Decode(text);
- }
-
- result = RC4::Decrypt(text, key);
- printf("\n✓ RC4解密结果:\n%s\n", result.c_str());
- break;
-
- case 7: // Caesar加密
- text = GetInput("请输入要加密的文本: ");
- printf("请输入位移量 (1-25): ");
- scanf("%d", &shift);
- getchar();
- result = Caesar::Encrypt(text, shift);
- printf("\n✓ Caesar加密结果:\n%s\n", result.c_str());
- break;
-
- case 8: // Caesar解密
- text = GetInput("请输入密文: ");
- printf("请输入位移量 (1-25): ");
- scanf("%d", &shift);
- getchar();
- result = Caesar::Decrypt(text, shift);
- printf("\n✓ Caesar解密结果:\n%s\n", result.c_str());
- break;
-
- case 9: // MD5哈希
- text = GetInput("请输入要哈希的文本: ");
- result = MD5::Hash(text);
- printf("\n✓ MD5哈希结果:\n%s\n", result.c_str());
- break;
-
- case 10: { // 文件加密
- std::string inputFile = GetInput("请输入输入文件路径: ");
- std::string outputFile = GetInput("请输入输出文件路径: ");
- key = GetInput("请输入密钥: ");
-
- if (XorCrypto::EncryptFile(inputFile, outputFile, key)) {
- printf("\n✓ 文件加密成功!\n");
- } else {
- printf("\n✗ 文件加密失败!\n");
- }
- break;
- }
-
- case 11: { // 文件解密
- std::string inputFile = GetInput("请输入输入文件路径: ");
- std::string outputFile = GetInput("请输入输出文件路径: ");
- key = GetInput("请输入密钥: ");
-
- if (XorCrypto::EncryptFile(inputFile, outputFile, key)) {
- printf("\n✓ 文件解密成功!\n");
- } else {
- printf("\n✗ 文件解密失败!\n");
- }
- break;
- }
-
- case 0:
- printf("\n感谢使用 CryptoTool,再见!\n\n");
- break;
-
- default:
- printf("\n无效的选择,请重试!\n");
- }
-
- if (choice != 0) {
- printf("\n按回车键继续...");
- 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 "Hello World" "mykey"
- * CryptoTool -e64 "Hello World"
- * CryptoTool -md5 "Hello World"
- * CryptoTool -ef input.txt output.enc mykey
- */
复制代码
|
本帖子中包含更多资源
您需要 登录 才可以下载或查看,没有账号?注册
x
评分
-
查看全部评分
|