C++将任意文件编码为汉字文本实现简单加密,将汉字文本转码还原原文件实现解密

本文主要是介绍C++将任意文件编码为汉字文本实现简单加密,将汉字文本转码还原原文件实现解密,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

C++将任意文件编码为汉字文本实现简单加密,将汉字文本转码还原原文件实现解密。

#include <QCoreApplication>
#include <iostream>
#include <fstream>
#include <map>
#include <vector>
#include <string>
#include <locale>

// 创建字节到汉字的映射
std::map<uint8_t, std::string> byte_to_hanzi = {
    {0x00, "凡"}, {0x01, "周"}, {0x02, "张"}, {0x03, "古"},
    {0x04, "陈"}, {0x05, "菲"}, {0x06, "慧"}, {0x07, "伟"},
    {0x08, "梅"}, {0x09, "邓"}, {0x0A, "钰"}, {0x0B, "琳"},
    {0x0C, "浩"}, {0x0D, "语"}, {0x0E, "洲"}, {0x0F, "蓓"},
    {0x10, "京"}, {0x11, "垓"}, {0x12, "秭"}, {0x13, "穰"},
    {0x14, "沟"}, {0x15, "涧"}, {0x16, "正"}, {0x17, "载"},
    {0x18, "极"}, {0x19, "恒"}, {0x1A, "酸"}, {0x1B, "僧"},
    {0x1C, "祇"}, {0x1D, "那"}, {0x1E, "由"}, {0x1F, "他"},
    {0x20, "甲"}, {0x21, "乙"}, {0x22, "丙"}, {0x23, "丁"},
    {0x24, "戊"}, {0x25, "己"}, {0x26, "庚"}, {0x27, "辛"},
    {0x28, "壬"}, {0x29, "癸"}, {0x2A, "子"}, {0x2B, "丑"},
    {0x2C, "寅"}, {0x2D, "卯"}, {0x2E, "辰"}, {0x2F, "巳"},
    {0x30, "午"}, {0x31, "未"}, {0x32, "申"}, {0x33, "酉"},
    {0x34, "戌"}, {0x35, "亥"}, {0x36, "东"}, {0x37, "南"},
    {0x38, "西"}, {0x39, "北"}, {0x3A, "中"}, {0x3B, "春"},
    {0x3C, "夏"}, {0x3D, "秋"}, {0x3E, "冬"}, {0x3F, "上"},
    {0x40, "下"}, {0x41, "左"}, {0x42, "右"}, {0x43, "前"},
    {0x44, "后"}, {0x45, "外"}, {0x46, "内"}, {0x47, "大"},
    {0x48, "小"}, {0x49, "高"}, {0x4A, "低"}, {0x4B, "远"},
    {0x4C, "近"}, {0x4D, "长"}, {0x4E, "短"}, {0x4F, "宽"},
    {0x50, "窄"}, {0x51, "强"}, {0x52, "弱"}, {0x53, "黑"},
    {0x54, "白"}, {0x55, "红"}, {0x56, "绿"}, {0x57, "蓝"},
    {0x58, "黄"}, {0x59, "金"}, {0x5A, "木"}, {0x5B, "水"},
    {0x5C, "火"}, {0x5D, "土"}, {0x5E, "风"}, {0x5F, "雷"},
    {0x60, "电"}, {0x61, "雨"}, {0x62, "雪"}, {0x63, "云"},
    {0x64, "雾"}, {0x65, "龙"}, {0x66, "虎"}, {0x67, "鸟"},
    {0x68, "鱼"}, {0x69, "花"}, {0x6A, "草"}, {0x6B, "谭"},
    {0x6C, "虫"}, {0x6D, "马"}, {0x6E, "牛"}, {0x6F, "羊"},
    {0x70, "猪"}, {0x71, "狗"}, {0x72, "猫"}, {0x73, "鼠"},
    {0x74, "兔"}, {0x75, "蛇"}, {0x76, "鹰"}, {0x77, "狼"},
    {0x78, "熊"}, {0x79, "星"}, {0x7A, "月"}, {0x7B, "日"},
    {0x7C, "光"}, {0x7D, "暗"}, {0x7E, "力"}, {0x7F, "气"},
    {0x80, "神"}, {0x81, "魔"}, {0x82, "妖"}, {0x83, "仙"},
    {0x84, "佛"}, {0x85, "人"}, {0x86, "鬼"}, {0x87, "魂"},
    {0x88, "灵"}, {0x89, "梦"}, {0x8A, "爱"}, {0x8B, "恨"},
    {0x8C, "情"}, {0x8D, "欲"}, {0x8E, "喜"}, {0x8F, "怒"},
    {0x90, "哀"}, {0x91, "乐"}, {0x92, "怨"}, {0x93, "惊"},
    {0x94, "恐"}, {0x95, "悲"}, {0x96, "痛"}, {0x97, "伤"},
    {0x98, "疾"}, {0x99, "病"}, {0x9A, "死"}, {0x9B, "生"},
    {0x9C, "老"}, {0x9D, "少"}, {0x9E, "地"}, {0x9F, "天"},
    {0xA0, "山"}, {0xA1, "海"}, {0xA2, "岛"}, {0xA3, "川"},
    {0xA4, "河"}, {0xA5, "湖"}, {0xA6, "江"}, {0xA7, "溪"},
    {0xA8, "泉"}, {0xA9, "沙"}, {0xAA, "漠"}, {0xAB, "湾"},
    {0xAC, "港"}, {0xAD, "村"}, {0xAE, "镇"}, {0xAF, "城"},
    {0xB0, "氟"}, {0xB1, "都"}, {0xB2, "国"}, {0xB3, "府"},
    {0xB4, "皇"}, {0xB5, "王"}, {0xB6, "帝"}, {0xB7, "崇"},
    {0xB8, "妃"}, {0xB9, "公"}, {0xBA, "侯"}, {0xBB, "伯"},
    {0xBC, "睿"}, {0xBD, "男"}, {0xBE, "将"}, {0xBF, "军"},
    {0xC0, "帅"}, {0xC1, "士"}, {0xC2, "卒"}, {0xC3, "兵"},
    {0xC4, "旅"}, {0xC5, "团"}, {0xC6, "连"}, {0xC7, "排"},
    {0xC8, "班"}, {0xC9, "战"}, {0xCA, "斗"}, {0xCB, "攻"},
    {0xCC, "守"}, {0xCD, "敌"}, {0xCE, "友"}, {0xCF, "盟"},
    {0xD0, "社"}, {0xD1, "会"}, {0xD2, "党"}, {0xD3, "团"},
    {0xD4, "队"}, {0xD5, "部"}, {0xD6, "组"}, {0xD7, "队"},
    {0xD8, "队"}, {0xD9, "校"}, {0xDA, "场"}, {0xDB, "店"},
    {0xDC, "院"}, {0xDD, "馆"}, {0xDE, "堂"}, {0xDF, "舍"},
    {0xE0, "房"}, {0xE1, "屋"}, {0xE2, "室"}, {0xE3, "厅"},
    {0xE4, "廊"}, {0xE5, "庙"}, {0xE6, "塔"}, {0xE7, "楼"},
    {0xE8, "台"}, {0xE9, "殿"}, {0xEA, "寺"}, {0xEB, "宫"},
    {0xEC, "阁"}, {0xED, "堂"}, {0xEE, "院"}, {0xEF, "盐"},
    {0xF0, "氧"}, {0xF1, "户"}, {0xF2, "门"}, {0xF3, "窗"},
    {0xF4, "墙"}, {0xF5, "柱"}, {0xF6, "梁"}, {0xF7, "顶"},
    {0xF8, "基"}, {0xF9, "础"}, {0xFA, "路"}, {0xFB, "桥"},
    {0xFC, "街"}, {0xFD, "巷"}, {0xFE, "预"}, {0xFF, "满"}
};
// 创建汉字到字节的映射
std::map<std::string, uint8_t> hanzi_to_byte;


