ios - Get SecKeyRef from modulus/exponent -


i have rsa key (pair) represented big integeger modulus , exponent , need encrypt/decrypt those.

i figured out how handle keys needed in ios using swift.

to question: there way convert modulus/exponent representation standard seckeyref?

both formatted big int (coming android), modulus example looks this:

23986589886077318012326064844037831693417390067186403792990846282531380456965701688980194375481519508455379138899060072530724598302129656976140458275478340281694599774176865257462922861492999970413042311221914141827738166785420817621605554859384423695247859963064446809695729281306530681131568503935369097838468173777374667631401317163094053418212485192857751897040859007584244053136110895205839896478287122804119514727484734998762296502939823974188856604771622873660784676915716476754048257418841069214486772931445697194023455179601077893872576165858771367831752886749210944303260745331014786145738511592470796648651

i had same task - given modulus , exponent had create public key , encrypt message using key. after long time spent in reading , trying various libraries, able accomplish openssl. i'm posting way of doing below. although it's written in objective-c, might helpful.

nsdata* message, modulus, exponent; bignum* mod = bn_bin2bn((unsigned char *)[modulus bytes], (int)modulus.length, null); if (mod == null) {     nslog(@"error creating modulus bignum"); }  bignum* exp = bn_bin2bn((unsigned char *)[exponent bytes], (int)exponent.length, null); if (exp == null) {     nslog(@"error creating exponent bignum"); }  rsa* rsa = rsa_new(); rsa->pad = 0; rsa->e = exp; rsa->n = mod;  int keylen = rsa_size(rsa); unsigned char* enc = malloc(keylen); char* err = malloc(130); int status = rsa_public_encrypt((int)message.length, (const unsigned char*)[message bytes], enc, rsa, rsa_no_padding);  if (status != -1) {     nsdata* encryptedmessage = [nsdata datawithbytes:enc length:keylen];     nslog(@"encryption successful: %@", encryptedmessage); } else {     err_load_crypto_strings();     err_error_string(err_get_error(), err);     nslog(@"encryption failed error: %s", err); }  free(enc); free(err); 

so first i'm creating big integers out of nsdata modulus , exponent. have them big integers, if they're not represented openssl's bignum type, you'll have convert them. bignum has other useful functions creating big integers bn_hex2bn , bn_dec2bn - these create big integers out of c strings containing hexadecimal or decimal numbers. in case modulus , exponent stored byte array in nsdata , bn_bin2bn creates bignum directly that.

moving on, create rsa structure represents key , holds modulus , exponent, , enc buffer, hold raw encrypted bytes. length of enc same size of key, because rsa can not encrypt messages longer key.

the main work done rsa_public_encrypt() function. takes 5 arguments - size of message you're going encrypt, actual message bytes, output buffer store encrypted message in, rsa key , padding scheme. i'm using no padding here, because message same size key, in rsa.h there macros represent common padding schemes.

lastly check status holds number of encrypted bytes , print error message if went wrong.

i hope , else. tell me if managed in swift. cheers ;-)

p.s. adding openssl ios project easy using cocoapods. add

pod 'openssl-universal', '1.0.1.k' 

to podfile.


Comments

Popular posts from this blog

javascript - gulp-nodemon - nodejs restart after file change - Error: listen EADDRINUSE events.js:85 -

Fatal Python error: Py_Initialize: unable to load the file system codec. ImportError: No module named 'encodings' -

javascript - oscilloscope of speaker input stops rendering after a few seconds -