Java to Objective-C RSA Implementation -
Java to Objective-C RSA Implementation -
i'm in problem in implementing rsa encryption , decryption in objective-c, made in java , tried translate java code in objc. here java code:
public static byte[] encryptrsa(byte[] text, publickey key) throws exception { byte[] ciphertext = null; // rsa cipher object , print provider cipher cipher = cipher.getinstance("rsa"); // encrypt plaintext using public key cipher.init(cipher.encrypt_mode, key); ciphertext = cipher.dofinal(text); homecoming ciphertext; } public static byte[] decryptrsa(byte[] text, privatekey key) throws exception { byte[] dectyptedtext = null; // decrypt text using private key cipher cipher = cipher.getinstance("rsa"); cipher.init(cipher.decrypt_mode, key); dectyptedtext = cipher.dofinal(text); homecoming dectyptedtext; } and how generate key pair
string seed = "somerandomseed"+long.tostring(system.currenttimemillis()); keypairgenerator keygen = keypairgenerator.getinstance("rsa"); securerandom rand = new securerandom(seed.getbytes()); keygen.initialize(4096,rand); keypair keypair = keygen.generatekeypair(); privatekey privatekey = keypair.getprivate(); publickey publickey = keypair.getpublic(); now in objc have writed code sems work, don't know hot generate rsa seed, in java, , how import key save in java method
//for import public static byte[] hexstringtobytearray(string s) { byte[] b = new byte[s.length() / 2]; (int = 0; < b.length; i++) { int index = * 2; int v = integer.parseint(s.substring(index, index + 2), 16); b[i] = (byte) v; } homecoming b; } //for export public static string bytearraytohexstring(byte[] b) { stringbuilder sb = new stringbuilder(b.length * 2); (int = 0; < b.length; i++) { int v = b[i] & 0xff; if (v < 16) { sb.append('0'); } sb.append(integer.tohexstring(v)); } homecoming sb.tostring().touppercase(); } here objc code
//this works +(nsstring *)decryptrsa:(nsstring *)cipherstring key:(seckeyref) privatekey { size_t plainbuffersize = seckeygetblocksize(privatekey); uint8_t *plainbuffer = malloc(plainbuffersize); nsdata *incomingdata = [cipherstring decodefromhexidecimal]; uint8_t *cipherbuffer = (uint8_t*)[incomingdata bytes]; size_t cipherbuffersize = seckeygetblocksize(privatekey); seckeydecrypt(privatekey, ksecpaddingoaepkey, cipherbuffer, cipherbuffersize, plainbuffer, &plainbuffersize); nsdata *decrypteddata = [nsdata datawithbytes:plainbuffer length:plainbuffersize]; nsstring *decryptedstring = [[nsstring alloc] initwithdata:decrypteddata encoding:nsutf8stringencoding]; homecoming decryptedstring; } //this works +(nsstring *)encryptrsa:(nsstring *)plaintextstring key:(seckeyref)publickey { size_t cipherbuffersize = seckeygetblocksize(publickey); uint8_t *cipherbuffer = malloc(cipherbuffersize); uint8_t *nonce = (uint8_t *)[plaintextstring utf8string]; seckeyencrypt(publickey, ksecpaddingoaepkey, nonce, strlen( (char*)nonce ), &cipherbuffer[0], &cipherbuffersize); nsdata *encrypteddata = [nsdata datawithbytes:cipherbuffer length:cipherbuffersize]; homecoming [encrypteddata hexadecimalstring]; } //here generate key pair #define kpublickeytag "com.apple.sample.publickey" #define kprivatekeytag "com.apple.sample.privatekey" //i should utilize these seed!?!!? - (void)generatekeypair:(nsuinteger)keysize { osstatus sanitycheck = noerr; publickeyref = null; privatekeyref = null; // container dictionaries. nsmutabledictionary * privatekeyattr = [[nsmutabledictionary alloc] init]; nsmutabledictionary * publickeyattr = [[nsmutabledictionary alloc] init]; nsmutabledictionary * keypairattr = [[nsmutabledictionary alloc] init]; // set top level dictionary keypair. [keypairattr setobject:(id)ksecattrkeytypersa forkey:(id)ksecattrkeytype]; [keypairattr setobject:[nsnumber numberwithunsignedinteger:keysize] forkey:(id)ksecattrkeysizeinbits]; // set private key dictionary. [privatekeyattr setobject:[nsnumber numberwithbool:yes] forkey:(id)ksecattrispermanent]; [privatekeyattr setobject:privatetag forkey:(id)ksecattrapplicationtag]; // see seckey.h set other flag values. // set public key dictionary. [publickeyattr setobject:[nsnumber numberwithbool:yes] forkey:(id)ksecattrispermanent]; [publickeyattr setobject:publictag forkey:(id)ksecattrapplicationtag]; // see seckey.h set other flag values. // set attributes top level dictionary. [keypairattr setobject:privatekeyattr forkey:(id)@ksecprivatekeyattrs]; [keypairattr setobject:publickeyattr forkey:(id)@ksecpublickeyattrs]; // seckeygeneratepair returns seckeyrefs educational purposes. sanitycheck = seckeygeneratepair((__bridge cfdictionaryref)keypairattr, &publickeyref, &privatekeyref); } this method utilize export keys in objc, seems work java method
+ (nsstring *)fromprivatekeytostring: (seckeyref) privatekey { size_t pkeysize = seckeygetblocksize(privatekey); nsdata* pkeydata = [nsdata datawithbytes:privatekey length:pkeysize]; nsstring* pkeystring = [pkeydata hexadecimalstring]; homecoming pkeystring; }
as explained in other answer, tricky generate same key pair using same value of prng. not seem after. seems want utilize own seeded prng generate key pair.
in general, default securerandom in java seeded operating system. thought can supply own random number generator may "better" results using instance own entropy pool (e.g. a hardware random number generator). default java prng seeded operating scheme provide plenty random.
as using securerandom class, supplant operating scheme provided seed own relatively weakly seeded prng. currenttimemilis not give much entropy, , password seems static. not thought plenty generating rsa key pairs.
if want can add entropy pool instead:
// create runtime default prng securerandom rng = new securerandom(); // create sure rng seeded operating scheme rng.nextint(); // add together secret pool rng.setseed("some_secret".getbytes(standardcharsets.utf_8)); // add together time info pool rng.setseed(system.currenttimemillis()); // utilize e.g. rsa key pair generation there seems no method of injecting own random number generator in apple's os x libraries. indicated, os provided random number generator enough. if want can write additional seeds /dev/random though.
java objective-c encryption rsa public-key-encryption
Comments
Post a Comment