// 编码函数:二进制文件 -> 汉字文件
void encode_to_hanzi(const std::string& input_file, const std::string& output_file) {
    std::ifstream ifs(input_file, std::ios::binary);
    std::ofstream ofs(output_file, std::ios::binary);
    uint8_t byte;
    while (ifs.read(reinterpret_cast<char*>(&byte), sizeof(byte))) {
        ofs << byte_to_hanzi[byte];
    }
}


// 读取并处理文件中的每个汉字
/* 这方法是注释掉的,也可以使用,按二进制读取文件。
void decode_to_binary(const std::string& input_file, const std::string& output_file) {
    std::ifstream ifs(input_file, std::ios::binary);
    std::ofstream ofs(output_file, std::ios::binary);
    if (!ifs.is_open() || !ofs.is_open()) {
        std::cerr << "无法打开文件" << std::endl;
        return;
    }

    char byte;
    // 用于存储单个汉字的3个字节
    std::string hanzi;

    while (ifs.get(byte)) {
        if ((byte & 0x80) == 0) {
            // ASCII字符,直接处理
            // ...处理ASCII字符...
        } else {
            // 汉字的第一个字节
            hanzi.push_back(byte);
            // 读取剩余的两个字节
            for (int i = 0; i < 2; ++i) {
                if (ifs.get(byte)) {
                    hanzi.push_back(byte);
                } else {
                    // 文件结束或读取错误
                    std::cerr << "文件读取错误" << std::endl;
                    return;
                }
            }
            // 查找汉字对应的字节值并写入输出文件
            auto it = hanzi_to_byte.find(hanzi);
            if (it != hanzi_to_byte.end()) {
                ofs.write(reinterpret_cast<const char*>(&it->second), sizeof(uint8_t));
            } else {
                std::cerr << "映射中未找到汉字: " << hanzi << std::endl;
            }
            // 清空hanzi字符串以读取下一个汉字
            hanzi.clear();
        }
    }
}
*/

// 将宽字符串转换为UTF-8编码的普通字符串
std::string wstring_to_utf8(const std::wstring& wstr) {
    // 定义一个用于存储转换后字符串的变量
    std::string converted_str;
    // 初始化转换状态对象
    std::mbstate_t state = std::mbstate_t();
    // 获取宽字符串的指针
    const wchar_t* wstr_ptr = wstr.data();
    // 计算转换为多字节字符串所需的长度
    size_t len = std::wcsrtombs(nullptr, &wstr_ptr, 0, &state);
    // 调整目标字符串的大小以适应转换后的长度
    converted_str.resize(len);
    // 执行实际的转换操作
    std::wcsrtombs(&converted_str[0], &wstr_ptr, len, &state);
    // 返回转换后的UTF-8字符串
    return converted_str;
}


//这方法通用一些,读取宽字符。将包含汉字的文件解码为二进制文件
void decode_to_binary(const std::string& input_file, const std::string& output_file) {
    // 以宽字符模式打开输入文件
    std::wifstream ifs(input_file);
    // 设置文件流的locale为UTF-8编码
    ifs.imbue(std::locale("en_US.UTF-8"));
    // 以二进制模式打开输出文件
    std::ofstream ofs(output_file, std::ios::binary);
    // 检查文件是否成功打开
    if (!ifs.is_open() || !ofs.is_open()) {
        // 如果无法打开文件,则输出错误信息并返回
        std::wcerr << L"无法打开文件" << std::endl;
        return;
    }
    // 定义一个宽字符变量用于读取输入文件中的字符
    wchar_t wch;
    // 循环读取每个字符
    while (ifs.get(wch)) {
        // 检查读取的字符是否是ASCII字符
        if (wch <= 0x7F) {
            // 如果是ASCII字符,则直接处理(此处省略具体处理代码)
            // ...处理ASCII字符...
        } else {
            // 如果不是ASCII字符,则认为是汉字字符
            // 创建一个只包含一个汉字的宽字符串
            std::wstring hanzi(1, wch);
            // 将宽字符串转换为UTF-8编码的字符串
            std::string s = wstring_to_utf8(hanzi);
            // 在映射中查找转换后的字符串对应的字节值
            auto it = hanzi_to_byte.find(s);
            if (it != hanzi_to_byte.end()) {
                // 如果找到,则将字节值写入输出文件
                ofs.write(reinterpret_cast<const char*>(&it->second), sizeof(uint8_t));
            } else {
                // 如果未找到,则输出错误信息
                std::wcerr << L"映射中未找到汉字: " << hanzi << std::endl;
            }
        }
    }
}

