objective c - Decrypt with java and AES file encrypted in Obj-C -
i have file encrypted obj-c code:
nsmutabledata *filedata = [nsmutabledata new]; // file data. [[self encrypt::filedata withkey:@"some_key"]] - (nsmutabledata*) encrypt:(nsmutabledata*)data withkey: (nsstring *) key { // 'key' should 32 bytes aes256, null-padded otherwise char keybuffer[kcckeysizeaes128+1]; // room terminator (unused) bzero( keybuffer, sizeof(keybuffer) ); // fill zeroes (for padding) [key getcstring: keybuffer maxlength: sizeof(keybuffer) encoding: nsutf8stringencoding]; // encrypts in-place, since mutable data object size_t numbytesencrypted = 0; size_t returnlength = ([data length] + kcckeysizeaes256) & ~(kcckeysizeaes256 - 1); char* returnbuffer = malloc(returnlength * sizeof(uint8_t) ); cccryptorstatus result = cccrypt(kccencrypt, kccalgorithmaes128 , kccoptionpkcs7padding | kccoptionecbmode, keybuffer, kcckeysizeaes128, nil, [data bytes], [data length], returnbuffer, returnlength, &numbytesencrypted); if(result == kccsuccess) return [nsmutabledata datawithbytes:returnbuffer length:numbytesencrypted]; else return nil; }
i want read file java. trying
secretkeyfactory factory = secretkeyfactory.getinstance("pbkdf2withhmacsha1"); // salt ?? keyspec spec = new pbekeyspec("some_key", salt, 65536, 256); secretkey tmp = factory.generatesecret(spec); secretkey secret = new secretkeyspec(tmp.getencoded(), "aes"); cipher cipher = cipher.getinstance("aes/cbc/pkcs5padding"); cipher.init(cipher.decrypt_mode, secret, new ivparameterspec(iv)); string plaintext = new string(cipher.dofinal(ciphertext), "utf-8"); system.out.println(plaintext);
i don't know obj-c. think done ? thank you.
use cipher.getinstance("aes/ecb/pkcs5padding");
since objective-c code using ecb instead of cbc (you ought use cbc or ctr instead of ecb, in case doesn't have choice)
also, use secretkey secret = new secretkeyspec("some_key".getbytes("utf-8"), "aes");
since objective-c code using raw bytes password generate key (rather using cryptographic key generator you're doing). "some_key" needs same password used objective-c code. in addition, "some_key".getbytes("utf-8") needs 16 bytes, if necessary need truncate 16 bytes or else pad 0's 16 bytes.
Comments
Post a Comment