- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
C 函数是:
int i2d_PrivateKey(EVP_PKEY *a, unsigned char **pp);
int i2d_X509(X509 *a, unsigned char **ppout);
我已经编写了这样的代码来复制到 std::vector 中:
// populate PrivateKey
std::vector<uint8_t> PrivateKey;
EVP_PKEY *privatekey = NULL;
int size = i2d_PrivateKey(privatekey, NULL);
if (size > 0)
{
PrivateKey.reserve(size);
uint8_t* ptr = &PrivateKey[0];
i2d_PrivateKey(privatekey, &ptr);
std::cout << "PrivateKey size=" << PrivateKey.size() << '\n';
}
PrivateKey.size() 返回零,因此我知道该 vector 尚未填充。但是,我知道 size 是一个正整数,所以执行 if (size block 中的代码。
如果 ptr 是 PrivateKey 数组的起始地址,那么这段代码不应该工作吗?
虽然这段代码使用了openssl,但我认为它更像是一个一般的指针问题。如果我创建一个临时 uint8_t 数组,那么它可以工作,但我宁愿直接复制到 vector 中并节省临时拷贝的开销。
代码如下:
#include <openssl/ssl.h>
#include <openssl/pem.h>
#include <openssl/ossl_typ.h>
#include <openssl/x509.h>
#include <openssl/x509v3.h>
#include <openssl/pkcs12.h>
#include <openssl/err.h>
#include <iostream>
#include <vector>
#include <string>
#include <cstdint>
int main()
{
std::vector<uint8_t> input; // contains pkcs12 data
std::string Password = "password";
std::vector<uint8_t> Certificate;
std::vector<uint8_t> PrivateKey;
OpenSSL_add_all_algorithms();
OpenSSL_add_all_ciphers();
OpenSSL_add_all_digests();
ERR_load_crypto_strings();
PKCS12* p12_cert = NULL;
const uint8_t* p1 = &input[0];
if(d2i_PKCS12(&p12_cert, &p1, input.size()) != NULL) {
EVP_PKEY *privatekey = NULL;
X509 *x509_cert = NULL;
// additional certs, last arg is CA which we don't care about
if (PKCS12_parse(p12_cert, Password.c_str(), &privatekey, &x509_cert, NULL))
{
// populate m_privateKey
int size = i2d_PrivateKey(privatekey, NULL);
std::cout << "privatekey size=" << size << '\n';
if (size > 0)
{
PrivateKey.reserve(size);
uint8_t* ptr = &PrivateKey[0];
i2d_PrivateKey(privatekey, &ptr);
std::cout << "PrivateKey size=" << PrivateKey.size() << '\n';
}
// populate certificate
size = i2d_X509(x509_cert, NULL);
std::cout << "certificate size=" << size << '\n';
if(size > 0)
{
Certificate.reserve(size);
uint8_t* ptr = &Certificate[0];
int ret = i2d_X509(x509_cert, &ptr);
std::cout << "ret=" << ret <<'\n';
std::cout << "cert size=" << Certificate.size() << '\n';
}
}
PKCS12_free(p12_cert);
}
}
更新,可以使用下面的代码来合并 Arron 的修复:
#include <openssl/ssl.h>
#include <openssl/pem.h>
#include <openssl/ossl_typ.h>
#include <openssl/x509.h>
#include <openssl/x509v3.h>
#include <openssl/pkcs12.h>
#include <openssl/err.h>
#include <iostream>
#include <fstream>
#include <iterator>
#include <iomanip>
#include <vector>
#include <string>
#include <cstdint>
using namespace std;
std::vector<uint8_t>& File2Buffer(const std::string path,
std::vector<uint8_t>& buffer) {
fstream fs(path, ios::in | ios::binary);
if (fs) {
// Don't skip new lines
fs.unsetf(ios::skipws);
fs.seekg(0, ios::end);
size_t size = static_cast<size_t>(fs.tellg());
fs.seekg(0, ios::beg);
buffer.reserve(size);
buffer.insert(buffer.begin(),
istream_iterator<uint8_t>(fs),
istream_iterator<uint8_t>());
}
return buffer;
}
int main(int argc, char* argv[])
{
if (argc != 3) {
cout << "Usage: " << argv[0] << " <pkcs12 file> " << "<password>\n";
exit(0);
}
std::vector<uint8_t> input;
File2Buffer(argv[1], input);
std::string Password = argv[2];
std::vector<uint8_t> Certificate;
std::vector<uint8_t> PrivateKey;
OpenSSL_add_all_algorithms();
OpenSSL_add_all_ciphers();
OpenSSL_add_all_digests();
ERR_load_crypto_strings();
PKCS12* p12_cert = NULL;
const uint8_t* p1 = &input[0];
if(d2i_PKCS12(&p12_cert, &p1, input.size()) != NULL) {
EVP_PKEY *privatekey = NULL;
X509 *x509_cert = NULL;
// additional certs, last arg is CA which we don't care about
if (PKCS12_parse(p12_cert, Password.c_str(), &privatekey, &x509_cert, NULL))
{
// populate m_privateKey
int size = i2d_PrivateKey(privatekey, NULL);
std::cout << "privatekey size=" << size << '\n';
if (size > 0)
{
PrivateKey.resize(size);
uint8_t* ptr = &PrivateKey[0];
i2d_PrivateKey(privatekey, &ptr);
std::cout << "PrivateKey size=" << PrivateKey.size() << '\n';
}
// populate certificate
size = i2d_X509(x509_cert, NULL);
std::cout << "certificate size=" << size << '\n';
if(size > 0)
{
Certificate.resize(size);
uint8_t* ptr = &Certificate[0];
int ret = i2d_X509(x509_cert, &ptr);
std::cout << "ret=" << ret <<'\n';
std::cout << "cert size=" << Certificate.size() << '\n';
}
}
PKCS12_free(p12_cert);
}
// test it out:
if (Certificate.size() > 0) {
cout << "Certificate size=" << Certificate.size() << '\n';
for (auto& ch : Certificate) {
cout << hex << ch << " ";
}
}
}
最佳答案
使用调整大小而不是保留。 reserve 的问题在于,如果您执行分配(如 PrivateKey[5] = 5),并调用 PrivateKey.size(),大小仍将保持为 0。(实际上,Reserve 可以与 std::copy 中的 back_inserter 配对使用),但在您的情况下,您应该调整大小。
关于c++ - 给定一个填充 unsigned char** 的 C 函数,如何在没有中间拷贝的情况下用数据填充 std::vector,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55678801/
有人可以给我一个更简单的以下代码的解决方案(它正在展开给定结构 0xFC :: len :: payload :: ... :: 0x0A :: 0x0D 的整数列表): object Payload
我已经在我的网站上安装了 SSL 证书,但 intermediate.crt 无法正常工作。任何 SSL 检查器(例如 GeoTrust Checker)都告诉我,缺少中间 key 。网站上已经使用了
如何让图像从这个框的中间开始? (中间纵横) 最佳答案 有几种方法可以做到这一点,如果它需要在所有浏览器(IE7+ 和其他浏览器)中工作,你需要做不同的事情来让它在某些情况下工作。 使用绝对位置
如何强制 min-height 和 vertical-align:middle 为 td 元素或其内部元素工作? 最佳答案 td 元素上的 height 等同于 min-height,因为如果需要,表
我正在尝试自动滚动到订单簿的中间行。 我有 orderBook div,其中放置了带有 orderBook 的表。该表的其中一行有一个 id middleRow。我想做的是滚动该行并将其放置在 ord
我正在尝试在 javascript 中计算绝对定位元素的 transform-origin 属性,以便它们在悬停时填充整个视口(viewport)。 我尝试通过 x 除以窗口宽度和 y 除以窗口高度来
我有休闲字符串 ' this is my string ' 是否可以删除开头和结尾的所有空格,只在单词之间留一个空格。 要选择我使用过的所有空间: SELECT regexp_replace('
我正在设法创建我的第一个复杂的 J2E 解决方案,并且在每个教程中我都发现了某种中间表的用法,如下所示: 表:用户、用户角色、角色虽然逻辑会简单地向用户表添加一个键来引用它在角色表上的角色,但为什么要
我正在寻找以下解决方案。我想定位一个图像元素,例如 在中间。所以高度是视口(viewport)的高度,宽度会自动设置,图像的中间应该在视口(viewport)宽度的中间。 我搜索的一个例子就像下面的网
我正在设计一种布局,它更像是注册用户的个人仪表板。我让它变得简单,使用基本的 2 列网格,一个用于侧边栏,一个用于主要内容。 因为,例如,80% 的网站使用将发生在一个单独的子系统中,在无 chrom
我有三个不同的 div 标签(不在彼此内部)和代码,所以它有一个把单词放在左边、中间或右边,但中心非常偏离中心。这是 HTML 代码: .desc { float: right; color:
我有以下CSS http://jsbin.com/azivip/75/edit我想让黄色的 div 高度填充蓝色和绿色 div 之间的空间。使用高度继承似乎使 div 超出了绿色 div。 有什么想法
我不得不在其父元素的中间放置一些文本。我用下面的代码实现了它: #div1 { position: relative; margin: 0; padding: 0; } #div2 {
发现一个使用合法证书(由thawte 签名)的网站,但所有浏览器都会拒绝它。我不明白为什么。thawte 的支持告诉我一个域有两个证书,然后将这个 https://www.sslshopper[dot
我正在尝试使用 OpenSSL 创建证书链,但出于某种原因,当我在我的计算机上安装我的根 CA 并尝试验证证书链时,它总是告诉我它找不到证书的颁发者.为了让事情发生,我必须安装中间 CA,这是没有意义
我看到 REST 的一大好处是依赖 HTTP 缓存。我不是在争论这个,而是完全认同这个想法。但是,我从来没有看到对中间 HTTP 缓存的更深入的解释。 如果我将 Cache-control heade
查看此图片 Facebook Messenger Android App Buttons ( MESSENGER\ACTIVE ) 我怎样才能做到这一点? 详细信息:- 带有 2px 红色边框的 di
我的任务是制作漂亮的文本,在文本中间加一条白线,如下图所示。是否可以使用 css 来实现?这是 Fiddle .container{ height:200px; width:400px;
在拉丁文字中,字母有大写和小写形式。在 Python 中,如果你想比较两个字符串而不考虑它们的大小写,你可以使用 'string'.upper() 或 'string'.lower() 将它们转换为相
我正在使用 awk 对文件进行一些文本处理。例如删除尾随空格。 awk '{gsub(/ +$/, "")} {print $0}' filename 这很好用。但是当我将输出重定向到原始文件时。它变
我是一名优秀的程序员,十分优秀!