生成秘钥
/*************************************************************************
> File Name: test1.c
> Author: ma6174
> Mail: ma6174@163.com
> Created Time: Sat 05 Apr 2014 10:15:51 AM PDT
************************************************************************/
#include <stdio.h>
#include <string.h>
#include <openssl/pem.h>
#define PUBKEYFILE "./pubkey.key"
#define PRIKEYFILE "./prikey.key"
int Prikey_Saveto_File(RSA *rsa,const char *filename);
int Pubkey_Saveto_File(RSA *rsa,const char *filename);
int main()
{
RSA *key;
FILE *fp_pub,*fp_pri;
key=RSA_generate_key(1024,65537,NULL,NULL);
if(NULL==key)
{
perror("generate_key error\n");
exit(0);
}
if(!Prikey_Saveto_File(key,PRIKEYFILE))
{
perror("Prikey_Saveto_File() error\n");
exit(0);
}
if(!Pubkey_Saveto_File(key,PUBKEYFILE))
{
perror("Pubkey_Saveto_File() error\n");
exit(0);
}
printf("generate key OK\n");
return 1;
}
/********************************************
*
* *write private key to file keyfile
*
* ********************************************/
int Prikey_Saveto_File(RSA *rsa,const char *filename)
{
FILE *file;
if (NULL == rsa)
{
printf("RSA not initial.\n");
return 0;
}
file = fopen(filename,"wb");
if (NULL == filename )
{
fprintf(stderr,"%s open error",filename);
return 0;
}
PEM_write_RSAPrivateKey(file, rsa, NULL, NULL, 512, NULL, NULL);
fclose(file);
return 1;
}
/********************************************
*
* *write public key to file keyfile
*
* ********************************************/
int Pubkey_Saveto_File(RSA *rsa,const char *filename)
{
FILE *file;
if (NULL == rsa)
{
printf("RSA not initial.\n");
return 0;
}
file = fopen(filename,"wb");
if (NULL == file )
{
fprintf(stderr,"%s open error",filename);
return 0;
}
PEM_write_RSAPublicKey(file, rsa);
fclose(file);
return 1;
}
加密文件
/*************************************************************************
> File Name: encrypt.c
> Author: ma6174
> Mail: ma6174@163.com
> Created Time: Sat 05 Apr 2014 08:23:55 PM PDT
************************************************************************/
#include <openssl/md5.h>
#include <openssl/rsa.h>
#include <openssl/pem.h>
#include <string.h>
#include <stdio.h>
#if 0
#define TEST_FILE_PATH "./test/test"
#define OUT_FILE_PATH "./test/jiami"
#endif
#define PUB_KEY_PATH "pubkey.key"
#define BUFSIZE 110
#define BUFSIZE1 2048
RSA* Pubkey_Getfrom_File(RSA *rsa,const char *filename);
int main(int argc,char **argv)
{
RSA* key;
char buf[BUFSIZE];
char buf1[BUFSIZE1];
int ret;
FILE *fp,*fp0,*file;
memset(buf,0,BUFSIZE);
memset(buf1,0,BUFSIZE1);
key=RSA_new();
if(NULL==key)
{
perror("RSA_new()");
exit(0);
}
if(argc<3)
{
perror("usage,input filein you needto encode and fileout\n");
exit(0);
}
//read prikey
key=Pubkey_Getfrom_File(key,PUB_KEY_PATH);
if(NULL==key)
{
perror("Pubkey_Getfrom_File() wrong");
exit(0);
}
//open relate file
fp=fopen(argv[1]/*TEST_FILE_PATH*/,"r");
if(fp==NULL)
{
perror("fopen() input wrong");
exit(0);
}
fp0=fopen(argv[2]/*OUT_FILE_PATH*/,"w");
if(fp0==NULL)
{
perror("fopen() output wrong");
exit(0);
}
while((ret=fread(buf,sizeof(char),BUFSIZE,fp))==BUFSIZE)
{//read string from file
memset(buf1,0,BUFSIZE1);
ret = RSA_public_encrypt(BUFSIZE, buf, buf1,
key, RSA_PKCS1_PADDING);//en-code
if (ret<0)
{
perror("error in enc");
}
else
{
fwrite(buf1,sizeof(char),ret,fp0);//write string to file
}
}
ret = RSA_public_encrypt(ret, buf, buf1,
key, RSA_PKCS1_PADDING); //en-code
if (ret<0)
{
perror("error in enc");
}
fwrite(buf1,sizeof(char),ret,fp0);//write string to file
fclose(fp);
fclose(fp0);
RSA_free(key);//relase
return 0;
}
/****************************************
*read public key from file
****************************************/
RSA* Pubkey_Getfrom_File(RSA *rsa,const char *filename)
{
FILE *file;
if (NULL == rsa)
{
printf("RSA not initial!\n");
return NULL;
}
file = fopen(filename, "rb");
if (NULL == file)
{
fprintf(stderr,"%s open error",filename);
return NULL;
}
rsa=PEM_read_RSAPublicKey(file,NULL, NULL, NULL);
if(rsa==NULL)
{
perror("PEM_read_RSAPublicKey() ");
fclose(file);
return NULL;
}
fclose(file);
return rsa;
}
解密文件
/*************************************************************************
> File Name: decrypt.c
> Author: ma6174
> Mail: ma6174@163.com
> Created Time: Sat 05 Apr 2014 08:35:13 PM PDT
***********************************************************************/
#include <openssl/pem.h>
#include <string.h>
#include <stdio.h>
#if 0
#define TEST_FILE_PATH "./test/jiami"
#define OUT_FILE_PATH "./test/jiemi"
#endif
#define PRI_KEY_PATH "prikey.key"
#define BUFSIZE 1024
#define BUFSIZE1 2048
RSA* Prikey_Getfrom_File(RSA *rsa,const char *filename);
int main(int argc,char **argv)
{
RSA* key;
char buf[BUFSIZE];
char buf1[BUFSIZE1];
int ret,rsa_len;
FILE *fp,*fp0,*file;
memset(buf,0,BUFSIZE);
memset(buf1,0,BUFSIZE1);
if(argc<3)
{
perror("usage input file needto decode and outfile\n");
exit(0);
}
key=RSA_new();
if(NULL==key)
{
perror("RSA_new()");
exit(0);
}
//read prikey
key=Prikey_Getfrom_File(key,PRI_KEY_PATH);
if(NULL==key)
{
perror("Prikey_Getfrom_File() wrong");
exit(0);
}
//open relate file
fp=fopen(argv[1]/*TEST_FILE_PATH*/,"r");
if(fp==NULL)
{
perror("fopen() input wrong");
exit(0);
}
fp0=fopen(argv[2]/*OUT_FILE_PATH*/,"w");
if(fp0==NULL)
{
perror("fopen() output wrong");
exit(0);
}
rsa_len=RSA_size(key);
while((ret=fread(buf,sizeof(char),rsa_len,fp))==rsa_len)
{//read string from file
memset(buf1,0,BUFSIZE1);
ret = RSA_private_decrypt(rsa_len, buf, buf1,
key, RSA_PKCS1_PADDING);//de-code
if (ret<0)
{
perror("error in enc");
}
else
{
fwrite(buf1,sizeof(char),ret,fp0);//write string to file
}
}
/*ret = RSA_private_decrypt(rsa_len, buf, buf1,
key, RSA_PKCS1_PADDING); //de-code
if (ret<0)
{
perror("error in enc");
}
fwrite(buf1,sizeof(char),ret,fp0);//write string to file
*/
fclose(fp);
fclose(fp0);
RSA_free(key);//relase
return 0;
}
/****************************************
*read private key from file
****************************************/
RSA* Prikey_Getfrom_File(RSA *rsa,const char *filename)
{
FILE *file;
if (NULL == rsa)
{
printf("RSA not initial!\n");
return NULL;
}
file = fopen(filename, "rb");
if (NULL == file)
{
fprintf(stderr,"%s open error",filename);
return NULL;
}
rsa=PEM_read_RSAPrivateKey(file, NULL, NULL, NULL);
if(rsa==NULL)
{
perror("PEM_read_RSAPrivateKey() wrong\n");
fclose(file);
return NULL;
}
fclose(file);
return rsa;
}