在C++中重新实现了一些C#加密内容的问题。使用加密

发布于 2025-02-06 23:13:37 字数 1641 浏览 0 评论 0原文

因此,我有这件C#代码:

void Decrypt(Stream input, Stream output, string password, int bufferSize) {
    using (var algorithm = Aes.Create()) {
        var IV = new byte[16];
        input.Read(IV, 0, 16);
        algorithm.IV = IV;
        var key = new Rfc2898DeriveBytes(password, algorithm.IV, 100);
        algorithm.Key = key.GetBytes(16);
        using(var decryptor = algorithm.CreateDecryptor())
        using(var cryptoStream = new CryptoStream(input, decryptor, CryptoStreamMode.Read)) {
            CopyStream(cryptoStream, output, bufferSize);
        }
    }
}

我试图用CryptoPP将其转换为C ++。 因此,这就是我写的:

void decrypt(std::ifstream& in_file, std::ofstream& out_file, std::string_view password, size_t bufSize) {
    using namespace CryptoPP;
    // Get IV
    byte iv[16];
    in_file.read(reinterpret_cast<char*>(iv), sizeof(iv));
    // Read cypher
    std::string cypher;
    while (in_file && cypher.size() != bufSize) {
        char c;
        in_file.read(&c, 1);
        cypher.push_back(c);
    }
    // Get key
    byte key[16];
    PKCS5_PBKDF2_HMAC<SHA1> pbkdf2;
    pbkdf2.DeriveKey(key, sizeof(key), 0, reinterpret_cast<const byte*>(password.data()), password.size(), iv, sizeof(iv), 100);
    // Decrypt
    CTR_Mode<AES>::Decryption decrypt(key, sizeof(key), iv);
    std::string output;
    StringSource(cypher, true, new StreamTransformationFilter(decrypt, new StringSink(output)));
    // Write output to file
    out_file.write(output.data(), output.size());
}

但是,从这个功能中,我只能恢复垃圾数据。我会做错什么?

谢谢

Tuxifan!

So I have this piece of C# code:

void Decrypt(Stream input, Stream output, string password, int bufferSize) {
    using (var algorithm = Aes.Create()) {
        var IV = new byte[16];
        input.Read(IV, 0, 16);
        algorithm.IV = IV;
        var key = new Rfc2898DeriveBytes(password, algorithm.IV, 100);
        algorithm.Key = key.GetBytes(16);
        using(var decryptor = algorithm.CreateDecryptor())
        using(var cryptoStream = new CryptoStream(input, decryptor, CryptoStreamMode.Read)) {
            CopyStream(cryptoStream, output, bufferSize);
        }
    }
}

and I am trying to translate this into C++ with CryptoPP.
So this is what I have written:

void decrypt(std::ifstream& in_file, std::ofstream& out_file, std::string_view password, size_t bufSize) {
    using namespace CryptoPP;
    // Get IV
    byte iv[16];
    in_file.read(reinterpret_cast<char*>(iv), sizeof(iv));
    // Read cypher
    std::string cypher;
    while (in_file && cypher.size() != bufSize) {
        char c;
        in_file.read(&c, 1);
        cypher.push_back(c);
    }
    // Get key
    byte key[16];
    PKCS5_PBKDF2_HMAC<SHA1> pbkdf2;
    pbkdf2.DeriveKey(key, sizeof(key), 0, reinterpret_cast<const byte*>(password.data()), password.size(), iv, sizeof(iv), 100);
    // Decrypt
    CTR_Mode<AES>::Decryption decrypt(key, sizeof(key), iv);
    std::string output;
    StringSource(cypher, true, new StreamTransformationFilter(decrypt, new StringSink(output)));
    // Write output to file
    out_file.write(output.data(), output.size());
}

However, from this function, I am only getting back trash data. What could I be doing wrong?

Thanks

Tuxifan!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

罪歌 2025-02-13 23:13:38

所以我找到了解决方案!首先,如@MBD所述,C#默认使用CBC。此外,我需要将其余数据删除:

while ((cipher.size() % 16) != 0) {
    cipher.pop_back();
}

So I found the solution! First of all, as @mbd mentioned, C# uses CBC by default. Additionally, I need to cut away the rest of the data like this:

while ((cipher.size() % 16) != 0) {
    cipher.pop_back();
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文