int main(int argc, char *argv[])
{
    QCoreApplication a(argc, argv);

    for (const auto& pair : byte_to_hanzi) {
        hanzi_to_byte[pair.second] = pair.first;
    }
    // 调用编码函数
    encode_to_hanzi("1.xls", "hanzi_output.txt");

    // 调用解码函数
    decode_to_binary("hanzi_output.txt", "2.xls");


    return a.exec();
}

这篇关于C++将任意文件编码为汉字文本实现简单加密,将汉字文本转码还原原文件实现解密的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1065008

相关文章

SpringBoot3实现Gzip压缩优化的技术指南

《SpringBoot3实现Gzip压缩优化的技术指南》随着Web应用的用户量和数据量增加,网络带宽和页面加载速度逐渐成为瓶颈,为了减少数据传输量,提高用户体验,我们可以使用Gzip压缩HTTP响应,... 目录1、简述2、配置2.1 添加依赖2.2 配置 Gzip 压缩3、服务端应用4、前端应用4.1 N

SpringBoot实现数据库读写分离的3种方法小结

《SpringBoot实现数据库读写分离的3种方法小结》为了提高系统的读写性能和可用性,读写分离是一种经典的数据库架构模式,在SpringBoot应用中,有多种方式可以实现数据库读写分离,本文将介绍三... 目录一、数据库读写分离概述二、方案一:基于AbstractRoutingDataSource实现动态

Python FastAPI+Celery+RabbitMQ实现分布式图片水印处理系统

《PythonFastAPI+Celery+RabbitMQ实现分布式图片水印处理系统》这篇文章主要为大家详细介绍了PythonFastAPI如何结合Celery以及RabbitMQ实现简单的分布式... 实现思路FastAPI 服务器Celery 任务队列RabbitMQ 作为消息代理定时任务处理完整

Java枚举类实现Key-Value映射的多种实现方式

《Java枚举类实现Key-Value映射的多种实现方式》在Java开发中,枚举(Enum)是一种特殊的类,本文将详细介绍Java枚举类实现key-value映射的多种方式,有需要的小伙伴可以根据需要... 目录前言一、基础实现方式1.1 为枚举添加属性和构造方法二、http://www.cppcns.co

使用Python实现快速搭建本地HTTP服务器

《使用Python实现快速搭建本地HTTP服务器》:本文主要介绍如何使用Python快速搭建本地HTTP服务器,轻松实现一键HTTP文件共享,同时结合二维码技术,让访问更简单,感兴趣的小伙伴可以了... 目录1. 概述2. 快速搭建 HTTP 文件共享服务2.1 核心思路2.2 代码实现2.3 代码解读3.

MySQL双主搭建+keepalived高可用的实现

《MySQL双主搭建+keepalived高可用的实现》本文主要介绍了MySQL双主搭建+keepalived高可用的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、测试环境准备二、主从搭建1.创建复制用户2.创建复制关系3.开启复制,确认复制是否成功4.同

Python使用自带的base64库进行base64编码和解码

《Python使用自带的base64库进行base64编码和解码》在Python中,处理数据的编码和解码是数据传输和存储中非常普遍的需求,其中,Base64是一种常用的编码方案,本文我将详细介绍如何使... 目录引言使用python的base64库进行编码和解码编码函数解码函数Base64编码的应用场景注意

Java实现文件图片的预览和下载功能

《Java实现文件图片的预览和下载功能》这篇文章主要为大家详细介绍了如何使用Java实现文件图片的预览和下载功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... Java实现文件(图片)的预览和下载 @ApiOperation("访问文件") @GetMapping("

使用Sentinel自定义返回和实现区分来源方式

《使用Sentinel自定义返回和实现区分来源方式》:本文主要介绍使用Sentinel自定义返回和实现区分来源方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Sentinel自定义返回和实现区分来源1. 自定义错误返回2. 实现区分来源总结Sentinel自定

Mysql表的简单操作(基本技能)

《Mysql表的简单操作(基本技能)》在数据库中,表的操作主要包括表的创建、查看、修改、删除等,了解如何操作这些表是数据库管理和开发的基本技能,本文给大家介绍Mysql表的简单操作,感兴趣的朋友一起看... 目录3.1 创建表 3.2 查看表结构3.3 修改表3.4 实践案例:修改表在数据库中,表的操作主要