Convert AES encryption of android in Objective-c xcode -
i have use aes encryption , decryption similiar below code
need pass similiar data android did generate key
package encypt.com; import java.io.bufferedreader; import java.io.filereader; import java.security.*; import java.security.spec.invalidkeyspecexception; import javax.crypto.cipher; import javax.crypto.spec.secretkeyspec; import sun.misc.*; public class testing { private static final string algorithm = "aes"; private static final int iterations = 2; private static final byte[] keyvalue = new byte[] { 't', 'h', 'i', 's', 'i', 's', 'a', 's', 'e', 'c', 'r', 'e', 't', 'k', 'e', 'y'}; public static string encrypt(string value, string salt) throws exception { key key = generatekey(); cipher c = cipher.getinstance(algorithm); c.init(cipher.encrypt_mode, key); string valuetoenc = null; string evalue = value; (int = 0; < iterations; i++) { valuetoenc = salt + evalue; byte[] encvalue = c.dofinal(valuetoenc.getbytes()); evalue = new base64encoder().encode(encvalue); } return evalue; } public static string decrypt(string value, string salt) throws exception { key key = generatekey(); cipher c = cipher.getinstance(algorithm); c.init(cipher.decrypt_mode, key); string dvalue = null; string valuetodecrypt = value; (int = 0; < iterations; i++) { byte[] decordedvalue = new base64decoder().decodebuffer(valuetodecrypt); byte[] decvalue = c.dofinal(decordedvalue); dvalue = new string(decvalue).substring(salt.length()); valuetodecrypt = dvalue; } return dvalue; } private static key generatekey() throws exception { key key = new secretkeyspec(keyvalue, algorithm); // secretkeyfactory keyfactory = secretkeyfactory.getinstance(algorithm); // key = keyfactory.generatesecret(new deskeyspec(keyvalue)); return key; }
for ios used
i've found code through lots of research:
#import "<commoncrypto/commoncryptor.h>" @implementation nsmutabledata(aes)
for encryption:
- (nsmutabledata*) encryptaes:(nsstring *)key { char keyptr[kcckeysizeaes256+1]; bzero( keyptr, sizeof(keyptr) ); [key getcstring: keyptr maxlength: sizeof(keyptr) encoding: nsutf16stringencoding]; size_t numbytesencrypted = 0; nsuinteger datalength = [self length]; size_t buffersize = datalength + kccblocksizeaes128; void *buffer = malloc(buffersize); nsmutabledata *output = [[nsdata alloc] init]; cccryptorstatus result = cccrypt(kccencrypt, kccalgorithmaes128, kccoptionpkcs7padding, keyptr, kcckeysizeaes256, null, [self mutablebytes], [self length], buffer, buffersize, &numbytesencrypted); output = [nsmutabledata datawithbytesnocopy:buffer length:numbytesencrypted]; if(result == kccsuccess) { return output; } return null; }
for decryption:
- (nsmutabledata*)decryptaes: (nsstring*)key andfordata:(nsmutabledata*)objencrypteddata { char keyptr[kcckeysizeaes256+1]; bzero( keyptr, sizeof(keyptr) ); [key getcstring:keyptr maxlength:sizeof(keyptr) encoding:nsutf16stringencoding]; size_t numbytesencrypted = 0; nsuinteger datalength = [self length]; size_t buffersize = datalength + kccblocksizeaes128; void *buffer_decrypt = malloc(buffersize); nsmutabledata *output_decrypt = [[nsdata alloc] init]; cccryptorstatus result = cccrypt(kccdecrypt , kccalgorithmaes128, kccoptionpkcs7padding, keyptr, kcckeysizeaes256, null, [self mutablebytes], [self length], buffer_decrypt, buffersize, &numbytesencrypted); output_decrypt = [nsmutabledata datawithbytesnocopy:buffer_decrypt length:numbytesencrypted]; if(result == kccsuccess) { return output_decrypt; } return null; } }
this code made correspond above code:
- (void)encrypt { //convert nsstring nsdata can used encrypt input nsstring *input = [inputbox text]; nsdata *inputdata = [input datausingencoding:nsutf8stringencoding]; //what here }
how use code, these methods? go in implementation file? line near top says you're adding aes functionality nsmutabledata:
@implementation nsmutabledata(aes)
in objective-c, called category; categories let extend existing class.
this code typically go in file named nsmutabledata-aes.m. create header file too, nsmutabledata-aes.h. should contain:
@interface nsmutabledata(aes) - (nsmutabledata*) encryptaes: (nsstring *) key; @end
include (#import) header in main file. add call encryption function in code:
nsdata *inputdata = [input datausingencoding:nsutf8stringencoding]; nsdata *encrypteddata = [inputdata encryptaes:@"myencryptionkey"];
similarly decryption.
but unable set keyvalue , salt android did! please help
private static final byte[] keyvalue = new byte[] { 't', 'h', 'i', 's', 'i', 's', 'a', 's', 'e', 'c', 'r', 'e', 't', 'k', 'e', 'y'}; valuetoenc = salt + evalue;
you can use third party library rncryptor in cocoapods en/decryption
// encryption nsdata *data = ... nsstring *password = @"secret password"; nsdata *ciphertext = [rncryptor encryptdata:data password:password]; // decryption nserror *error = nil; nsdata *plaintext = [rncryptor decryptdata:ciphertext password:password error:&error]; if (error != nil) { nslog(@"error:%@", error); return } // ...
Comments
Post a Comment