資源簡介
通過讀取PEM密鑰,調用openssl開發RSA1024的加密解密,包括私鑰的加解密和公鑰的加解密。
代碼片段和文件信息
#include?
#include?
#include?
#include?
#include?
#include?
#include?
#define?OPENSSLKEY?“test.key“
#define?PUBLICKEY?“public2.pem“
#define?PRIVATEKEY?“private.pem“
int?my_encrypt(char?*str?char?*path_key?unsigned?char?*caRspCode);?//加密
int?my_decrypt(char?*str?const?int?ilen?char?*path_key?unsigned?char?*caRspData); //解密
int?my_encryptPrivate(char?*str?char?*path_key?unsigned?char?*caRspData); //解密
int?my_decryptPrivate(char?*str?const?int?ilen?char?*path_key?unsigned?char?*caRspData); //解密
/**?
*?Use?EVP?to?base64?encode?the?input?byte?array?to?readable?text?
*/??
int?base64(const?unsigned?char?*inputBuffer?int?inputLen?unsigned?char?*base64)??
{??
EVP_ENCODE_CTX??ctx;??
int?base64Len?=?(((inputLen+2)/3)*4)?+?1;?//?base64?text?length??
int?pemLen?=?base64Len?+?base64Len/64;?//?PEM?adds?a?newline?every?64?bytes??
//char?base64?=?new?char[pemLen];??
int?result;??
EVP_EncodeInit(&ctx);??
EVP_EncodeUpdate(&ctx?(unsigned?char?*)base64?&result?(unsigned?char?*)inputBuffer?inputLen);??
EVP_EncodeFinal(&ctx?(unsigned?char?*)&base64[result]?&result);??
return?pemLen;??
}??
??
/**
*?Use?EVP?to?base64?decode?the?input?readable?text?to?original?bytes?
*/
int?unbase64(char?*input?int?length?int*?outLen?unsigned?char?*?orgBuf)??
{??
EVP_ENCODE_CTX??ctx;??
int?orgLen?=?(((length+2)/4)*3)?+?1;??
//unsigned?char*?orgBuf?=?new?unsigned?char[orgLen];??
int?result?tmpLen;??
EVP_DecodeInit(&ctx);??
EVP_DecodeUpdate(&ctx?(unsigned?char?*)orgBuf?&result?(unsigned?char?*)input?length);??
EVP_DecodeFinal(&ctx?(unsigned?char?*)&orgBuf[result]?&tmpLen);??
result?+=?tmpLen;??
*outLen?=?result;??
return?result;??
}
int?main(int?argc?char?*?argv[])
{
int?iLen?iLen2;
int?i;
int?iRet;
unsigned?char?caRspData[2048];
char????caDecRDat[2048];
char?caOutBuf[2048];
unsigned?char?caOutBuf2[1024];
????char?*source=“i?like?dancing?!“;
????char?*ptr_en*ptr_de;
if(argc?2)?
{
printf(“argc?2?err“);
return?0;
}
????printf(“source?is????:%s\n“source);
memset(caRspData?0x00?sizeof(caRspData));
iLen?=?my_encryptPrivate(source?argv[1]?caRspData);
????if(iLen?>?0)
{
printf(“S1:“);
for(i?=?0;?i? {
printf(“%02X“?caRspData[i]);
}
printf(“?%d\n“?iLen);
}
else
return?-1;
iRet?=?0;
if(iLen?>?0)
{
memset(caOutBuf?0x00?sizeof(caOutBuf));
//iRet?=?base64_encode(caRspData?iLen?caOutBuf);
iRet?=?base64(caRspData?iLen?caOutBuf);
//iRet?=?base64_encodeA(caRspData?iLen?caOutBuf);
if(iRet?0)
{
printf(?“base64_encode?failed\n“);
return?-1;
}
printf(“\noutdata:%s\n“?caOutBuf);
}
memset(caRspData?0x00?sizeof(caRspData));
//iLen?=?base64_decode(caOutBuf?caRspData);
iLen2?=?0;
iLen?=?unbase64(caOutBuf?iRet?&iLen2?caRspData);
//iLen?=?base64_decodeA(caOutBuf
- 上一篇:stm32f407+FFT浮點運算例程
- 下一篇:MSComm_VS2010
評論
共有 條評論