简体   繁体   English

C(或通过外壳)中的OpenSSL“密封”

[英]OpenSSL “Seal” in C (or via shell)

I'm working on porting some PHP code to C, that contacts a web API. 我正在将一些PHP代码移植到与Web API联系的C语言上。

The issue I've come across is that the PHP code uses the function openssl_seal() , but I can't seem to find any way to do the same thing in C or even via openssl in a call to system() . 我遇到的问题是PHP代码使用函数openssl_seal() ,但是我似乎找不到任何方法可以在C中甚至通过调用system() openssl来完成相同的操作。

From the PHP manual on openssl_seal() : openssl_seal()的PHP手册中:

int openssl_seal ( string $data , string &$sealed_data , array &$env_keys , array $pub_key_ids ) int openssl_seal(字符串$ data,字符串&$ sealed_data,数组&$ env_keys,数组$ pub_key_ids)

openssl_seal() seals (encrypts) data by using RC4 with a randomly generated secret key. openssl_seal()通过将RC4与随机生成的密钥一起使用来密封(加密)数据。 The key is encrypted with each of the public keys associated with the identifiers in pub_key_ids and each encrypted key is returned in env_keys . 使用与pub_key_ids中的标识符关联的每个公共密钥对密钥进行加密,并在env_keys中返回每个加密的密钥。 This means that one can send sealed data to multiple recipients (provided one has obtained their public keys). 这意味着一个人可以将密封的数据发送给多个收件人(前提是已经获得了他们的公钥)。 Each recipient must receive both the sealed data and the envelope key that was encrypted with the recipient's public key. 每个收件人都必须同时接收密封的数据和使用收件人的公共密钥加密的信封密钥。

What would be the best way to implement this? 实施此方法的最佳方法是什么? I'd really prefer not to call out to a PHP script every time, for obvious reasons. 出于明显的原因,我真的希望每次都调用PHP脚本。

You are after the EVP ("Envelope Encryption") part of the C interface to the OpenSSL library: 您在C接口的OpenSSL库的EVP(“信封加密”)部分之后:

#include <openssl/evp.h>

int EVP_SealInit(EVP_CIPHER_CTX *ctx, const EVP_CIPHER *type,
                 unsigned char **ek, int *ekl, unsigned char *iv,
                 EVP_PKEY **pubk, int npubk);
int EVP_SealUpdate(EVP_CIPHER_CTX *ctx, unsigned char *out,
        int *outl, unsigned char *in, int inl);
int EVP_SealFinal(EVP_CIPHER_CTX *ctx, unsigned char *out,
        int *outl);

(In this case, since you want RC4 for compatibility with the PHP code, you'd use EVP_rc4() as the type parameter to EVP_SealInit() ). (在这种情况下,既然你想RC4与PHP代码的兼容性,你会使用EVP_rc4()作为type参数EVP_SealInit()

如果您被允许使用C ++而不仅是C,那么您可以使用Crypto ++ ,它将轻松地完成您需要的操作。

仅当您精通c ++时,才考虑使用Crypto ++。

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

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