本文整理汇总了C++中RSA_private_encrypt函数的典型用法代码示例。如果您正苦于以下问题:C++ RSA_private_encrypt函数的具体用法?C++ RSA_private_encrypt怎么用?C++ RSA_private_encrypt使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了RSA_private_encrypt函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: RsaPssVerification
//Hardcoding this test code for PSS-SHA1
void RsaPssVerification(unsigned int uKeySize, const unsigned char* pMsg, const unsigned char* pSign,
unsigned int E, const unsigned char* pN, const unsigned char* pD)
{
RSA* pRsaKey = NULL;
const unsigned char* pDigest = NULL;
size_t uDigestLen = 20;
unsigned char EM[512];
unsigned char signature[512];
unsigned int uLen = 0;
int status = 0;
// Generate an RSA key pair
pRsaKey = GetRsaKey(E, pN, pD);
if (pRsaKey) {
//Use the already hashed input message and compute the PSS padded data with max salt size
pDigest = pMsg;
printbin("HASH", pDigest, 20);
status = RSA_padding_add_PKCS1_PSS(pRsaKey, EM, pDigest, EVP_sha1(), -2);
printbin("EM", EM, uKeySize);
if (status == 1) {
//Now do Rsa Signature (RSA private encrypt)
status = RSA_private_encrypt(uKeySize, EM, signature, pRsaKey, RSA_NO_PADDING);
printbin("Sign", signature, uKeySize);
if (status != -1) {
//Now its time to verify the signature using RSA public decryption
//We could directly use signature, but we are here to verify the signature generated by HW KM1
uLen = hex2bin(signature, pSign);
//assert(uLen == uKeySize)
printbin("Sign", signature, uLen);
status = RSA_public_decrypt(uKeySize, signature, EM, pRsaKey, RSA_NO_PADDING);
printbin("EM", EM, uKeySize);
if (status != -1) {
//Verify the data against the message with expecting max salt length from ssignature
status = RSA_verify_PKCS1_PSS(pRsaKey, pDigest, EVP_sha1(), EM, -2);
if (status == 1) {
printf("GREAT: Signature verification successful\n");
} else {
printf("RSA_verify_PKCS1_PSS failed with error %s\n", ERR_error_string(ERR_get_error(), NULL));
}
} else {
printf("RSA_public_decrypt failed with error %s\n", ERR_error_string(ERR_get_error(), NULL));
}
} else {
printf("RSA_private_encrypt failed with error %s\n", ERR_error_string(ERR_get_error(), NULL));
}
} else {
printf("RSA_padding_add_PKCS1_PSS failed with error %s\n", ERR_error_string(ERR_get_error(), NULL));
}
}
if (pRsaKey) {
RSA_free(pRsaKey);
}
}
示例2: pkey_rsa_sign
static int
pkey_rsa_sign(EVP_PKEY_CTX *ctx, unsigned char *sig, size_t *siglen,
const unsigned char *tbs, size_t tbslen)
{
int ret;
RSA_PKEY_CTX *rctx = ctx->data;
RSA *rsa = ctx->pkey->pkey.rsa;
if (rctx->md) {
if (tbslen != (size_t)EVP_MD_size(rctx->md)) {
RSAerr(RSA_F_PKEY_RSA_SIGN,
RSA_R_INVALID_DIGEST_LENGTH);
return -1;
}
if (EVP_MD_type(rctx->md) == NID_mdc2) {
unsigned int sltmp;
if (rctx->pad_mode != RSA_PKCS1_PADDING)
return -1;
ret = RSA_sign_ASN1_OCTET_STRING(NID_mdc2, tbs, tbslen,
sig, &sltmp, rsa);
if (ret <= 0)
return ret;
ret = sltmp;
} else if (rctx->pad_mode == RSA_X931_PADDING) {
if (!setup_tbuf(rctx, ctx))
return -1;
memcpy(rctx->tbuf, tbs, tbslen);
rctx->tbuf[tbslen] =
RSA_X931_hash_id(EVP_MD_type(rctx->md));
ret = RSA_private_encrypt(tbslen + 1, rctx->tbuf, sig,
rsa, RSA_X931_PADDING);
} else if (rctx->pad_mode == RSA_PKCS1_PADDING) {
unsigned int sltmp;
ret = RSA_sign(EVP_MD_type(rctx->md), tbs, tbslen, sig,
&sltmp, rsa);
if (ret <= 0)
return ret;
ret = sltmp;
} else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) {
if (!setup_tbuf(rctx, ctx))
return -1;
if (!RSA_padding_add_PKCS1_PSS_mgf1(rsa, rctx->tbuf,
tbs, rctx->md, rctx->mgf1md, rctx->saltlen))
return -1;
ret = RSA_private_encrypt(RSA_size(rsa), rctx->tbuf,
sig, rsa, RSA_NO_PADDING);
} else
return -1;
} else
ret = RSA_private_encrypt(tbslen, tbs, sig, ctx->pkey->pkey.rsa,
rctx->pad_mode);
if (ret < 0)
return ret;
*siglen = ret;
return 1;
}
示例3: rsa_encrypt_private
/*
* rsa private encrypt
*/
char* rsa_encrypt_private(unsigned char*txt,int txt_len,char* public_key_str,int p_len,int* enc_len)
{
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_RSAPrivateKey(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_private_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;
}
RSA_free(rsa);
*enc_len = rc;
return p_en;
}
示例4: sign_rsa
static int sign_rsa(EVP_PKEY* pkey, keymaster_rsa_sign_params_t* sign_params, const uint8_t* data,
const size_t dataLength, uint8_t** signedData, size_t* signedDataLength) {
if (sign_params->digest_type != DIGEST_NONE) {
ALOGW("Cannot handle digest type %d", sign_params->digest_type);
return -1;
} else if (sign_params->padding_type != PADDING_NONE) {
ALOGW("Cannot handle padding type %d", sign_params->padding_type);
return -1;
}
Unique_RSA rsa(EVP_PKEY_get1_RSA(pkey));
if (rsa.get() == NULL) {
logOpenSSLError("openssl_sign_rsa");
return -1;
}
UniquePtr<uint8_t, Malloc_Free> signedDataPtr(reinterpret_cast<uint8_t*>(malloc(dataLength)));
if (signedDataPtr.get() == NULL) {
logOpenSSLError("openssl_sign_rsa");
return -1;
}
unsigned char* tmp = reinterpret_cast<unsigned char*>(signedDataPtr.get());
if (RSA_private_encrypt(dataLength, data, tmp, rsa.get(), RSA_NO_PADDING) <= 0) {
logOpenSSLError("openssl_sign_rsa");
return -1;
}
*signedDataLength = dataLength;
*signedData = signedDataPtr.release();
return 0;
}
示例5: generate_certificate
/** Generate a new certificate for our loaded or generated keys, and write it
* to disk. Return 0 on success, nonzero on failure. */
static int
generate_certificate(void)
{
char buf[8192];
time_t now = time(NULL);
struct tm tm;
char published[ISO_TIME_LEN+1];
char expires[ISO_TIME_LEN+1];
char fingerprint[FINGERPRINT_LEN+1];
char *ident = key_to_string(identity_key);
char *signing = key_to_string(signing_key);
FILE *f;
size_t signed_len;
char digest[DIGEST_LEN];
char signature[1024]; /* handles up to 8192-bit keys. */
int r;
get_fingerprint(identity_key, fingerprint);
tor_localtime_r(&now, &tm);
tm.tm_mon += months_lifetime;
format_iso_time(published, now);
format_iso_time(expires, mktime(&tm));
tor_snprintf(buf, sizeof(buf),
"dir-key-certificate-version 3"
"%s%s"
"\nfingerprint %s\n"
"dir-key-published %s\n"
"dir-key-expires %s\n"
"dir-identity-key\n%s"
"dir-signing-key\n%s"
"dir-key-certification\n",
address?"\ndir-address ":"", address?address:"",
fingerprint, published, expires, ident, signing);
tor_free(ident);
tor_free(signing);
signed_len = strlen(buf);
SHA1((const unsigned char*)buf,signed_len,(unsigned char*)digest);
r = RSA_private_encrypt(DIGEST_LEN, (unsigned char*)digest,
(unsigned char*)signature,
EVP_PKEY_get1_RSA(identity_key),
RSA_PKCS1_PADDING);
strlcat(buf, "-----BEGIN SIGNATURE-----\n", sizeof(buf));
signed_len = strlen(buf);
base64_encode(buf+signed_len, sizeof(buf)-signed_len, signature, r);
strlcat(buf, "-----END SIGNATURE-----\n", sizeof(buf));
if (!(f = fopen(certificate_file, "w"))) {
log_err(LD_GENERAL, "Couldn't open %s for writing: %s",
certificate_file, strerror(errno));
return 1;
}
fputs(buf, f);
fclose(f);
return 0;
}
示例6: RSATest
void RSATest()
{
RSA *key = NULL;
BIO *bio_sec = NULL;
BIO *bio_pub = NULL;
FILE *fp_sec = NULL;
FILE *fp_pub = NULL;
fopen_s(&fp_sec, "sec.pem", "r");
fopen_s(&fp_pub, "pub.pem", "r");
bio_sec = BIO_new_fp(fp_sec, BIO_CLOSE);
bio_pub = BIO_new_fp(fp_pub, BIO_CLOSE);
if (!bio_sec || !bio_pub) {
printf("fucked BIO_new_file\n");
return;
}
if (!PEM_read_bio_RSAPrivateKey(bio_sec, &key, NULL, "")) {
printf("fucked PEM_read_bio_RSAPrivateKey\n");
return;
}
if (!PEM_read_bio_RSAPublicKey(bio_pub, &key, NULL, "")) {
printf("fucked PEM_read_bio_RSAPublicKey\n");
return;
}
int len = RSA_size(key);
unsigned char *buf = new unsigned char[len];
memset(buf, 0, len);
if (-1 == RSA_private_encrypt(strlen(RSA_TEXT), (const unsigned char *)RSA_TEXT, buf, key, RSA_PKCS1_PADDING) ) {
printf("fucked RSA_private_encrypt\n");
return;
}
OutputHexString(buf, len);
printf("\n");
/*
unsigned char *buf_test = new unsigned char[len];
memset(buf_test, 0, len);
if (-1 == RSA_public_decrypt(len, buf, buf_test, key, RSA_PKCS1_PADDING) ) {
printf("fucked RSA_public_decrypt\n");
return;
}
OutputHexString(buf_test, strlen(RSA_TEXT));
printf("\n");
delete [] buf_test;
*/
delete [] buf;
BIO_free(bio_sec);
}
示例7: 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);
}
示例8: int32
bool RSAipher::Encrypt(const std::string& in, std::string& out)
{
if(!IsValid())
return false;
int32 nLeft = int32(in.length());
const unsigned char* pIn = (const unsigned char *)in.c_str();
do
{
memset( m_ptr0, 0, m_nRSALen);
memset( m_ptr1, 0, m_nRSALen);
const int32 inLen = nLeft > m_nRSALen - RSA_PKCS1_PADDING_SIZE ? m_nRSALen - RSA_PKCS1_PADDING_SIZE : nLeft;
memcpy( m_ptr0, pIn, inLen);
int32 nRetLen = IsPubKey() ?
RSA_public_encrypt( inLen, m_ptr0, m_ptr1, m_pKey, RSA_PKCS1_PADDING) :
RSA_private_encrypt( inLen, m_ptr0, m_ptr1, m_pKey, RSA_PKCS1_PADDING);
if( nRetLen < 0 )
break;
nLeft -= inLen;
pIn += inLen;
out += inLen;
} while ( nLeft > 0);
return nLeft <= 0;
}
示例9: ops_rsa_private_encrypt
/**
\ingroup Core_Crypto
\brief Signs data with RSA
\param out Where to write signature
\param in Data to sign
\param length Length of data
\param srsa RSA secret key
\param rsa RSA public key
\return number of bytes decrypted
*/
int ops_rsa_private_encrypt(unsigned char *out,const unsigned char *in,
size_t length,const ops_rsa_secret_key_t *srsa,
const ops_rsa_public_key_t *rsa)
{
RSA *orsa;
int n;
orsa=RSA_new();
orsa->n=rsa->n; // XXX: do we need n?
orsa->d=srsa->d;
orsa->p=srsa->q;
orsa->q=srsa->p;
/* debug */
orsa->e=rsa->e;
// If this isn't set, it's very likely that the programmer hasn't
// decrypted the secret key. RSA_check_key segfaults in that case.
// Use ops_decrypt_secret_key_from_data() to do that.
assert(orsa->d);
assert(RSA_check_key(orsa) == 1);
orsa->e=NULL;
/* end debug */
n=RSA_private_encrypt(length,in,out,orsa,RSA_NO_PADDING);
orsa->n=orsa->d=orsa->p=orsa->q=NULL;
RSA_free(orsa);
return n;
}
示例10: psRsaEncryptPriv
int32_t psRsaEncryptPriv(psPool_t *pool, psRsaKey_t *key,
const unsigned char *in, psSize_t inlen,
unsigned char *out, psSize_t outlen,
void *data)
{
return RSA_private_encrypt(inlen, in, out, *key, RSA_PKCS1_PADDING);
}
示例11: keyfile
QString AuthorizationManager::GenerateEncString_bridge(QString str){
//Get the private key
QFile keyfile("/usr/local/etc/sysadm/ws_bridge.key");
keyfile.open(QIODevice::ReadOnly);
QSslKey key(&keyfile, QSsl::Rsa);
QByteArray privkey = key.toPem();
keyfile.close();
//Now use this private key to encode the given string
unsigned char encode[4098] = {};
RSA *rsa= NULL;
BIO *keybio = NULL;
keybio = BIO_new_mem_buf(privkey.data(), -1);
if(keybio==NULL){ return ""; }
rsa = PEM_read_bio_RSAPrivateKey(keybio, &rsa,NULL, NULL);
if(rsa==NULL){ return ""; }
int len = RSA_private_encrypt(str.length(), (unsigned char*)(str.toLatin1().data()), encode, rsa, RSA_PKCS1_PADDING);
if(len <0){ return ""; }
else{
//Now return this as a base64 encoded string
QByteArray str_encode( (char*)(encode), len);
/*qDebug() << "Encoded String Info";
qDebug() << " - Raw string:" << str << "Length:" << str.length();
qDebug() << " - Encoded string:" << str_encode << "Length:" << str_encode.length();*/
str_encode = str_encode.toBase64();
/*qDebug() << " - Enc string (base64):" << str_encode << "Length:" << str_encode.length();
qDebug() << " - Enc string (QString):" << QString(str_encode);*/
return QString( str_encode );
}
}
示例12: openssl_sign_data
static int openssl_sign_data(const keymaster_device_t* dev,
const void* params,
const uint8_t* keyBlob, const size_t keyBlobLength,
const uint8_t* data, const size_t dataLength,
uint8_t** signedData, size_t* signedDataLength) {
int result = -1;
EVP_MD_CTX ctx;
size_t maxSize;
if (data == NULL) {
ALOGW("input data to sign == NULL");
return -1;
} else if (signedData == NULL || signedDataLength == NULL) {
ALOGW("output signature buffer == NULL");
return -1;
}
Unique_EVP_PKEY pkey(unwrap_key(keyBlob, keyBlobLength));
if (pkey.get() == NULL) {
return -1;
}
if (EVP_PKEY_type(pkey->type) != EVP_PKEY_RSA) {
ALOGW("Cannot handle non-RSA keys yet");
return -1;
}
keymaster_rsa_sign_params_t* sign_params = (keymaster_rsa_sign_params_t*) params;
if (sign_params->digest_type != DIGEST_NONE) {
ALOGW("Cannot handle digest type %d", sign_params->digest_type);
return -1;
} else if (sign_params->padding_type != PADDING_NONE) {
ALOGW("Cannot handle padding type %d", sign_params->padding_type);
return -1;
}
Unique_RSA rsa(EVP_PKEY_get1_RSA(pkey.get()));
if (rsa.get() == NULL) {
logOpenSSLError("openssl_sign_data");
return -1;
}
UniquePtr<uint8_t> signedDataPtr(reinterpret_cast<uint8_t*>(malloc(dataLength)));
if (signedDataPtr.get() == NULL) {
logOpenSSLError("openssl_sign_data");
return -1;
}
unsigned char* tmp = reinterpret_cast<unsigned char*>(signedDataPtr.get());
if (RSA_private_encrypt(dataLength, data, tmp, rsa.get(), RSA_NO_PADDING) <= 0) {
logOpenSSLError("openssl_sign_data");
return -1;
}
*signedDataLength = dataLength;
*signedData = signedDataPtr.release();
return 0;
}
示例13: Encode_RSA
void Encode_RSA(unsigned char *in,int fd)
{
RSA *r;
BIGNUM *bne,*bnn,*bnd;
int bits = 1024, ret, len, flen, padding, i;
unsigned char *key, *p;
BIO *b;
unsigned char *encData,*decData,*tmpData;//加密后的数据/解密后的数据/临时指针
/* Key data */
unsigned long e = 75011;
/* 构建RSA数据结构 */
bne = BN_new();
bnd = BN_new();
bnn = BN_new();
ret = BN_set_word(bne, e);
BN_hex2bn(&bnd, PRIVATE);
BN_hex2bn(&bnn, MODULUS);
r = RSA_new();
r->e=bne;
r->d=bnd;
r->n=bnn;
/* Debug output key */
/*RSA_print_fp(stdout, r, 5);*/
/*准备输出的加密数据结构 */
flen = RSA_size(r);// - 11;
encData = (unsigned char *)malloc(flen);
bzero(encData, flen);//memset(encData, 0, flen);
ret = RSA_private_encrypt(flen, in, encData, r, RSA_NO_PADDING);
if(ret < 0){
JCG("Encrypt failed!\n");
return;
}
write(fd,encData,flen);
tmpData=encData;
#if 0
for (i=0; i<ret; i++)
{
JDG("0x%02x, ", *tmpData);
if(i%16 == 7)
JDG("\t");
else if(i%16 == 15)
JDG("\n");
tmpData++;
}
JDG("\n");
JCG("end private encrypt ");
#endif
free(encData);
RSA_free(r);
}
示例14: Encrypt
int Encrypt(int flen, const uint8_t* from, uint8_t* to) {
if (!rsa)
return -1;
if (ispub)
return RSA_public_encrypt(flen, from, to, rsa, RSA_PKCS1_PADDING);
return RSA_private_encrypt(flen, from, to, rsa, RSA_PKCS1_PADDING);
}
示例15: encrypt
int encrypt(int flen, unsigned char* from, unsigned char* to, rsa_padding padding) {
assert(from && to);
int pad = this->rsa_padding2int(padding);
if (this->is_pub) {
return RSA_public_encrypt(flen, from, to, this->rsa_handle.get(), pad);
}
else {
return RSA_private_encrypt(flen, from, to, this->rsa_handle.get(), pad);
}
}