实际项目中遇到的情况,需要rsa加解密和签名,但是由于是嵌入式开发,openssl 的库过大,故而采用了EURO的rsa加解密和签名。但是实际情况是他们并不是完全对接的。对方服务器采用了java中的rsa和openssl rsa是对接的。故一字一字的对比后对EURO的加解密做了部分修改,完了成rsa加解密的对接。需要注意的是本例只针对1024长度。其中填充数据由对比的来,不可移植其他长度上去。
/*
* qyh_rsa.c
*
* Created on: 2012-11-4
* Author: qinyinghao
*/
#include
#include
#include
#include "rsaeuro.h"
#include "rsa.h"
int rsa_public_encode(char *in, int in_len, char *out, int *out_len,
const char *key, int key_len) {
int j = 0;
char temp[258] = { 0 };
unsigned char iv[8];
R_RSA_PUBLIC_KEY *publicKey =NULL;
R_RANDOM_STRUCT *randomStruct =NULL;
publicKey = (R_RSA_PUBLIC_KEY *) malloc(
sizeof(R_RSA_PUBLIC_KEY));
memset(publicKey,0,sizeof(R_RSA_PUBLIC_KEY));
for (j = 0; j < 258; j++) {
// printf("%d 0x%02X\n", j, *(key + j));
temp[j] = *(key + j);
}
//memcpy(publicKey->bits, temp, 2);
publicKey->bits = 1024;
for (j = 0; j < 128; j++) {
publicKey->modulus[j] = *(temp + 2 + j);
publicKey->exponent[j] = *(temp + 130 + j);
}
randomStruct = (R_RANDOM_STRUCT *) malloc(
sizeof(R_RANDOM_STRUCT));
memset(randomStruct,0,sizeof(R_RANDOM_STRUCT));
R_GenerateBytes(temp, 258, randomStruct);
R_GenerateBytes(iv, 8, randomStruct);
int i = RSAPublicEncrypt(out, out_len, in, in_len, publicKey, randomStruct);
if(publicKey!=NULL){
free(publicKey);
publicKey=NULL;
}
if(randomStruct!=NULL){
free(randomStruct);
randomStruct=NULL;
}
return i;
}
int rsa_private_decode(char *in, int in_len, char *out, int *out_len, char *key,
int key_len) {
char temp[706] = { 0 };
int j = 0, i = 0;
R_RSA_PRIVATE_KEY *privateKey =NULL;
for (j = 0; j < 706; j++) {
// printf("%d 0x%02X\n", j, *(key + j));
temp[j] = *(key + j);
}
privateKey = (R_RSA_PRIVATE_KEY *) malloc(
sizeof(R_RSA_PRIVATE_KEY));
memset(privateKey,0,sizeof(R_RSA_PRIVATE_KEY));
//将key转存到privatekey中
privateKey->bits = 1024;
for (j = 0; j < 128; j++) {
privateKey->modulus[j] = *(temp + 2 + j);
privateKey->publicExponent[j] = *(temp + 130 + j);
privateKey->exponent[j] = *(temp + 258 + j);
}
for (j = 0; j < 64; j++) {
privateKey->prime[0][j] = *(temp + 386 + j);
privateKey->prime[1][j] = *(temp + 450 + j);
privateKey->primeExponent[0][j] = *(temp + 514 + j);
privateKey->primeExponent[1][j] = *(temp + 578 + j);
privateKey->coefficient[j] = *(temp + 642 + j);
}
i = RSAPrivateDecrypt(out, out_len, in, in_len, privateKey);
if(privateKey!=NULL){
free(privateKey);
privateKey=NULL;
}
return i;
}
/**
* md5绛惧�
*/
int rsa_sign(char *in, int in_len, char *out, int *out_len, char *key,
int key_len) {
int digestAlgorithm, status;
R_SIGNATURE_CTX context = { 0 };
digestAlgorithm = 5; //浠h〃md5
status = 0;
R_RSA_PRIVATE_KEY *privateKey =NULL;
char temp[706] = { 0 };
int j = 0, i = 0;
for (j = 0; j < 706; j++) {
temp[j] = *(key + j);
}
privateKey = (R_RSA_PRIVATE_KEY *) malloc(
sizeof(R_RSA_PRIVATE_KEY));
memset(privateKey,0,sizeof(R_RSA_PRIVATE_KEY));
//将key转存到privatekey中
privateKey->bits = 1024;
for (j = 0; j < 128; j++) {
privateKey->modulus[j] = *(temp + 2 + j);
privateKey->publicExponent[j] = *(temp + 130 + j);
privateKey->exponent[j] = *(temp + 258 + j);
}
for (j = 0; j < 64; j++) {
privateKey->prime[0][j] = *(temp + 386 + j);
privateKey->prime[1][j] = *(temp + 450 + j);
privateKey->primeExponent[0][j] = *(temp + 514 + j);
privateKey->primeExponent[1][j] = *(temp + 578 + j);
privateKey->coefficient[j] = *(temp + 642 + j);
}
//md5
if ((status = R_SignInit(&context, digestAlgorithm)) == 0) {
R_SignUpdate(&context, in, in_len);
//绛惧�
if ((status = R_SignFinal(&context, out, out_len, privateKey)) == 0) {
//printf("sign successful!\n");
if(privateKey!=NULL){
free(privateKey);
privateKey=NULL;
}
return 0;
} else {
printf("sign fail 01!\n");
if(privateKey!=NULL){
free(privateKey);
privateKey=NULL;
}
return -1;
}
} else {
printf("sign fail 02!\n");
if(privateKey!=NULL){
free(privateKey);
privateKey=NULL;
}
return -1;
}
}
int rsa_varify(char *in, int in_len, char *out, int *out_len, char *key,
int key_len) {
R_RSA_PUBLIC_KEY *publicKey=NULL;
char temp[258] = { 0 };
R_SIGNATURE_CTX context;
int digestAlgorithm, status, j;
status = 0;
digestAlgorithm = 5;
publicKey = (R_RSA_PUBLIC_KEY *) malloc(sizeof(R_RSA_PUBLIC_KEY));
for (j = 0; j < 258; j++) {
// printf("%d 0x%02X\n", j, *(key + j));
temp[j] = *(key + j);
}
publicKey->bits = 1024;
for (j = 0; j < 128; j++) {
publicKey->modulus[j] = *(temp + 2 + j);
publicKey->exponent[j] = *(temp + 130 + j);
}
//md5
R_VerifyInit(&context, digestAlgorithm);
if ((status = R_VerifyUpdate(&context, in, in_len)) != 0) {
printf("varify error 01\n");
if(publicKey!=NULL){
free(publicKey);
publicKey=NULL;
}
return -1;
}
status = R_VerifyFinal(&context, out, out_len, publicKey);
if(publicKey!=NULL){
free(publicKey);
publicKey=NULL;
}
return status == 0 ? 0 : -1;
}
#include
#include
#include "RSAEURO/rsaeuro.h"
#include "RSAEURO/qyh_rsa.h"
static char MYPK[512] = {0};
static char MYSK[1024] = {0};
static char HISPK[512] = {0};
int jiamiPackageInfo(char *in, char *securityInfo, char *packageInfo,
char *signagureInfo) {
//Ë°ŸÖ¹«Ô¿ÑéրǩÃû
unsigned char mypk_e[512] = { 0x00 }; //¹«Ô¿ECB
unsigned char mysk_e[1024] = { 0x00 }; //˜ԿECB
unsigned char hispk_e[512] = { 0x00 }; //Ë°ŸÖ¹«Ô¿ECB
int mypublickey_len = 0;
int myprivatekey_len = 0;
int hispublickey_len = 0;
int i = 0;
unsigned char packageInfo_befor64[1024] = { 0 };
int packageInfo_before64_len = 0;
unsigned char signagureInfo_befor64[1024] = { 0 };
int signagureInfo_befor64_len = 0;
char securityInfo_before64[256] = { 0 };
int securityInfo_before64_len = 0;
unsigned char suijima[] = { 0x24, 0x6d, 0x61, 0x6b, 0x65, 0x6d, 0x6f, 0x6e,
0x65, 0x79, 0x32, 0x30, 0x31, 0x32, 0x00, 0x00 }; //Ëæ»úÊý
unsigned char *mypublickey = NULL;
unsigned char *myprivatekey = NULL;
unsigned char *hispublickey = NULL;
mypublickey = (char *) malloc(sizeof(char) * 2048);
memset(mypublickey, 0, 2048);
myprivatekey = (unsigned char *) malloc(sizeof(char) * 2048);
memset(myprivatekey, 0, 2048);
hispublickey = (char *) malloc(sizeof(char) * 2048);
memset(hispublickey, 0, 2048);
base64_decode(MYPK, strlen(MYPK), mypublickey, &mypublickey_len);
base64_decode(MYSK, strlen(MYSK), myprivatekey, &myprivatekey_len);
base64_decode(HISPK, strlen(HISPK), hispublickey, &hispublickey_len);
mypk_e[0] = 0x00;
mypk_e[1] = 0x04;
//žŽÖÆ128λeÖµ
for (i = 0; i < 128; i++) {
mypk_e[i + 2] = mypublickey[i + 29];
}
//žŽÖÆÈýλnÖµ
for (i = 0; i < 3; i++) {
mypk_e[i + 255] = mypublickey[mypublickey_len - 3 + i];
}
//ËûµÄ¹«Ô¿
hispk_e[0] = 0x00;
hispk_e[1] = 0x04;
//žŽÖÆ128λeÖµ
for (i = 0; i < 128; i++) {
hispk_e[i + 2] = hispublickey[i + 29];
}
//žŽÖÆÈýλnÖµ
for (i = 0; i < 3; i++) {
hispk_e[i + 255] = hispublickey[hispublickey_len - 3 + i];
}
//ÎҵĘԿ
mysk_e[0] = 0x00;
mysk_e[1] = 0x04;
for (i = 0; i < 128; i++) {
mysk_e[i + 2] = myprivatekey[37 + i];
}
for (i = 0; i < 3; i++) {
mysk_e[i + 255] = myprivatekey[167 + i];
}
for (i = 0; i < 128; i++) {
mysk_e[i + 258] = myprivatekey[i + 174];
}
for (i = 0; i < 64; i++) {
mysk_e[i + 386] = myprivatekey[i + 305];
mysk_e[i + 450] = myprivatekey[i + 372];
mysk_e[i + 514] = myprivatekey[i + 438];
mysk_e[i + 578] = myprivatekey[i + 504];
mysk_e[i + 642] = myprivatekey[i + 570];
}
//1.Ë°ŸÖ¹«Ô¿ŒÓÃÜ,²¢base64µÃµœsecurityInfo
rsa_public_encode(suijima, 16, securityInfo_before64,
&securityInfo_before64_len, hispk_e, 258);
base64_encode(securityInfo_before64, securityInfo_before64_len,
securityInfo);
//2.¶ÔpackageInfoÔÎÄdesŒÓÃÜ,base64ºó·ÅÈëPackageInfoÖÐ
des_encode(in, strlen(in), packageInfo_befor64, &packageInfo_before64_len,
suijima);
base64_encode(packageInfo_befor64, packageInfo_before64_len, packageInfo);
//3,¶ÔpackageInfoBefor64Ç©Ãû
rsa_sign(packageInfo_befor64, packageInfo_before64_len,
signagureInfo_befor64, &signagureInfo_befor64_len, mysk_e, 706);
base64_encode(signagureInfo_befor64, signagureInfo_befor64_len,
signagureInfo);
if (NULL != myprivatekey)
{
free(myprivatekey);
myprivatekey = NULL;
}
if (NULL != mypublickey)
{
free(mypublickey);
mypublickey = NULL;
}
if (NULL != hispublickey)
{
free(hispublickey);
hispublickey = NULL;
}
return 0;
}
int getPackageInfo(char *securityInfo, char *packageInfo, char *signagureInfo,
char *mingwen) {
unsigned char mypk_e[259] = {0x00};
unsigned char hispk_e[259] = {0x00};
unsigned char mysk_e[707] = { 0x00 };
unsigned char beforeSign[1024*8] = { 0 };
unsigned char afterSign[1024*8] = { 0 };
unsigned char suijima[256] = { 0 };
unsigned char out1[256] = { 0 };
char *mypublickey = (char *) malloc(sizeof(char) * 2048);
unsigned char *myprivatekey = (unsigned char *) malloc(sizeof(char) * 2048);
char *hispublickey = (char *) malloc(sizeof(char) * 2048);
memset(mypublickey,0,2048);
memset(myprivatekey,0,2048);
memset(hispublickey,0,2048);
int beforeSign_len = 0;
int afterSign_len = 0;
int out1_len = 0;
int suijima_len = 0;
int mingwen_len = 0,res = 0,varifyResult = 0;
int hispublickey_len = 0;
int myprivatekey_len = 0;
int mypublickey_len = 0;
int i = 0;
memset(mypk_e,0,259);
memset(hispk_e,0,259);
memset(mysk_e,0,707);
memset(beforeSign,0,1024*8);
memset(afterSign,0,1024*8);
memset(suijima,0,256);
memset(out1,0,256);
base64_decode(MYPK, strlen(MYPK), mypublickey, &mypublickey_len);
base64_decode(MYSK, strlen(MYSK), myprivatekey, &myprivatekey_len);
base64_decode(HISPK, strlen(HISPK), hispublickey, &hispublickey_len);
mypk_e[0] = 0x00;
mypk_e[1] = 0x04;
//žŽÖÆ128λeÖµ
for (i = 0; i < 128; i++) {
mypk_e[i + 2] = mypublickey[i + 29];
}
//žŽÖÆÈýλnÖµ
for (i = 0; i < 3; i++) {
mypk_e[i + 255] = mypublickey[mypublickey_len - 3 + i];
}
//¹«Ô¿
hispk_e[0] = 0x00;
hispk_e[1] = 0x04;
//žŽÖÆ128λeÖµ
for (i = 0; i < 128; i++) {
hispk_e[i + 2] = hispublickey[i + 29];
}
//žŽÖÆÈýλnÖµ
for (i = 0; i < 3; i++) {
hispk_e[i + 255] = hispublickey[hispublickey_len - 3 + i];
}
//ÎҵĘԿ
mysk_e[0] = 0x00;
mysk_e[1] = 0x04;
for (i = 0; i < 128; i++) {
mysk_e[i + 2] = myprivatekey[37 + i];
}
for (i = 0; i < 3; i++) {
mysk_e[i + 255] = myprivatekey[167 + i];
}
for (i = 0; i < 128; i++) {
mysk_e[i + 258] = myprivatekey[i + 174];
}
for (i = 0; i < 64; i++) {
mysk_e[i + 386] = myprivatekey[i + 305];
mysk_e[i + 450] = myprivatekey[i + 372];
mysk_e[i + 514] = myprivatekey[i + 438];
mysk_e[i + 578] = myprivatekey[i + 504];
mysk_e[i + 642] = myprivatekey[i + 570];
}
//Ë°ŸÖ¹«Ô¿ÑéրǩÃû
base64_decode(packageInfo, strlen(packageInfo), beforeSign,
&beforeSign_len);
base64_decode(signagureInfo, strlen(signagureInfo), afterSign,
&afterSign_len);
varifyResult = rsa_varify(beforeSign, beforeSign_len, afterSign,
afterSign_len, hispk_e, 258);
if (varifyResult < 0) {
//Ñéրʧ°Ü
printf("ÑéրǩÃûʧ°Ü\n");
return -1;
} else {
//ÑéÖ€³É¹Š
printf("ÑéրǩÃû³É¹Š\n");
}
//ω̆packageInfo
//»ñÈ¡Ëæ»úÂë
base64_decode(securityInfo, strlen(securityInfo), out1, &out1_len);
//ÎҵĘԿœâÃÜ
rsa_private_decode(out1, out1_len, suijima, &suijima_len, mysk_e, 706);
res = des_decode(beforeSign, beforeSign_len, mingwen, &mingwen_len,
suijima);
if (res != 0) {
printf("des decode fail!\n");
} else {
printf("des decode success!\nThe packageInfo we get is:\n%s\n", mingwen);
}
if (NULL != myprivatekey)
{
free(myprivatekey);
myprivatekey = NULL;
}
if (NULL != mypublickey)
{
free(mypublickey);
mypublickey = NULL;
}
if (NULL != hispublickey)
{
free(hispublickey);
hispublickey = NULL;
}
}
/*
******************************************************
* ¶ÁÈ¡ÃÜÔ¿ÎÄŒþ
* ÎÄŒþÃû³Æ:mypk,mysk,hispk
* ·µ»ØÖµ:
* 0:Õý³£·µ»Ø
* -1:¶ÁÈ¡mypkʧ°Ü
* -2:¶ÁÈ¡myskʧ°Ü
* -3:¶ÁÈ¡hispkʧ°Ü
******************************************************
*/
int readRsaKey(void)
{
char CH;
int i = 0;
int returnvalue = 0;
FILE *fp1 = NULL;
FILE *fp2 = NULL;
FILE *fp3 = NULL;
memset(MYPK,0,512);
memset(MYSK,0,1024);
memset(HISPK,0,512);
i = 0;
if((fp1=fopen("/f-disk/app/mypk","r"))==NULL){
returnvalue = -1;
goto returnout;
}
CH=fgetc(fp1);
while(CH!=EOF&&CH!=0xFF) {
MYPK[i++]=CH;
CH=fgetc(fp1);
}
MYPK[i]='\0';
i = 0;
if((fp2=fopen("/f-disk/app/mysk","r"))==NULL){
returnvalue = -2;
goto returnout;
}
CH=fgetc(fp2);
while(CH!=EOF&&CH!=0xFF) {
MYSK[i++]=CH;
CH=fgetc(fp2);
}
MYSK[i]='\0';
i = 0;
if((fp3=fopen("/f-disk/app/hispk","r"))==NULL){
returnvalue = -3;
goto returnout;
}
CH=fgetc(fp3);
while(CH!=EOF&&CH!=0xFF) {
HISPK[i++]=CH;
CH=fgetc(fp3);
}
HISPK[i]='\0';
returnout:
if(fp1!=NULL)
fclose(fp1);
if(fp2!=NULL)
fclose(fp2);
if(fp3!=NULL)
fclose(fp3);
return returnvalue;
}