Qt对二进制文件进行加密及解密操作

ops/2024/10/17 7:52:09/

在工作中可能会做一些二进制文件加密及解密的任务,比如说仪器的时序指令bin文件。

#include <iostream>
#include <fstream>
#include <vector>
#include <QCryptographicHash>
#include <QFile>
#include <QDataStream>
#include <QByteArray>
#include <QIODevice>// 加密密钥(128位)
const QByteArray encryptionKey = "0123456789abcdef";// 加密函数
void encryptFile(const QString& inputFile, const QString& outputFile) {QFile input(inputFile);QFile output(outputFile);if (!input.open(QIODevice::ReadOnly) || !output.open(QIODevice::WriteOnly)) {std::cout << "Failed to open the input or output file." << std::endl;return;}// 创建一个存储文件内容的缓冲区QByteArray buffer = input.readAll();// 加密文件内容QByteArray encryptedData = QCryptographicHash::hash(buffer, QCryptographicHash::Sha256);QByteArray encryptedKey = QCryptographicHash::hash(encryptionKey, QCryptographicHash::Sha256);for (int i = 0; i < buffer.size(); ++i) {encryptedData[i] = encryptedData[i] ^ encryptedKey[i % encryptedKey.size()];}// 写入加密后的文件内容QDataStream outputStream(&output);outputStream.writeRawData(encryptedData.constData(), encryptedData.size());// 关闭文件input.close();output.close();std::cout << "File encrypted successfully." << std::endl;
}// 解密函数
void decryptFile(const QString& inputFile, const QString& outputFile) {QFile input(inputFile);QFile output(outputFile);if (!input.open(QIODevice::ReadOnly) || !output.open(QIODevice::WriteOnly)) {std::cout << "Failed to open the input or output file." << std::endl;return;}// 创建一个存储文件内容的缓冲区QByteArray buffer = input.readAll();// 解密文件内容QByteArray decryptedData = buffer;QByteArray encryptedKey = QCryptographicHash::hash(encryptionKey, QCryptographicHash::Sha256);for (int i = 0; i < decryptedData.size(); ++i) {decryptedData[i] = decryptedData[i] ^ encryptedKey[i % encryptedKey.size()];}// 写入解密后的文件内容QDataStream outputStream(&output);outputStream.writeRawData(decryptedData.constData(), decryptedData.size());// 关闭文件input.close();output.close();std::cout << "File decrypted successfully." << std::endl;
}int main() {QString inputFile = "input_file.bin";QString encryptedFile = "encrypted_file.bin";QString decryptedFile = "decrypted_file.bin";encryptFile(inputFile, encryptedFile);decryptFile(encryptedFile, decryptedFile);return 0;
}


http://www.ppmy.cn/ops/48024.html

相关文章

访问网站时IP被阻止?原因及解决方法

在互联网上&#xff0c;用户可能会面临一个令人困扰的问题——当尝试访问某个特定的网站时&#xff0c;却发现自己的IP地址被该网站屏蔽。 IP地址被网站屏蔽是一个相对常见的现象&#xff0c;而导致这种情况的原因多种多样&#xff0c;包括恶意行为、违规访问等。本文将解释IP地…

第十五届蓝桥杯pb组国赛E题[马与象] (15分)BFS算法 详解

博客主页&#xff1a;誓则盟约 系列专栏&#xff1a;IT竞赛 专栏 关注博主&#xff0c;后期持续更新系列文章 如果有错误感谢请大家批评指出&#xff0c;及时修改 感谢大家点赞&#x1f44d;收藏⭐评论✍ 问题描述&#xff1a; 小蓝有一个大小为 N N 的棋盘&#xff08;棋…

BC C language

题目汇总 No.1 打印有规律的字符(牛牛的字符菱形) 代码展示 #include<stdio.h> int main() {char ch0;scanf("%c",&ch);for(int i0;i<5;i){for(int j0;j<5;j){if((i0||i4)&&j2)printf("%c", ch);else if ((i 1||i3) &&…

Mysql中表的常用约束

在MySQL表中常用的约束有以下几种&#xff1a; 1. 主键约束&#xff08;Primary Key Constraint&#xff09;&#xff1a;用于标识表中的唯一记录。一个表只能有一个主键&#xff0c;主键列不能有重复值&#xff0c;也不能为NULL。 2. 唯一约束&#xff08;Unique Constraint…

Django与MySQL:配置数据库的详细步骤

文章目录 Django-MySQL 配置配置完执行数据迁移&#xff0c;如果报错: Error loading MySQLdb module&#xff0c; Django-MySQL 配置 # settings.pyDATABASES {# 默认配置sqlite3数据库# default: {# ENGINE: django.db.backends.sqlite3,# NAME: BASE_DIR / db.sqli…

Python | Leetcode Python题解之第135题分发糖果

题目&#xff1a; 题解&#xff1a; class Solution:def candy(self, ratings: List[int]) -> int:n len(ratings)ret 1inc, dec, pre 1, 0, 1for i in range(1, n):if ratings[i] > ratings[i - 1]:dec 0pre (1 if ratings[i] ratings[i - 1] else pre 1)ret p…

springboot接收byte[]字节

在Spring Boot中&#xff0c;可以使用RequestBody注解来接收字节流。以下是一个简单的示例&#xff1a; 1. 首先&#xff0c;创建一个控制器类&#xff0c;如ByteController&#xff1a; java import org.springframework.web.bind.annotation.PostMapping; import org.sprin…

DeepSpeed Mixture-of-Quantization (MoQ)

属于QAT (Quantization-Aware Training)的一种&#xff0c;训练阶段用量化。 特点是&#xff1a; 1. 从16-bit INT开始训练&#xff0c;逐渐减1bit&#xff0c;训练一些steps就减1bit&#xff0c;直至减至8bit INT&#xff1b; 2. &#xff08;可选&#xff0c;不一定非用&a…