1. The. PEM-formatted OpenSSL public key file is required to be available in the background. For details, please refer to:http://www.qmailer.net/archives/216.html (OpenSSL key-related command )
Char *my_encrypt (const char *str,const char *path_key) {
Char *p_en;
RSA *p_rsa;
FILE *file;
int Flen,rsa_len;
if ((File=fopen (Path_key, "R")) ==null) {
Perror ("Open key file Error");
return NULL;
}
if ((P_rsa=pem_read_rsa_pubkey (file,null,null,null)) ==null) {
if ((P_rsa=pem_read_rsapublickey (file,null,null,null)) ==null) {
ERR_PRINT_ERRORS_FP (stdout);
return NULL;
}
NSString * plainstring = @ "plainstring";
int len = (int) [plainstring length];//the length of the letter string to be encrypted
unsigned char *plainbuffer = (unsigned char *) [plainstring utf8string];
int clen = rsa_size (P_RSA);
unsigned char *cipherbuffer = calloc (clen, sizeof (unsigned char));
Rsa_public_encrypt (Len, Plainbuffer, Cipherbuffer, P_rsa, rsa_pkcs1_padding);
return cipherbuffer;
}
2. You can also try:
-(NSString *) encryptwithstring: (NSString *) content
{
NSData *publickey = [NSData datafrombase64string:rsa_key_base64];
NSData *usernamm = [content datausingencoding:nsutf8stringencoding];
NSData *newkey= [Seckeywrapper Encrypt:usernamm Publickey:publickey];
NSString *result = [NewKey base64encodedstring];
return result;
}
Reference Link: http://www.cnblogs.com/aLittleBitCool/archive/2011/09/22/2185418.html
Http://www.qmailer.net/archives/216.html
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
iOS client RSA public key encryption. NET background private key decryption solution (based on OpenSSL)