如何创建与OpenSSL C ++输出进行比较的OpenSSL输出以生成单元测试? [英] How can I create OpenSSL output that compares to OpenSSL C++ output to generate unit tests?

查看:143
本文介绍了如何创建与OpenSSL C ++输出进行比较的OpenSSL输出以生成单元测试?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用C ++包装器进行OpenSSL的AES-256-CBC加密,其遵循 OpenSSL文档.这意味着需要一些数据进行加密,一个密钥和一个初始化向量,然后返回加密的字节.

I'm using a C++ wrapper for OpenSSL's AES-256-CBC encryption that follows the recipe from OpenSSL docs. It's meant to take some data to encrypt, a key and an initialization vector, and return encrypted bytes.

就相对而言,加密和解密工作正常.我可以加密任何东西并将其解密并检索原始数据.但是,我想在绝对意义上进行测试 .因此,我在命令行/终端中使用OpenSSL创建加密数据,然后将其注入测试中.但是输出不匹配,我不确定从哪里开始或如何系统地解决这个问题.

Now on the relative sense, encryption and decryption works fine. I can encrypt anything and decrypt it and retrieve the original data. However, I'd like to test it in the absolute sense. So, I'm using OpenSSL in command line/terminal to create encrypted data, and then inject it in my tests. But the outputs are not matching, and I'm not sure where to start or how to systematically tackle this.

要生成十六进制数据:

printf '%s' 'Hello world!' | xxd -ps

返回48656c6c6f20776f726c6421.

然后,使用OpenSSL对其进行加密:

Then, to encrypt it with OpenSSL:

printf '%s' '48656c6c6f20776f726c6421' | xxd -r -ps | openssl aes-256-cbc -iv 00000000000000000000000000000000 -k 0000000000000000000000000000000000000000000000000000000000000000 -nosalt | xxd -ps

给出f1b486ded0fd22dad9dbdab2205f61c2.

xxd -r将十六进制转换为字节,而xxd则相反.我一开始也尝试了不带xxd的普通Hello world!,但这也是错误的.结果总是与我在C ++ OpenSSL代码中得到的不匹配.我在这里想念什么?

xxd -r converts hex to bytes, and xxd does the opposite. I tried also with plain Hello world! without xxd in the beginning, but also is wrong. The result always doesn't match what I get in the C++ OpenSSL code. What am I missing here?

在我的代码的最小,可验证的完整示例中结果.

推荐答案

我知道了.问题是我使用了错误的命令行参数.密钥应为-K,而不是-k.这是正确的格式:

I figured it out. The problem was that I'm using the wrong command line parameter. It should be -K for the key, not -k. This is the correct format:

printf '%s' '48656c6c6f20776f726c6421' | xxd -r -ps | openssl aes-256-cbc -iv 00000000000000000000000000000000 -K 0000000000000000000000000000000000000000000000000000000000000000 -nosalt | xxd -ps

这给出了与C ++中的OpenSSL完全相同的结果.

and this gives the exact same result as OpenSSL in C++.

这篇关于如何创建与OpenSSL C ++输出进行比较的OpenSSL输出以生成单元测试?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