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# winform组件 怎样集成

    在C# WinForms应用程序中集成组件通常涉及以下几个步骤: 添加引用: 首先,确保你已经安装了所需的组件。例如,如果你想要集成一个第三方库,你需要将该库的DLL...

  • c# winform组件 怎样提高效率

    要提高C# WinForms应用程序的效率,可以从多个方面入手,包括优化绘图过程、使用异步操作、合理设置控件属性等。以下是一些具体的优化策略:
    优化绘图过程 ...

  • c# winform组件 怎样自定义

    在C#中,要自定义WinForms组件,您需要创建一个继承自现有控件的新类,并重写其构造函数、事件处理程序和其他相关方法。以下是一个简单的示例,说明如何创建一个...

  • c# winform组件 怎样选择合适

    在C# WinForms应用程序开发中,选择合适的组件对于构建高效、用户友好的界面至关重要。以下是一些选择合适组件的建议:
    控件选择 根据需求选择:选择控件时...

  • 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...