117.info
人生若只如初见

c++文件加密解密的方法是什么

在C++中进行文件加密和解密通常采用对称加密算法,其中最常用的是AES(Advanced Encryption Standard)算法。以下是一个简单的C++代码示例,演示如何使用AES算法进行文件加密和解密:

#include 
#include 
#include 
#include 

void encryptFile(const std::string& inputFile, const std::string& outputFile, const std::string& key) {
    AES_KEY aesKey;
    AES_set_encrypt_key((unsigned char*)key.c_str(), 128, &aesKey);

    std::ifstream in(inputFile, std::ios::binary);
    std::ofstream out(outputFile, std::ios::binary);

    unsigned char iv[AES_BLOCK_SIZE];
    RAND_bytes(iv, AES_BLOCK_SIZE);
    out.write(reinterpret_cast(iv), AES_BLOCK_SIZE);

    unsigned char inBuffer[AES_BLOCK_SIZE];
    unsigned char outBuffer[AES_BLOCK_SIZE];
    int numBytesRead = 0;
    while (in.read(reinterpret_cast(inBuffer), AES_BLOCK_SIZE)) {
        AES_cbc_encrypt(inBuffer, outBuffer, AES_BLOCK_SIZE, &aesKey, iv, AES_ENCRYPT);
        out.write(reinterpret_cast(outBuffer), AES_BLOCK_SIZE);
        numBytesRead += AES_BLOCK_SIZE;
    }

    in.close();
    out.close();
}

void decryptFile(const std::string& inputFile, const std::string& outputFile, const std::string& key) {
    AES_KEY aesKey;
    AES_set_decrypt_key((unsigned char*)key.c_str(), 128, &aesKey);

    std::ifstream in(inputFile, std::ios::binary);
    std::ofstream out(outputFile, std::ios::binary);

    unsigned char iv[AES_BLOCK_SIZE];
    in.read(reinterpret_cast(iv), AES_BLOCK_SIZE);

    unsigned char inBuffer[AES_BLOCK_SIZE];
    unsigned char outBuffer[AES_BLOCK_SIZE];
    int numBytesRead = 0;
    while (in.read(reinterpret_cast(inBuffer), AES_BLOCK_SIZE)) {
        AES_cbc_encrypt(inBuffer, outBuffer, AES_BLOCK_SIZE, &aesKey, iv, AES_DECRYPT);
        out.write(reinterpret_cast(outBuffer), AES_BLOCK_SIZE);
        numBytesRead += AES_BLOCK_SIZE;
    }

    in.close();
    out.close();
}

int main() {
    std::string inputFile = "plaintext.txt";
    std::string encryptedFile = "encrypted.bin";
    std::string decryptedFile = "decrypted.txt";
    std::string key = "mysecretkey";

    encryptFile(inputFile, encryptedFile, key);
    decryptFile(encryptedFile, decryptedFile, key);

    return 0;
}

在上面的示例中,encryptFile函数用于加密文件,decryptFile函数用于解密文件。需要注意的是,需要安装OpenSSL库,并在编译时链接对应的库文件。此外,文件加密和解密的过程中需要使用相同的密钥。

未经允许不得转载 » 本文链接:https://www.117.info/ask/fe9ebAzsICABQA1Q.html

推荐文章

  • C++中setcursel函数的用法是什么

    在C++中,SetCurSel函数是用于设置组合框(ComboBox)或列表框(ListBox)中当前选中项的函数。
    语法:
    int SetCurSel(int nIndex); 参数: nIndex:要...

  • c++迭代器调用失败怎么解决

    当C++迭代器调用失败时,可能是由于以下几个原因导致的: 迭代器未初始化:在使用迭代器之前,确保已正确初始化迭代器。例如,在使用STL容器的迭代器时,需要确保...

  • c++中set的用法是什么

    在C++中,set是一种容器,用于存储一组唯一的元素,按照一定的顺序进行排序。set的用法如下: 包含头文件:
    声明一个set对象:std::set set_name;
    插入...

  • c++中的beginthreadex有什么用途

    在C++中,_beginthreadex 函数用于创建一个新的线程。
    它有以下用途: 多线程编程:_beginthreadex 允许在程序中创建多个线程,从而实现并发执行。这样可以...

  • java锁的使用场景有哪些

    多线程访问共享资源的场景:在多线程环境下,为了保证数据的一致性和安全性,通常会使用锁来控制对共享资源的访问,避免出现数据竞争和并发访问引起的问题。 生产...

  • qt中字符串转换的方法有哪些

    在Qt中,字符串转换的方法有以下几种: 使用QString的toStdString()方法将QString转换为std::string。
    使用QString的toUtf8()方法将QString转换为QByteArra...

  • ubuntu vncserver安装的步骤是什么

    在终端中输入以下命令来安装 VNC 服务器软件: sudo apt-get update
    sudo apt-get install vnc4server 输入以下命令来启动 VNC 服务器并设置密码: vncserv...

  • python字符串加密的方法是什么

    Python中常见的字符串加密方法包括: 使用 hashlib 模块进行哈希加密,如 MD5、SHA-1、SHA-256 等加密算法。
    示例代码: import hashlib text = "Hello, Wo...