Unable to do AES 128 bit encrption with 1024 iterations - iOS and C# .NET -


i using 128 bit aes encryption 1024 iterations encryption in .net, using article reference:

http://steelmon.wordpress.com/2013/07/01/simple-interoperable-encryption-in-java-and-net/

the .net code encryption fine:

class cls_security {     private const int iterations = 1024; // recommendation >= 1000     private const string skeydemo = "abcdefghijklmnopq";// "_?73^?dvt3st5har3";     private const string saltkeydemo = "1234567890abcdefghi";//"!2s@lt&kt3st5har3ey";            private const string initvectordemo = "abcdefgh12345678";      public static bool encryptfile(string srcfilename, string destfilename , bool isdemo)     {         bool res = false;         var aes = new aesmanaged();         aes.blocksize = 128;         aes.keysize = 128;         var salt = encoding.utf8.getbytes(saltkeydemo);         var key = new rfc2898derivebytes("", salt, iterations);             salt = encoding.utf8.getbytes(saltkeydemo);             key = new rfc2898derivebytes(skeydemo, salt, iterations);             aes.key = key.getbytes(aes.keysize / 8);              aes.iv = encoding.ascii.getbytes(initvectordemo);          aes.mode = ciphermode.cbc;         aes.padding = paddingmode.pkcs7;          icryptotransform transform = aes.createencryptor(aes.key, aes.iv);          using (var dest = new filestream(destfilename, filemode.create, fileaccess.write, fileshare.none))         {             using (var cryptostream = new cryptostream(dest, transform, cryptostreammode.write))             {                 using (var source = new filestream(srcfilename, filemode.open, fileaccess.read, fileshare.read))                 {                     source.copyto(cryptostream);                     res = true;                 }             }         }         return res;     } } 

the objective-c code doesn't decrypt file perfectly:

#import <commoncrypto/commoncrypto.h> #import "testviewcontroller.h" #import "rncryptmanager.h"  @interface testviewcontroller ()  @end  @implementation testviewcontroller  - (void)viewdidload {     [super viewdidload];     // additional setup after loading view, typically nib.      nsstring *url = [[nsbundle mainbundle] pathforresource:@"topics" oftype:@"xml"];      nsdata *inputeddata = [[nsdata alloc] initwithcontentsoffile:url];      nsstring *path = [nsstring stringwithformat:@"%@topics.xml",nstemporarydirectory()];      //this can decrypt     nsstring *key = @"abcdefghijklmnopq";       //on .net side using     nsstring *ivkey = @"abcdefgh12345678";     nsstring *saltkey = @"1234567890abcdefghi";        if(inputeddata)     {             nsdata *iv    =  [ivkey datausingencoding:nsutf8stringencoding];             nsdata *salt  =  [saltkey datausingencoding:nsutf8stringencoding]; ;             nserror *error = nil;             nsdata *resulteddata = [rncryptmanager decrypteddatafordata:inputeddata password:key iv:iv salt:salt error:&error];           [resulteddata writetofile:path atomically:yes];          nslog(@"error === >> %@",error);         nslog(@"result path ===>>> %@",path);     }  }  + (nsdata *)docipher:(nsdata *)datain                   iv:(nsdata *)iv                  key:(nsdata *)symmetrickey              context:(ccoperation)encryptordecrypt {    //int  bytes[] = { 0x0, 0x1, 0x2, 0x3, 0x5, 0x6, 0x7, 0x8, 0xa, 0xb, 0xc, 0xd, 0xf, 0x10, 0x11, 0x12 };     cccryptorstatus ccstatus   = kccsuccess;     size_t          cryptbytes = 0;    // number of bytes moved buffer.     nsmutabledata  *dataout    = [nsmutabledata datawithlength:datain.length + kccblocksizeaes128];      ccstatus = cccrypt( encryptordecrypt,                        kccalgorithmaes128,                        kccoptionpkcs7padding|kccmodecbc,                        symmetrickey.bytes,                        kcckeysizeaes128,                        null,/*here u can provid iv byte array*/                        datain.bytes,                        datain.length,                        dataout.mutablebytes,                        dataout.length,                        &cryptbytes);      if (ccstatus != kccsuccess) {         nslog(@"cccrypt status: %d", ccstatus);     }      dataout.length = cryptbytes;      return dataout; }   - (nsdata *)aes128decryptwithkey:(nsstring *)key  datain:(nsdata *)datain   iv:(nsdata *)iv{     // 'key' should 32 bytes aes256, null-padded otherwise     char keyptr[kcckeysizeaes128+1]; // room terminator (unused) // oorspronkelijk 256     bzero(keyptr, sizeof(keyptr)); // fill zeroes (for padding)      // fetch key data     [key getcstring:keyptr maxlength:sizeof(keyptr) encoding:nsutf8stringencoding];      nsuinteger datalength = [datain length];      //see doc: block ciphers, output size less or     //equal input size plus size of 1 block.     //that's why need add size of 1 block here     size_t buffersize = datalength + kccblocksizeaes128;     void *buffer = malloc(buffersize);      size_t numbytesdecrypted = 0;     cccryptorstatus cryptstatus = cccrypt(kccdecrypt, kccalgorithmaes128, kccoptionpkcs7padding,                                           keyptr, kcckeysizeaes128, // oorspronkelijk 256                                           [iv bytes] /* initialization vector (optional) */,                                           [datain bytes], datalength, /* input */                                           buffer, buffersize, /* output */                                           &numbytesdecrypted);      if (cryptstatus == kccsuccess) {         //the returned nsdata takes ownership of buffer , free on deallocation         return [nsdata datawithbytesnocopy:buffer length:numbytesdecrypted];     }      free(buffer); //free buffer;     return nil; }   - (void)didreceivememorywarning {     [super didreceivememorywarning];     // dispose of resources can recreated. }  @end 

could please tell me i'm doing wrong , how work?

the same code works, ensure number of iterations , other parameters valid encrypted content.


Comments

Popular posts from this blog

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -

python 3.x - Mapping specific letters onto a list of words -