iOS client RSA public key encryption. NET background private key decryption solution (based on OpenSSL)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.