最近呢,需要做一个对视频进行加密和解密的功能,现在记录一下,方便以后查找,以前也弄过但是没记录造成现在只能重新弄,也方便以后不用再重新写了。
视频加密方式也就是,取出一段固定大小的数据,进行加密处理
加密方式:异或操作
-
fopen
方式操作文件
#include <iostream>
#include <cstdio>
// 加密函数
void encryptFile(const std::string& inputFile, const std::string& outputFile, const std::string& key) {
const size_t bufferSize = 4096;
char readBuffer[bufferSize], writeBuffer[bufferSize];
FILE* inFile = fopen(inputFile.c_str(), "rb");
FILE* outFile = fopen(outputFile.c_str(), "wb");
while (!feof(inFile)) {
size_t bytesRead = fread(readBuffer, 1, bufferSize, inFile);
for (size_t i = 0; i < bytesRead; ++i) {
writeBuffer[i] = readBuffer[i] ^ key[i % key.size()];
}
fwrite(writeBuffer, 1, bytesRead, outFile);
}
fclose(inFile);
fclose(outFile);
}
// 解密函数
void decryptFile(const std::string& inputFile, const std::string& outputFile, const std::string& key) {
const size_t bufferSize = 4096;
char readBuffer[bufferSize], writeBuffer[bufferSize];
FILE* inFile = fopen(inputFile.c_str(), "rb");
FILE* outFile = fopen(outputFile.c_str(), "wb");
while (!feof(inFile)) {
size_t bytesRead = fread(readBuffer, 1, bufferSize, inFile);
for (size_t i = 0; i < bytesRead; ++i) {
writeBuffer[i] = readBuffer[i] ^ key[i % key.size()];
}
fwrite(writeBuffer, 1, bytesRead, outFile);
}
fclose(inFile);
fclose(outFile);
}
int main()
{
std::string inputFile = "input_video.mp4"; // 输入视频文件路径
std::string encryptedFile = "encrypted_video"; // 加密后的文件输出路径
std::string decryptedFile = "decrypted_video.mp4"; // 解密后的文件输出路径
std::string encryptionKey = "123456password"; // 加密密钥,必须保密
encryptFile(inputFile, encryptedFile, encryptionKey); // 加密视频文件
decryptFile(encryptedFile, decryptedFile, encryptionKey); // 解密视频文件
return 0;
}
fopen_s
方式操作文件
#include <iostream>
#include <cstdio>
// 加密函数
void encryptFile(const std::string& inputFile, const std::string& outputFile, const std::string& key) {
const size_t bufferSize = 4096;
char readBuffer[bufferSize], writeBuffer[bufferSize];
FILE* inFile;
if (fopen_s(&inFile, inputFile.c_str(), "rb") != 0) {
std::cerr << "Failed to open input file." << std::endl;
return;
}
FILE* outFile;
if (fopen_s(&outFile, outputFile.c_str(), "wb") != 0) {
std::cerr << "Failed to open output file." << std::endl;
fclose(inFile);
return;
}
while (!feof(inFile)) {
size_t bytesRead = fread(readBuffer, 1, bufferSize, inFile);
for (size_t i = 0; i < bytesRead; ++i) {
writeBuffer[i] = readBuffer[i] ^ key[i % key.size()];
}
fwrite(writeBuffer, 1, bytesRead, outFile);
}
fclose(inFile);
fclose(outFile);
}
// 解密函数
void decryptFile(const std::string& inputFile, const std::string& outputFile, const std::string& key) {
const size_t bufferSize = 4096;
char readBuffer[bufferSize], writeBuffer[bufferSize];
FILE* inFile;
if (fopen_s(&inFile, inputFile.c_str(), "rb") != 0) {
std::cerr << "Failed to open input file." << std::endl;
return;
}
FILE* outFile;
if (fopen_s(&outFile, outputFile.c_str(), "wb") != 0) {
std::cerr << "Failed to open output file." << std::endl;
fclose(inFile);
return;
}
while (!feof(inFile)) {
size_t bytesRead = fread(readBuffer, 1, bufferSize, inFile);
for (size_t i = 0; i < bytesRead; ++i) {
writeBuffer[i] = readBuffer[i] ^ key[i % key.size()];
}
fwrite(writeBuffer, 1, bytesRead, outFile);
}
fclose(inFile);
fclose(outFile);
}
int main()
{
std::string inputFile = "input_video.mp4"; // 输入视频文件路径
std::string encryptedFile = "encrypted_video"; // 加密后的文件输出路径
std::string decryptedFile = "decrypted_video.mp4"; // 解密后的文件输出路径
std::string encryptionKey = "123456password"; // 加密密钥,必须保密
encryptFile(inputFile, encryptedFile, encryptionKey); // 加密视频文件
decryptFile(encryptedFile, decryptedFile, encryptionKey); // 解密视频文件
return 0;
}
上面的两种都是一种方法,只是打开文件用的函数不同,毕竟有些版本是不能使用
fopen_s
的,所以一起记录。
小孩子才做选择,成年人全都要