本文整理汇总了C++中RSA_public_encrypt函数的典型用法代码示例。如果您正苦于以下问题:C++ RSA_public_encrypt函数的具体用法?C++ RSA_public_encrypt怎么用?C++ RSA_public_encrypt使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了RSA_public_encrypt函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: pkey_rsa_encrypt
static int pkey_rsa_encrypt(EVP_PKEY_CTX *ctx,
unsigned char *out, size_t *outlen,
const unsigned char *in, size_t inlen)
{
int ret;
RSA_PKEY_CTX *rctx = ctx->data;
if (rctx->pad_mode == RSA_PKCS1_OAEP_PADDING) {
int klen = RSA_size(ctx->pkey->pkey.rsa);
if (!setup_tbuf(rctx, ctx))
return -1;
if (!RSA_padding_add_PKCS1_OAEP_mgf1(rctx->tbuf, klen,
in, inlen,
rctx->oaep_label,
rctx->oaep_labellen,
rctx->md, rctx->mgf1md))
return -1;
ret = RSA_public_encrypt(klen, rctx->tbuf, out,
ctx->pkey->pkey.rsa, RSA_NO_PADDING);
} else
ret = RSA_public_encrypt(inlen, in, out, ctx->pkey->pkey.rsa,
rctx->pad_mode);
if (ret < 0)
return ret;
*outlen = ret;
return 1;
}
示例2: rsa_public_encrypt
void
rsa_public_encrypt(BIGNUM *out, BIGNUM *in, RSA *key)
{
u_char *inbuf, *outbuf;
int len, ilen, olen;
if (BN_num_bits(key->e) < 2 || !BN_is_odd(key->e))
errx(1, "rsa_public_encrypt() exponent too small or not odd");
olen = BN_num_bytes(key->n);
outbuf = (u_char*)malloc(olen);
ilen = BN_num_bytes(in);
inbuf = (u_char*)malloc(ilen);
if (outbuf == NULL || inbuf == NULL)
err(1, "malloc");
BN_bn2bin(in, inbuf);
if ((len = RSA_public_encrypt(ilen, inbuf, outbuf, key,
RSA_PKCS1_PADDING)) <= 0)
errx(1, "rsa_public_encrypt() failed");
BN_bin2bn(outbuf, len, out);
memset(outbuf, 0, olen);
memset(inbuf, 0, ilen);
free(outbuf);
free(inbuf);
}
示例3: rsa_encrypt_public
/*
* rsa public key encrypt
*/
char* rsa_encrypt_public(unsigned char*txt,int txt_len,char* public_key_str,int p_len,int* enc_len)
{
//char *public_key = "-----BEGIN RSA PUBLIC KEY-----\nMIGJAoGBAL331YpDOljAJznk4eNt0TfZJREYypIhWTN/gx0g1iUIaLPlFR7ydjaB\npd9V7G3GvvOf3mGijP+9LjKdgQ8p1pgDW7DeXZk2dTAeQ4hdY287/sw6NFKJxMXA\nFGoUdARObVespCZBdHSqo8kFMAjVGge6ZoH6nAjGzvIfijgsj+2jAgMBAAE=\n-----END RSA PUBLIC KEY-----\n";
//char * private_key = "-----BEGIN RSA PRIVATE KEY-----\nMIICXQIBAAKBgQC999WKQzpYwCc55OHjbdE32SURGMqSIVkzf4MdINYlCGiz5RUe\n8nY2gaXfVextxr7zn95hooz/vS4ynYEPKdaYA1uw3l2ZNnUwHkOIXWNvO/7MOjRS\nicTFwBRqFHQETm1XrKQmQXR0qqPJBTAI1RoHumaB+pwIxs7yH4o4LI/towIDAQAB\nAoGBAI1ALF2EI2w+ZGxdzcBntXtLUI5n2qfReBwcogcUlWYv3Hp2yb+bFV7uA8IO\nh6AQeYd4xcffL+wwZJtqFb6Ko25XAei8Os3xjb9k5fCcyrmyY+5oeXdQHlcbd/f8\niy8/rOEHZTr4iBXe/8ADlQZlRUkYCblPZ4i4BgzBUB6HzhxhAkEA8wJRx/FjOo6F\noO1aTewbvFIv4Dckqq5j/pBu9fkv1AhMxSfdGnsYcuIn15Y1/RlnpxrmJNWgryvd\n+6LJGDgjWQJBAMgfoINe80YiPCdMoboMd/u1uf1BhwujbiJPSrS40lc3jfyPmHA4\n8hppo8QuELI4rXRE/im4c+zmyphxEyULpVsCQQDnD96JGin65MeE1AsYqpdYwmEJ\ndgVkUXt88wK+2ZizqMyubpAa/M6rdgTiRc7CASUAzF/myEYIKdLh0NAbOk3JAkAE\nxEQVfPh8bipSoU+k19EvzKdOcfYef9kKtirIXTKdYzRdlKoD2kdh+6wr6xD4vcLb\n5xzKr5sLRIAE24SiOEHLAkB1TBlvvvIltttSc9lOpq3UhmtHQJaS32lD2Lk2/zNx\nW6Jbsk+sCQXM0ww4GTCpHMISfokEPtqOPikPcVFs98Oj\n-----END RSA PRIVATE KEY-----\n";
RSA* rsa;
int rsa_len;
char *p_en;
#if 1
//public_key = rsa_key_seliaze(public_key_str);
BIO* p_bio = BIO_new_mem_buf(public_key_str, -1);
printf("rsa_encrypt is %p \n",p_bio);
rsa = PEM_read_bio_RSAPublicKey(p_bio, NULL, NULL, NULL); //PEM_read_bio_RSAPrivateKey
if ( rsa == NULL ) {
printf("RSA is NULL\n");
return NULL;
}
#else
FILE* file=fopen("/tmp/r_pub.key","r");
rsa=PEM_read_RSAPrivateKey(file,NULL,NULL,NULL);//PEM_read_RSAPrivateKey
#endif
rsa_len=RSA_size(rsa);
p_en=(unsigned char *)calloc(rsa_len+1,1);
printf("rsa length = %d\n",rsa_len);
int rc=0;
if((rc=RSA_public_encrypt(txt_len,(unsigned char *)txt,(unsigned char*)p_en,rsa,RSA_PKCS1_PADDING))<=0) {
int e=ERR_get_error();
printf("error code is:%s\n",ERR_error_string(e,NULL));
return NULL;
}
printf("rsa length = %d\n",strlen(p_en));
RSA_free(rsa);
*enc_len = rc;
return p_en;
}
示例4: rsa_encrypt
array::array* rsa_encrypt(const array::array* data, RSA* rsa) {
if(data == nullptr || data == 0) {
std::cout << "(i) " << __func__ << ": empty or null data." << std::endl;
return nullptr;
}
if(rsa == nullptr || rsa == 0) {
std::cout << "(i)" << __func__ << ": empty or null key." << std::endl;
return nullptr;
}
int result;
byte* out = new byte[RSA_size(rsa)];
result = RSA_public_encrypt(data->length, data->data, out, rsa, RSA_PKCS1_PADDING);
if(result < 0) {
delete[] out;
return nullptr;
}
array::array* encrypted = array::create(result, out);
delete[] out;
return encrypted;
}
示例5: EVP_PKEY_encrypt_old
/* GMSSL: EVP_PKEY_encrypt_old() is modified */
int EVP_PKEY_encrypt_old(unsigned char *out, const unsigned char *in,
int inlen, EVP_PKEY *pkey)
{
int ret = 0;
EVP_PKEY_CTX *ctx = NULL;
size_t size;
if (pkey->type == EVP_PKEY_RSA) {
ret = RSA_public_encrypt(inlen, in, out, pkey->pkey.rsa,
RSA_PKCS1_PADDING);
} else {
if (!(ctx = EVP_PKEY_CTX_new(pkey, NULL))) {
return 0;
}
if (1 != EVP_PKEY_encrypt_init(ctx)) {
return 0;
}
if (1 != EVP_PKEY_encrypt(ctx, out, &size, in, inlen)) {
goto end;
}
ret = (int)size;
}
end:
EVP_PKEY_CTX_free(ctx);
return ret;
}
示例6: RSAEncrypt
/**
* 功能描述:RSA加密
* @param pData:原始数据
* @param ilen: 原始数据长度
* @param pEncodeData: 加密后数据
* @return -1: 失败, 其他: 加密数据长度
**/
int RSAEncrypt(const char* pData , int iLen, char** pEncodeData)
{
char chPublicKey[] = "-----BEGIN PUBLIC KEY-----\n"
"MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQCb/vAGucNg3OJyBV6/aWEd7IK9"
"46GYnOT089mDzNY2zDBB9hPWwdSUYOTbDROlc3Gd4eOudeQqlnAgHB7zqwVGqWuG"
"vbqHWSSPpp6pMilpVVz9SMbL/1BgfhK+dKWIDYHJDRJFpBLFUpe0vq8n+8Mdgp1z"
"NPH3cR+rWK8zI5xF5wIDAQAB"
"\n-----END PUBLIC KEY-----\n";
BIO *bio = BIO_new_mem_buf(chPublicKey, -1);
if (!bio)
{
return -1;
}
RSA* rsa = PEM_read_bio_RSA_PUBKEY(bio, NULL, NULL, NULL);
if (!rsa)
{
return -1;
}
int nLen = RSA_size(rsa);
char* pEncode = (char*)malloc(nLen + 1);
int rc = RSA_public_encrypt(iLen, (const unsigned char*)pData, (unsigned char*)pEncode, rsa, RSA_PKCS1_PADDING);
*pEncodeData = pEncode;
RSA_free(rsa);
CRYPTO_cleanup_all_ex_data();
return rc;
}
示例7: KBE_ASSERT
//-------------------------------------------------------------------------------------
int KBE_RSA::encrypt(const std::string& instr, std::string& outCertifdata)
{
KBE_ASSERT(rsa_public != NULL);
unsigned char* certifdata =(unsigned char*)calloc(RSA_size(static_cast<RSA*>(rsa_public)) + 1, sizeof(unsigned char));
int certifsize = RSA_public_encrypt(instr.size(),
(unsigned char*)instr.c_str(), certifdata, static_cast<RSA*>(rsa_public), RSA_PKCS1_OAEP_PADDING);
if (certifsize < 0)
{
ERR_load_crypto_strings();
char err[1024];
char* errret = ERR_error_string(ERR_get_error(), err);
ERROR_MSG(fmt::format("KBE_RSA::encrypt: RSA_public_encrypt error({} : {})\n",
errret, err));
free(certifdata);
return certifsize;
}
outCertifdata.assign((const char*)certifdata, certifsize);
free(certifdata);
return certifsize;
}
示例8: while
//公钥加密会话密钥:
bool CRsaDesManager::RSAPubEncode(ui16 wKeyIdx, unsigned char *pcInData, unsigned long ulInLen, unsigned char *pcOutData, unsigned long *ulOutLen)
{
if(!CheckRSAKeyIdx(wKeyIdx))
return false;
*ulOutLen = 0;
unsigned long ulInTmpLen = std::min<int>(ulInLen, RSA_ENCODE_LEN_UNIT);
unsigned long ulOutTmpLen = 0;
while(ulInLen)
{
ulOutTmpLen = RSA_public_encrypt(ulInTmpLen, pcInData, pcOutData+(*ulOutLen), m_pPubRSA[wKeyIdx], 1);
if(ulOutTmpLen <= 0)
break;
*ulOutLen = (*ulOutLen) + ulOutTmpLen;
pcInData = pcInData+ulInTmpLen;
ulInLen -= ulInTmpLen;
ulInTmpLen = std::min<int>(ulInLen, RSA_ENCODE_LEN_UNIT);
}
if(ulInLen <= 0)
return true;
else
return false;
}
示例9: psRsaEncryptPub
int32_t psRsaEncryptPub(psPool_t *pool, psRsaKey_t *key,
const unsigned char *in, psSize_t inlen,
unsigned char *out, psSize_t outlen,
void *data)
{
return RSA_public_encrypt(inlen, in, out, *key, RSA_PKCS1_PADDING);
}
示例10: printf
ZBuffer& RSAZCryptor::publickey_encrypt(int in_len, unsigned char* in, ZBuffer& result)
{
if(in_len <= 0)
{
if(m_trace_level > 0)
printf("Warning: input to publickey_encrypt is empty\n");
return result;
}
ZBuffer onebuf(pub_size);
int onelen = 0;
int inc = pub_size - 42;
int resultlen = (in_len/inc + (((in_len%inc) == 0)?0:1)) * pub_size;
result.reserve(resultlen);
int curpos = 0;
unsigned char* resultptr = result.buffer();
while(curpos < in_len)
{
int cur_inc = (in_len - curpos > inc)?inc:(in_len - curpos);
onelen = RSA_public_encrypt(cur_inc, in + curpos, onebuf.buffer(), pub_rsa, RSA_PKCS1_OAEP_PADDING);
if(onelen < 0)
{
throw_error();
}
memcpy(resultptr, onebuf.buffer(), onelen);
curpos += cur_inc;
resultptr += onelen;
}
return result;
}
示例11: rsa_encrypt
//使用公钥加密
int rsa_encrypt(char *in, const char *key_path, char* out)
{
RSA *p_rsa;
FILE *file;
int rsa_len;
if ((file=fopen(key_path, "r"))==NULL)
{
perror("open key file error");
return 0;
}
//if ((p_rsa=PEM_read_RSA_PUBKEY(file, NULL, &ccbb, NULL))==NULL){
if ((p_rsa=PEM_read_RSAPublicKey(file, NULL, NULL, NULL))==NULL){
ERR_print_errors_fp(stdout);
return 0;
}
rsa_len=RSA_size(p_rsa);
if (RSA_public_encrypt(rsa_len, (unsigned char*)in, (unsigned char*)out, p_rsa, RSA_NO_PADDING)<0)
{
return 0;
}
RSA_free(p_rsa);
fclose(file);
return 1;
}
示例12: __ops_rsa_public_encrypt
/**
\ingroup Core_Crypto
\brief RSA-encrypts data
\param out Where to write the encrypted data
\param in Plaintext
\param length Size of plaintext
\param pubkey RSA Public Key
*/
int
__ops_rsa_public_encrypt(uint8_t *out,
const uint8_t *in,
size_t length,
const __ops_rsa_pubkey_t *pubkey)
{
RSA *orsa;
int n;
/* printf("__ops_rsa_public_encrypt: length=%ld\n", length); */
orsa = RSA_new();
orsa->n = pubkey->n;
orsa->e = pubkey->e;
/* printf("len: %ld\n", length); */
/* __ops_print_bn("n: ", orsa->n); */
/* __ops_print_bn("e: ", orsa->e); */
n = RSA_public_encrypt((int)length, in, out, orsa, RSA_NO_PADDING);
if (n == -1) {
BIO *fd_out;
fd_out = BIO_new_fd(fileno(stderr), BIO_NOCLOSE);
ERR_print_errors(fd_out);
}
orsa->n = orsa->e = NULL;
RSA_free(orsa);
return n;
}
示例13: rsa_encrypt
static int rsa_encrypt(u_int8_t *text, int len, u_int8_t *res)
{
RSA *rsa;
u_int8_t modules[256];
u_int8_t exponent[8];
int size;
char n[] =
"59dE8qLieItsH1WgjrcFRKj6eUWqi+bGLOX1HL3U3GhC/j0Qg90u3sG/1CUtwC"
"5vOYvfDmFI6oSFXi5ELabWJmT2dKHzBJKa3k9ok+8t9ucRqMd6DZHJ2YCCLlDR"
"KSKv6kDqnw4UwPdpOMXziC/AMj3Z/lUVX1G7WSHCAWKf1zNS1eLvqr+boEjXuB"
"OitnZ/bDzPHrTOZz0Dew0uowxf/+sG+NCK3eQJVxqcaJ/vEHKIVd2M+5qL71yJ"
"Q+87X6oV3eaYvt3zWZYD6z5vYTcrtij2VZ9Zmni/UAaHqn9JdsBWLUEpVviYnh"
"imNVvYFZeCXg/IdTQ+x4IRdiXNv5hEew==";
char e[] = "AQAB";
rsa=RSA_new();
size=base64_decode(n,modules);
rsa->n=BN_bin2bn(modules,size,NULL);
size=base64_decode(e,exponent);
rsa->e=BN_bin2bn(exponent,size,NULL);
size=RSA_public_encrypt(len, text, res, rsa, RSA_PKCS1_OAEP_PADDING);
RSA_free(rsa);
return size;
}
示例14: ops_rsa_public_encrypt
/**
\ingroup Core_Crypto
\brief RSA-encrypts data
\param out Where to write the encrypted data
\param in Plaintext
\param length Size of plaintext
\param rsa RSA Public Key
*/
int ops_rsa_public_encrypt(unsigned char *out,const unsigned char *in,
size_t length,const ops_rsa_public_key_t *rsa)
{
RSA *orsa;
int n;
// printf("ops_rsa_public_encrypt: length=%ld\n", length);
orsa=RSA_new();
orsa->n=rsa->n;
orsa->e=rsa->e;
// printf("len: %ld\n", length);
// ops_print_bn("n: ", orsa->n);
// ops_print_bn("e: ", orsa->e);
n=RSA_public_encrypt(length,in,out,orsa,RSA_NO_PADDING);
if (n==-1)
{
BIO *fd_out;
fd_out=BIO_new_fd(fileno(stderr), BIO_NOCLOSE);
ERR_print_errors(fd_out);
}
orsa->n=orsa->e=NULL;
RSA_free(orsa);
return n;
}
示例15: RSA_size
void RSACipher::cipher(const std::vector<unsigned char>& src, std::vector<unsigned char>& dest, const AsymmetricKey& key, KeyCompound key_compound)
{
const RSAKey& rsakey = static_cast<const RSAKey&>(key);
int len = 0;
size_t sumlen = 0;
size_t blen = RSA_size(rsakey.d_rsa.get()) - 11;
size_t c = (src.size() / blen) + 1;
dest.resize(RSA_size(rsakey.d_rsa.get()) * c);
for (size_t offset = 0; offset < src.size(); offset += blen)
{
if (blen + offset > src.size())
{
blen = src.size() - offset;
}
if (key_compound == KC_PUBLIC)
{
len = RSA_public_encrypt(static_cast<int>(blen), &src[offset], &dest[sumlen], rsakey.d_rsa.get(), RSA_PKCS1_PADDING);
}
else
{
len = RSA_private_encrypt(static_cast<int>(blen), &src[offset], &dest[sumlen], rsakey.d_rsa.get(), RSA_PKCS1_PADDING);
}
EXCEPTION_ASSERT_WITH_LOG(len >= 0, OpenSSLException, "RSA public encrypt failed");
sumlen += len;
}
dest.resize(sumlen);
}