本文整理汇总了C++中RSA_public_decrypt函数的典型用法代码示例。如果您正苦于以下问题:C++ RSA_public_decrypt函数的具体用法?C++ RSA_public_decrypt怎么用?C++ RSA_public_decrypt使用的例子?那么, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了RSA_public_decrypt函数的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: openssh_RSA_verify
static int
openssh_RSA_verify(int type, u_char *hash, u_int hashlen,
u_char *sigbuf, u_int siglen, RSA *rsa)
{
u_int ret, rsasize, oidlen = 0, hlen = 0;
int len, oidmatch, hashmatch;
const u_char *oid = NULL;
u_char *decrypted = NULL;
ret = 0;
switch (type) {
case NID_sha1:
oid = id_sha1;
oidlen = sizeof(id_sha1);
hlen = 20;
break;
case NID_md5:
oid = id_md5;
oidlen = sizeof(id_md5);
hlen = 16;
break;
default:
goto done;
}
if (hashlen != hlen) {
error("bad hashlen");
goto done;
}
rsasize = RSA_size(rsa);
if (siglen == 0 || siglen > rsasize) {
error("bad siglen");
goto done;
}
decrypted = xmalloc(rsasize);
if ((len = RSA_public_decrypt(siglen, sigbuf, decrypted, rsa,
RSA_PKCS1_PADDING)) < 0) {
error("RSA_public_decrypt failed: %s",
ERR_error_string(ERR_get_error(), NULL));
goto done;
}
if (len < 0 || (u_int)len != hlen + oidlen) {
error("bad decrypted len: %d != %d + %d", len, hlen, oidlen);
goto done;
}
oidmatch = timingsafe_bcmp(decrypted, oid, oidlen) == 0;
hashmatch = timingsafe_bcmp(decrypted + oidlen, hash, hlen) == 0;
if (!oidmatch) {
error("oid mismatch");
goto done;
}
if (!hashmatch) {
error("hash mismatch");
goto done;
}
ret = 1;
done:
if (decrypted)
xfree(decrypted);
return ret;
}
示例3: qDebug
QString AuthorizationManager::DecryptSSLString(QString encstring, QString pubkey){
//Convert from the base64 string back into byte array
QByteArray enc;
enc.append(encstring);
enc = QByteArray::fromBase64(enc);
QByteArray pkey;
pkey.append(pubkey);
pkey = QByteArray::fromBase64(pkey);
//Now start the SSL routine
/*qDebug() << "Decrypt String:" << "Length:" << enc.length() << enc;
qDebug() << " - Base64:" << encstring << "Length:" << encstring.length();
qDebug() << " - pubkey (base64):" << pubkey << "Length:" << pubkey.length();
qDebug() << " - pubkey:" << pkey << "Length:" << pkey.length();*/
unsigned char decode[4098] = {};
RSA *rsa= NULL;
BIO *keybio = NULL;
//qDebug() << " - Generate keybio";
keybio = BIO_new_mem_buf(pkey.data(), -1);
if(keybio==NULL){ return ""; }
//qDebug() << " - Read pubkey";
rsa = PEM_read_bio_RSA_PUBKEY(keybio, &rsa,NULL, NULL);
if(rsa==NULL){ qDebug() << " - Invalid RSA key!!"; return ""; }
//qDebug() << " - Decrypt string";
int len = RSA_public_decrypt(enc.length(), (unsigned char*)(enc.data()), decode, rsa, RSA_PKCS1_PADDING);
if(len<0){ return ""; }
else{ return QString( QByteArray( (char*)(decode), len) ); }
}
示例4: decode
// RSA decrypting of a block of data in ctext into ptext
// This code assumes the RSA public key is embedded in PEM format in
// the executable via simlib as 'simatra'
int decode(int clen, unsigned char *ctext, int *plen, unsigned char *ptext){
RSA *rsa_pub;
BIO *mem;
char *keydata;
int keylen;
int tmp_clen;
unsigned char tmp_ctext[KEYLEN];
// Check for valid sizes and existence of buffers
if(clen < 0 || clen > B64LEN || NULL == ctext || NULL == ptext){
return 1;
}
// Read in the RSA public key
if(get_contents_from_archive("", "simatra", &keylen, &keydata)){
return 2;
}
mem = BIO_new_mem_buf(keydata, keylen);
rsa_pub = PEM_read_bio_RSA_PUBKEY(mem, NULL, NULL, NULL);
BIO_free(mem);
if(NULL == rsa_pub){
return 3;
}
// Convert the base64 encoded license to the raw encrypted cipher text form
base64decode(clen, ctext, &tmp_clen, tmp_ctext);
// Decrypt the cipher text into plain text
*plen = RSA_public_decrypt(tmp_clen, tmp_ctext, ptext, rsa_pub, RSA_PKCS1_PADDING);
if(*plen < 1)
return 4;
return 0;
}
示例5: RSA_size
void RSACipher::decipher(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());
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_decrypt(static_cast<int>(blen), &src[offset], &dest[sumlen], rsakey.d_rsa.get(), RSA_PKCS1_PADDING);
}
else
{
len = RSA_private_decrypt(static_cast<int>(blen), &src[offset], &dest[sumlen], rsakey.d_rsa.get(), RSA_PKCS1_PADDING);
}
EXCEPTION_ASSERT_WITH_LOG(len >= 0, OpenSSLException, "RSA public decrypt failed");
sumlen += len;
}
dest.resize(sumlen);
}
示例6: malloc
std::vector<byte> DofusRSA::DofusPKeyDecrypt(std::vector<byte> signature)
{
char * DPKey =(char *) malloc(DofusPublicKey.size());
strcpy(DPKey, DofusPublicKey.c_str());
BIO *bp_dofus = BIO_new_mem_buf(DPKey, DofusPublicKey.size());
RSA *my_rsa = PEM_read_bio_RSA_PUBKEY(bp_dofus, NULL, NULL, NULL);
byte *inputSignature, *outputSignature;
inputSignature = (byte*) malloc(5000);
outputSignature = (byte*) malloc(5000);
inputSignature = reinterpret_cast<byte*> (&signature[0]);
int buflen = RSA_public_decrypt(signature.size(), inputSignature, outputSignature, my_rsa, RSA_PKCS1_PADDING);
if (buflen == -1)
{
char *buferr = new char[120];
for (int i = 0; i < 120; i++)
buferr[i] = 0;
buferr = ERR_error_string(ERR_get_error(), buferr);
qDebug() << buferr;
return signature;
}
std::vector<byte> outputSignatureVector(outputSignature, outputSignature+buflen);
return outputSignatureVector;
}
示例7: rsa_decrypt_public
/*
* rsa public decrypt
*/
char* rsa_decrypt_public(unsigned char *enc,int enc_len,char* private_key_str,int p_len,int *dec_len)
{
RSA* rsa;
int rsa_len;
char *p_de;
#if 1
// private_key = rsa_key_seliaze(private_key_str);
BIO* p_bio = BIO_new_mem_buf(private_key_str, -1);
rsa = PEM_read_bio_RSAPublicKey(p_bio, NULL, 0, NULL); //
if ( rsa == NULL ) {
printf("RSA is NULL\n");
return NULL;
}
#else
FILE* file=fopen("/tmp/r.key","r");
rsa=PEM_read_RSA_PUBKEY(file,NULL,NULL,NULL);
#endif
rsa_len=RSA_size(rsa);
p_de=(unsigned char *)calloc(rsa_len+1,1);
printf("rsa length = %d\n",rsa_len);
int rc=0;
rc = RSA_public_decrypt(rsa_len,(unsigned char *)enc,(unsigned char*)p_de,rsa,RSA_PKCS1_PADDING); //RSA_public_decrypt RSA_private_decrypt
if ( rc<=0 ) {
int e=ERR_get_error();
printf("error code is:%s\n",ERR_error_string(e,NULL));
return NULL;
}
RSA_free(rsa);
printf("plain = %s\n",p_de);
*dec_len = rc;
return p_de;
}
示例8: pkey_rsa_verifyrecover
static int pkey_rsa_verifyrecover(EVP_PKEY_CTX *ctx,
unsigned char *rout, size_t *routlen,
const unsigned char *sig, size_t siglen)
{
int ret;
RSA_PKEY_CTX *rctx = ctx->data;
if (rctx->md) {
if (rctx->pad_mode == RSA_X931_PADDING) {
if (!setup_tbuf(rctx, ctx))
return -1;
ret = RSA_public_decrypt(siglen, sig,
rctx->tbuf, ctx->pkey->pkey.rsa,
RSA_X931_PADDING);
if (ret < 1)
return 0;
ret--;
if (rctx->tbuf[ret] != RSA_X931_hash_id(EVP_MD_type(rctx->md))) {
RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER,
RSA_R_ALGORITHM_MISMATCH);
return 0;
}
if (ret != EVP_MD_size(rctx->md)) {
RSAerr(RSA_F_PKEY_RSA_VERIFYRECOVER,
RSA_R_INVALID_DIGEST_LENGTH);
return 0;
}
if (rout)
memcpy(rout, rctx->tbuf, ret);
} else if (rctx->pad_mode == RSA_PKCS1_PADDING) {
size_t sltmp;
ret = int_rsa_verify(EVP_MD_type(rctx->md),
NULL, 0, rout, &sltmp,
sig, siglen, ctx->pkey->pkey.rsa);
if (ret <= 0)
return 0;
ret = sltmp;
} else
return -1;
} else
ret = RSA_public_decrypt(siglen, sig, rout, ctx->pkey->pkey.rsa,
rctx->pad_mode);
if (ret < 0)
return ret;
*routlen = ret;
return 1;
}
示例9: pkey_rsa_verify
static int
pkey_rsa_verify(EVP_PKEY_CTX *ctx, const unsigned char *sig, size_t siglen,
const unsigned char *tbs, size_t tbslen)
{
RSA_PKEY_CTX *rctx = ctx->data;
RSA *rsa = ctx->pkey->pkey.rsa;
size_t rslen;
if (rctx->md) {
if (rctx->pad_mode == RSA_PKCS1_PADDING)
return RSA_verify(EVP_MD_type(rctx->md), tbs, tbslen,
sig, siglen, rsa);
if (rctx->pad_mode == RSA_X931_PADDING) {
if (pkey_rsa_verifyrecover(ctx, NULL, &rslen, sig,
siglen) <= 0)
return 0;
} else if (rctx->pad_mode == RSA_PKCS1_PSS_PADDING) {
int ret;
if (!setup_tbuf(rctx, ctx))
return -1;
ret = RSA_public_decrypt(siglen, sig, rctx->tbuf,
rsa, RSA_NO_PADDING);
if (ret <= 0)
return 0;
ret = RSA_verify_PKCS1_PSS_mgf1(rsa, tbs, rctx->md,
rctx->mgf1md, rctx->tbuf, rctx->saltlen);
if (ret <= 0)
return 0;
return 1;
} else
return -1;
} else {
if (!setup_tbuf(rctx, ctx))
return -1;
rslen = RSA_public_decrypt(siglen, sig, rctx->tbuf, rsa,
rctx->pad_mode);
if (rslen == 0)
return 0;
}
if (rslen != tbslen || memcmp(tbs, rctx->tbuf, rslen))
return 0;
return 1;
}
示例10: openssl_verify_data
static int openssl_verify_data(const keymaster_device_t* dev,
const void* params,
const uint8_t* keyBlob, const size_t keyBlobLength,
const uint8_t* signedData, const size_t signedDataLength,
const uint8_t* signature, const size_t signatureLength) {
if (signedData == NULL || signature == NULL) {
ALOGW("data or signature buffers == 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;
} else if (signatureLength != signedDataLength) {
ALOGW("signed data length must be signature length");
return -1;
}
Unique_RSA rsa(EVP_PKEY_get1_RSA(pkey.get()));
if (rsa.get() == NULL) {
logOpenSSLError("openssl_verify_data");
return -1;
}
UniquePtr<uint8_t> dataPtr(reinterpret_cast<uint8_t*>(malloc(signedDataLength)));
if (dataPtr.get() == NULL) {
logOpenSSLError("openssl_verify_data");
return -1;
}
unsigned char* tmp = reinterpret_cast<unsigned char*>(dataPtr.get());
if (!RSA_public_decrypt(signatureLength, signature, tmp, rsa.get(), RSA_NO_PADDING)) {
logOpenSSLError("openssl_verify_data");
return -1;
}
int result = 0;
for (size_t i = 0; i < signedDataLength; i++) {
result |= tmp[i] ^ signedData[i];
}
return result == 0 ? 0 : -1;
}
示例11: Decrypt
int Decrypt(int flen, const uint8_t* from, uint8_t* to) {
if (!rsa)
return -1;
if (ispub)
return RSA_public_decrypt(flen, from, to, rsa, RSA_PKCS1_PADDING);
return RSA_private_decrypt(flen, from, to, rsa, RSA_PKCS1_PADDING);
}
示例12: PEM_read_bio_RSA_PUBKEY
bool HttpAuth::InitPassword(const std::string & strLicense, BIO * pBio)
{
if (pBio)
{
RSA * pRsa = NULL;
PEM_read_bio_RSA_PUBKEY(pBio, &pRsa, NULL, NULL);
if (!pRsa)
{
return false;
}
std::string strDecodeLic;
CUtil::Base64DecodeBySSL(strDecodeLic, strLicense);
unsigned char szBuf[1024] = {0};
INT32 ret = RSA_public_decrypt((INT32)(strDecodeLic.size()), (const unsigned char *)strDecodeLic.data(), szBuf, pRsa, RSA_PKCS1_PADDING);
RSA_free(pRsa);
if (ret <= 0)
{
return false;
}
char szNamePwdTime[3][256];
if (sscanf((const char *)szBuf , "%[^:]:%[^:]:%s" , szNamePwdTime[0] , szNamePwdTime[1] , szNamePwdTime[2]) != 3)
{
return false;
}
time_t now = Timer::GetTime();
for (CollectionEntrysT::iterator iter = m_vecEntries.begin();iter != m_vecEntries.end();++iter)
{
if(iter->nExpireTime < now || CUtil::stricmp(iter->strName.c_str() , szNamePwdTime[0]) == 0)
{
iter = m_vecEntries.erase(iter);
}
else
{
++iter;
}
}
INT64 llExpireTime = CUtil::atoi(szNamePwdTime[2]);
if (llExpireTime == 0 || llExpireTime > now)
{
Entry entry;
entry.strName = szNamePwdTime[0];
entry.strPWD = szNamePwdTime[1];
entry.nExpireTime = llExpireTime;
m_vecEntries.push_back(entry);
return true;
}
}
return false;
}
示例13: openssh_RSA_verify
static int
openssh_RSA_verify(int hash_alg, u_char *hash, size_t hashlen,
u_char *sigbuf, size_t siglen, RSA *rsa)
{
size_t ret, rsasize = 0, oidlen = 0, hlen = 0;
int len, oidmatch, hashmatch;
const u_char *oid = NULL;
u_char *decrypted = NULL;
ret = SSH_ERR_INTERNAL_ERROR;
switch (hash_alg) {
case SSH_DIGEST_SHA1:
oid = id_sha1;
oidlen = sizeof(id_sha1);
hlen = 20;
break;
default:
goto done;
}
if (hashlen != hlen) {
ret = SSH_ERR_INVALID_ARGUMENT;
goto done;
}
rsasize = RSA_size(rsa);
if (rsasize <= 0 || rsasize > SSHBUF_MAX_BIGNUM ||
siglen == 0 || siglen > rsasize) {
ret = SSH_ERR_INVALID_ARGUMENT;
goto done;
}
if ((decrypted = malloc(rsasize)) == NULL) {
ret = SSH_ERR_ALLOC_FAIL;
goto done;
}
if ((len = RSA_public_decrypt(siglen, sigbuf, decrypted, rsa,
RSA_PKCS1_PADDING)) < 0) {
ret = SSH_ERR_LIBCRYPTO_ERROR;
goto done;
}
if (len < 0 || (size_t)len != hlen + oidlen) {
ret = SSH_ERR_INVALID_FORMAT;
goto done;
}
oidmatch = timingsafe_bcmp(decrypted, oid, oidlen) == 0;
hashmatch = timingsafe_bcmp(decrypted + oidlen, hash, hlen) == 0;
if (!oidmatch || !hashmatch) {
ret = SSH_ERR_SIGNATURE_INVALID;
goto done;
}
ret = 0;
done:
if (decrypted) {
explicit_bzero(decrypted, rsasize);
free(decrypted);
}
return ret;
}
示例14: decrypt_checksum
static void
decrypt_checksum(unsigned char *alleged_sum)
{
unsigned char raw_sum[CSUM_MAX_LEN];
memcpy(raw_sum, alleged_sum, CSUM_MAX_LEN);
memset(alleged_sum, '\0', CSUM_MAX_LEN);
RSA_public_decrypt(sizeof(raw_sum), raw_sum, alleged_sum,
signature_key, RSA_PKCS1_PADDING);
}
示例15: check_rsa
static void
check_rsa(const unsigned char *in, size_t len, RSA *rsa, int padding)
{
unsigned char *res, *res2;
int keylen;
res = malloc(RSA_size(rsa));
if (res == NULL)
errx(1, "res: ENOMEM");
res2 = malloc(RSA_size(rsa));
if (res2 == NULL)
errx(1, "res2: ENOMEM");
/* signing */
keylen = RSA_private_encrypt(len, in, res, rsa, padding);
if (keylen <= 0)
errx(1, "failed to private encrypt: %d %d", (int)len, (int)keylen);
if (keylen > RSA_size(rsa))
errx(1, "keylen > RSA_size(rsa)");
keylen = RSA_public_decrypt(keylen, res, res2, rsa, padding);
if (keylen <= 0)
errx(1, "failed to public decrypt: %d", (int)keylen);
if (keylen != len)
errx(1, "output buffer not same length: %d", (int)keylen);
if (memcmp(res2, in, len) != 0)
errx(1, "string not the same after decryption");
/* encryption */
keylen = RSA_public_encrypt(len, in, res, rsa, padding);
if (keylen <= 0)
errx(1, "failed to public encrypt: %d", (int)keylen);
if (keylen > RSA_size(rsa))
errx(1, "keylen > RSA_size(rsa)");
keylen = RSA_private_decrypt(keylen, res, res2, rsa, padding);
if (keylen <= 0)
errx(1, "failed to private decrypt: %d", (int)keylen);
if (keylen != len)
errx(1, "output buffer not same length: %d", (int)keylen);
if (memcmp(res2, in, len) != 0)
errx(1, "string not the same after decryption");
free(res);
free(res2);
}