IOS RSA Certificate Encryption

Source: Internet
Author: User

#import"GLQyRsa.h"#import"GLSupprot.h"#import"GLLoginViewController.h"@implementation GlqyrsaStaticSeckeyref _public_key=Nil;+(seckeyref) getpublickeyfile{//get the Seckeyref pointer to the public key from the public key certificate file    if(_public_key = =Nil) {        //NSData *certificatedata = [Rsa_key_base64 datausingencoding:nsutf8stringencoding];//nsstring *publickeypath = [[NSBundle mainbundle] pathforresource:@ "Public_key"//oftype:@ "der"];//if (Publickeypath = = nil) {//NSLog (@ "Can not find Pub.der");//return nil;//        }NSString *fielname =[[Nsuserdefaults standarduserdefaults]objectforkey:my_publickeyfilename]; //NSLog (@ "fielname:%@", fielname), filename for. cer certificate        if(!fielname) {NSLog (@"fielname Nil"); returnNil; } nsdate*certificatedata =[NSData Datawithcontentsoffile:fielname]; if(Certificatedata = =Nil) {NSLog (@"Can not read from Pub.der"); returnNil; } seccertificateref mycertificate=Seccertificatecreatewithdata (Kcfallocatordefault, (__bridge cfdataref) certificatedata); Secpolicyref MyPolicy=SecPolicyCreateBasicX509 ();        Sectrustref Mytrust; Osstatus Status= Sectrustcreatewithcertificates (mycertificate,mypolicy,&mytrust);        Sectrustresulttype Trustresult; if(Status = =NOERR) {Status= Sectrustevaluate (Mytrust, &Trustresult); } _public_key=Sectrustcopypublickey (Mytrust);        Cfrelease (mycertificate);        Cfrelease (MyPolicy);    Cfrelease (Mytrust); }    return_public_key;}+ (nsdata*) rsaencryptstring: (nsstring*)string{seckeyref key=[self getpublickeyfile]; if(!key) {NSLog (@"Seckeyrefnull"); returnNil; } size_t cipherbuffersize=seckeygetblocksize (key); uint8_t*cipherbuffer = malloc (Cipherbuffersize *sizeof(uint8_t)); NSData*stringbytes = [stringdatausingencoding:nsutf8stringencoding]; size_t blockSize= Cipherbuffersize- One; size_t Blockcount= (size_t) ceil ([Stringbytes length]/(Double) blockSize); Nsmutabledata*encrypteddata =[[Nsmutabledata alloc] init];  for(intI=0; i<blockcount; i++) {        intbuffersize = MIN (blocksize,[stringbytes length]-I *blockSize); NSData*buffer = [Stringbytes subdatawithrange:nsmakerange (i *blockSize, buffersize)]; Osstatus Status= Seckeyencrypt (Key, KSecPaddingPKCS1, (Constuint8_t *) [Buffer bytes], [buffer length], Cipherbuffer,&cipherbuffersize); if(Status = =NOERR) {NSData*encryptedbytes = [[NSData alloc] Initwithbytes: (Const void*) Cipherbuffer Length:cipherbuffersize];                    [EncryptedData appenddata:encryptedbytes]; }Else{            if(Cipherbuffer) free (cipherbuffer); returnNil; }    }            if(Cipherbuffer) free (cipherbuffer); //NSLog (@ "Encrypted text (%d bytes):%@", [EncryptedData length], [EncryptedData description]); //NSLog (@ "Encrypted text base64:%@", [Base64 Encode:encrypteddata]);    returnEncryptedData;}

IOS RSA Certificate Encryption

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.