zoukankan      html  css  js  c++  java
  • C++ 文件加解密

    通过文件二进制数据 与密钥进行异或处理,可加密文件


    #ifndef __ENCRYPT__HEAD__
    #define __ENCRYPT__HEAD__

    #include <fstream>
    #include <vector>

    namespace Crypto {

       
    /*
    加密文件数据:
    参数:
    filePath    需要加密的文件路径
    key            用于加密的密钥
    size        文件大小
    返回值:
    char*        加密后的二进制数据(可将此数据保存至文件)
    */
    char* encrypt(const char* inFilePath, const char* inKey, int &outSize)
    {
        //以二进制方式读取文件
        std::ifstream fin(inFilePath, std::ios::binary);
        //保存文件二进制数据
        std::vector<char> buf;
        //
        char ch;
        //读取每个字节,并保存在buf容器里
        while( fin.get(ch) ){
            buf.push_back(ch);
        }
        fin.clear();
        fin.close();
        //文件数据大小
        int size = buf.size();
        outSize = size;
        //字节数组
        char* data = new char[size + 1];
        //把二进制数据复制到数组
        std::copy(buf.begin(), buf.end(), data);
        //结束
        data[size] = 0;
        int keyLen = strlen(inKey);
        for (int i = 0; i < size; i++) {
            //将每个字节与key作异或处理
            data[i] ^= inKey[i >= keyLen?i%keyLen:i];
        }
        return data;
    }

    }

    #endif

  • 相关阅读:
    使用element-ui的table组件时,渲染为html格式
    vue-quill-editor富文本编辑器,添加了汉化样式却汉化不了
    MySQL版本问题导致的SQLException
    MySQL中 ORDER BY 与 LIMIT 的执行顺序
    MySQL 测试数据批量导入
    CentOS 7 安装 Maven
    CentOS 7 安装 Gradle
    CentOS 7 安装 RabbitMQ
    CentOS 7 安装 Tomcat 8.5.43
    CentOS 7 配置网络
  • 原文地址:https://www.cnblogs.com/imzhstar/p/4096879.html
Copyright © 2011-2022 走看看