最近搞了下openssl 的加密,做个mark,csdn这破货用不起了
rsa生成密钥对
void ConvertToArray(ostream& stream, const string& name, unsigned char* buf, size_t size)
{
stream << "const unsigned char " << name << "[" << size << "] = {" << endl;
for (size_t i = 0; i < size; i++)
{
if (i != 0)
stream << ",";
stream << (int)buf[i];
}
stream << endl << "};" << endl << endl;
}
void ConvertToArray(ostream& stream, const string &name, RSA* key, bool isPrivate)
{
int size = 0;
if (isPrivate)
size = i2d_RSAPrivateKey(key, NULL);
else
size = i2d_RSAPublicKey(key, NULL);
if (size < 0)
{
cout << "SaveRsKey error" << endl;
return;
}
unsigned char* prkey = new unsigned char[size + 20];
unsigned char *p = prkey;
if (isPrivate)
size = i2d_RSAPrivateKey(key, &p);
else
size = i2d_RSAPublicKey(key, &p);
if (size < 0)
{
cout << "SaveRsKey error" << endl;
}
else
{
ConvertToArray(stream, name, prkey, size);
}
delete[] prkey;
}
void SaveRSAKey(const string& fileName, RSA* key, bool isPrivate )
{
int size = 0;
if (isPrivate)
size = i2d_RSAPrivateKey(key, NULL);
else
size = i2d_RSAPublicKey(key, NULL);
if (size < 0)
{
cout << "SaveRsKey error" << endl;
return;
}
unsigned char* prkey = new unsigned char[size + 20];
unsigned char *p = prkey;
if (isPrivate)
size = i2d_RSAPrivateKey(key, &p);
else
size = i2d_RSAPublicKey(key, &p);
if (size < 0)
{
cout << "SaveRsKey error" << endl;
}
else
{
ofstream f(fileName.c_str(), ios_base::out | ios_base::binary);
if (!f)
{
cout << "SaveRsKey error" << endl;
}
else
{
//ConvertToArray()
f.write((char*)prkey, size);
f.close();
}
}
delete[] prkey;
}
void GenKeys()
{
ostringstream keys;
ofstream fpbtext("f:\\pbkeys.txt", ios_base::out);
ofstream fprtext("f:\\prkeys.txt", ios_base::out);
for (int i = 0; i < 10; i++)
{
ostringstream ps, ks, pn, kn;
ps << "f:\\key" << i << ".pbk";
pn << "pbk" << i;
ks << "f:\\key" << i << ".prk";
kn << "prk" << i;
RSA *key = RSA_generate_key(512, RSA_F4, NULL, NULL);
//SaveRSAKey(ps.str(), key, false);
//SaveRSAKey(ks.str(), key, true);
ConvertToArray(fpbtext, pn.str(), key, false);
ConvertToArray(fprtext, kn.str(), key, true);
RSA_free(key);
}
}
rsa加密和解密
unsigned char* prk = &prk0[0];
key = d2i_RSAPrivateKey(NULL, (const unsigned char**)&prk, sizeof(prk0));
//key = RSA_generate_key(512, RSA_F4, NULL, NULL);
plen = sizeof(ptext_ex) - 1;
num = RSA_private_encrypt(plen, ptext_ex, ctext, key,
RSA_PKCS1_PADDING);
if (num != RSA_size(key)) //模数长度
{
printf("%d PKCS#1 v1.5 encryption failed:%s!\n", RSA_size(key), ERR_reason_error_string(ERR_get_error()));
err=1;
goto next;
}
//加密后的数据
printf("encrypted text: \n");
for (i = 0; i < num; i++)
{
printf("\\x%02x", ctext[i]);
}
printf("\n");
printf("RSA_private_encrypt num: %d\n", num);
RSA_free(key);
unsigned char* pbk = &pbk0[0];
key = d2i_RSAPublicKey(NULL, (const unsigned char**)&pbk, sizeof(pbk0));
num = RSA_public_decrypt(num, ctext, ptext, key,
RSA_PKCS1_PADDING);
if (num != plen || memcmp(ptext, ptext_ex, num) != 0)
{
printf("PKCS#1 v1.5 decryption failed!\n");
err=1;
}
else
printf("PKCS #1 v1.5 encryption/decryption ok\n");
printf("RSA_public_decrypt num: %d\n", num);
ptext[num] = '\0'; //字符串结尾
printf("ptext: %s\n", ptext);
RSA_free(key);