简体   繁体   English

使用AES和XTR-DH Crypto ++加密音频文件

[英]Encrypt an audio file with AES and XTR-DH Crypto++

I've done write code for encrypt an audio file with WAV format with AES CFB mode and XTR-DH for generate secret key. 我已经完成了编写代码,用于使用AES CFB模式加密WAV格式的音频文件,并使用XTR-DH加密生成密钥。

The code here 这里的代码

int main()
{
AutoSeededRandomPool aSRPA;
XTR_DH xtrA(aSRPA, 170, 160);

string inputfilename;
string encryptfilename;
int aesKeyLength = SHA256::DIGESTSIZE;
int defBlockSize = AES::BLOCKSIZE;
const std::string extention(".wav");

byte iv[AES::BLOCKSIZE];
aSRPA.GenerateBlock(iv, AES::BLOCKSIZE);
SecByteBlock privateA(xtrA.PrivateKeyLength());
SecByteBlock publicA(xtrA.PublicKeyLength());
SecByteBlock secretKeyA(xtrA.AgreedValueLength());
xtrA.GenerateKeyPair(aSRPA, privateA, publicA);

SecByteBlock key(SHA256::DIGESTSIZE);
SHA256().CalculateDigest(key, secretKeyA, secretKeyA.size());

cout << "file path plain text : ";
cin >> inputfilename;
cout << "file path cipher text : ";
cin >> encryptfilename;

        if (inputfilename != extention &&
            encryptfilename != extention &&
            inputfilename.size() > extention.size() &&
            encryptfilename.size() > extention.size() &&
            inputfilename.substr(inputfilename.size() - extention.size()) == 
            ".wav" &&
            encryptfilename.substr(encryptfilename.size() - 
            extention.size()) == ".wav")
        {


            CFB_Mode<AES>::Encryption cfbEncryption(key, aesKeyLength, iv);
            cfbEncryption.ProcessData((byte*)inputfilename.c_str(), 
                                      (byte*)inputfilename.c_str(), 
                                      inputfilename.length() + 1);


            FileSink file_kunci(encryptfilename.c_str());
            StringSource ss(inputfilename, true,
                new StreamTransformationFilter(cfbEncryption,
                    new Redirector(file_kunci)));
         }
}

It's work but when I read the encrypted file, it's not a wave form like an audio file. 可以,但是当我读取加密文件时,它不是像音频文件那样的波形。 What's wrong with my code ? 我的代码有什么问题? Is it need to convert the audio file to array of byte or something ? 是否需要将音频文件转换为字节数组或其他内容?

It's not clear what you actually want as your question title and code differ, therefore I'm going to take your code as reference. 由于您的问题标题和代码不同,因此尚不清楚您真正想要的是什么,因此,我将以您的代码为参考。 To me it looks like you want to encrypt a file and decrypt it later. 对我来说,您似乎想加密文件并在以后解密。

Your big if block is really too much... you can do everything pretty easiely with std::filesystem . 您的大if块实在太多了……您可以使用std::filesystem轻松完成所有操作。

Also you seem to be mixing things up, this can happen if you are not familiar with cipher algorithms and/or the documentation of the library itself. 同样,您似乎正在混淆,如果您不熟悉密码算法和/或库本身的文档,则可能会发生这种情况。 First you ProcessData which seems to encrypt the string in inputfilename (?). 首先, ProcessData ,这似乎在加密字符串inputfilename (?)。

After all, you might want to take a look into the documentation of the library and the provided samples - these already help you a lot to understand how things are handled. 毕竟,您可能想看一下该库的文档和所提供的示例-这些已经对您大有帮助,以帮助您了解如何进行处理。

Here's a working self-contained sample of encryption and decryption of a file. 这是文件加密和解密的有效的独立示例。

#include <filesystem>

#include "..\CryptoLib\osrng.h"
#include "..\CryptoLib\modes.h"
#include "..\CryptoLib\files.h"
#include "..\CryptoLib\aes.h"

template <typename T, std::size_t N>
constexpr std::size_t FixedArraySize(T(&arr)[N])
{ return N; }

int wmain(int argv, wchar_t **args)
{
    try
    {
        using namespace CryptoPP;

        constexpr CryptoPP::byte    IV[]    = { 0xab,0x10,0x55,0x29,0x73,0xb9,0xae,0xba,0xe9,0xe9,0xe5,0x26,0x35,0x66,0x63,0x55 };
        constexpr char              key[]   = "ABCDEFGHIJKLMNOP";

        const std::string extension         = ".TEST";
        const std::string sourceFile        = __FILE__;
        const std::string destinationFile   = std::filesystem::path("Encrypted").replace_extension(extension).string();
        const std::string finalFile         = std::filesystem::path("Decrypted").replace_extension(extension).string();

        {
            CFB_Mode<AES>::Encryption encryption(reinterpret_cast<const CryptoPP::byte*>(key), FixedArraySize(key) - 1, IV);
            FileSource fsEnc(sourceFile.c_str(), true, new StreamTransformationFilter(encryption, new FileSink(destinationFile.c_str())));

            CFB_Mode<AES>::Decryption decryption(reinterpret_cast<const CryptoPP::byte*>(key), FixedArraySize(key) - 1, IV);
            FileSource fsDec(destinationFile.c_str(), true, new StreamTransformationFilter(decryption, new FileSink(finalFile.c_str())));
        }

        std::filesystem::remove(destinationFile);
        std::filesystem::remove(finalFile);
    }
    catch (const std::exception &e)
    {
        std::cout << e.what() << std::endl;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM