ios - RSA Encryption using existing public key -
i trying encrypt nsdata following method:
- (nsdata *) encryptwithdata:(nsdata *)content { size_t plainlen = [content length]; void *plain = malloc(plainlen); [content getbytes:plain length:plainlen]; size_t cipherlen = 256; void *cipher = malloc(cipherlen); osstatus returncode = seckeyencrypt("public key here", ksecpaddingpkcs1, plain, plainlen, cipher, &cipherlen); nsdata *result = nil; if (returncode != 0) { nslog(@"seckeyencrypt fail. error code: %ld", returncode); } else { result = [nsdata datawithbytes:cipher length:cipherlen]; } free(plain); free(cipher); return result;
}
where written "public key here" want load existing public key copied bundle. how can that?
for example use certificate file contains public key:
nsdata *certificatedata = [nsdata datawithcontentsofurl:certificateurl options:0 error:&error]; if (certificatedata) { seccertificateref certificate = seccertificatecreatewithdata(null, (__bridge cfdataref)(certificatedata)); // ... seckeyref publickey; seccertificatecopypublickey(certificate, &publickey); // ... }
to load data bundle:
nsarray *certificateurls = [[nsbundle mainbundle] urlsforresourceswithextension:@"cer" subdirectory:@"mycertificates"]; (nsurl *certificateurl in certificateurls) { nsdata *certificatedata = [nsdata datawithcontentsofurl:certificateurl options:0 error:&error]; // ... }
Comments
Post a Comment