title: php + ios encrypt and decrypt 3des + base64
body: <p>i need encrypt and decrypt with php a string code generated by that ios method explained here <a href="https://stackoverflow.com/a/9479657/721253">https://stackoverflow.com/a/9479657/721253</a></p>
  
  <p>for encrypt with php i use that script</p>
  
  <pre><code>$key = 'f968f8e82961489a8b14b345';
  $data = 'odio quando sto studiando e un velociraptor mi lancia addosso banane';
  $encrypted = null;
  $m = mcrypt_module_open(MCRYPT_3DES, null, MCRYPT_MODE_ECB, null);
  $fake_iv = str_repeat(chr(0), mcrypt_enc_get_iv_size($m));
  mcrypt_generic_init($m, $key, $fake_iv);
  $encrypted = mcrypt_generic($m, $data);
  die(base64_encode($encrypted));
  </code></pre>
  
  <p>and on IOs for decript i use that</p>
  
  <pre><code>NSURL *url = [NSURL URLWithString:@"http://oscurodrago.it/tools/crypto.php"];
  NSData *data = [NSData dataWithContentsOfURL:url];
  NSData *outData = [self TripleDES:data encryptOrDecrypt:kCCDecrypt key:@"f968f8e82961489a8b14b345"];
  </code></pre>
  
  <p>ccStatus return decode error   ->  if (ccStatus == kCCDecodeError) NSLog( @"DECODE ERROR");</p>
  
  <p><strong>update</strong>
  that is viewcontroller.m where i'm testing that script</p>
  
  <pre><code>#import &lt;CommonCrypto/CommonCryptor.h&gt;
  #import "GTMBase64.h"
  
  #import "ViewController.h"
  
  @interface ViewController ()
  
  @end
  
  @implementation ViewController
  
  - (NSData*)TripleDES:(NSData*)plainData encryptOrDecrypt:(CCOperation)encryptOrDecrypt key:(NSString*)key {
  
      const void *vplainText;
      size_t plainTextBufferSize;
  
      if (encryptOrDecrypt == kCCDecrypt)
      {
          NSData *EncryptData = [GTMBase64 decodeData:plainData];
          plainTextBufferSize = [EncryptData length];
          vplainText = [EncryptData bytes];
      }
      else
      {
          plainTextBufferSize = [plainData length];
          vplainText = (const void *)[plainData bytes];
      }
  
      CCCryptorStatus ccStatus;
      uint8_t *bufferPtr = NULL;
      size_t bufferPtrSize = 0;
      size_t movedBytes = 0;
      // uint8_t ivkCCBlockSize3DES;
  
      bufferPtrSize = (plainTextBufferSize + kCCBlockSize3DES) &amp; ~(kCCBlockSize3DES - 1);
      bufferPtr = malloc( bufferPtrSize * sizeof(uint8_t));
      memset((void *)bufferPtr, 0x0, bufferPtrSize);
      // memset((void *) iv, 0x0, (size_t) sizeof(iv));
  
      //    NSString *key = @"123456789012345678901234";
      NSString *initVec = @"init Vec";
      const void *vkey = (const void *) [key UTF8String];
      const void *vinitVec = (const void *) [initVec UTF8String];
  
      ccStatus = CCCrypt(encryptOrDecrypt,
                         kCCAlgorithm3DES,
                         kCCOptionPKCS7Padding,
                         vkey, //"123456789012345678901234", //key
                         kCCKeySize3DES,
                         vinitVec, //"init Vec", //iv,
                         vplainText, //"Your Name", //plainText,
                         plainTextBufferSize,
                         (void *)bufferPtr,
                         bufferPtrSize,
                         &amp;movedBytes);
      if (ccStatus == kCCSuccess) NSLog(@"SUCCESS");
      else if (ccStatus == kCCParamError) NSLog( @"PARAM ERROR");
       else if (ccStatus == kCCBufferTooSmall) NSLog( @"BUFFER TOO SMALL");
       else if (ccStatus == kCCMemoryFailure) NSLog( @"MEMORY FAILURE");
       else if (ccStatus == kCCAlignmentError) NSLog( @"ALIGNMENT");
       else if (ccStatus == kCCDecodeError) NSLog( @"DECODE ERROR");
       else if (ccStatus == kCCUnimplemented) NSLog( @"UNIMPLEMENTED");
  
      NSData *result;
  
      if (encryptOrDecrypt == kCCDecrypt)
      {
          result = [NSData dataWithBytes:(const void *)bufferPtr length:(NSUInteger)movedBytes];
      }
      else
      {
          NSData *myData = [NSData dataWithBytes:(const void *)bufferPtr length:(NSUInteger)movedBytes];
          result = [GTMBase64 encodeData:myData];
      }
  
      return result;
  }
  
  
  - (void)viewDidLoad
  {
      [super viewDidLoad];
      // Do any additional setup after loading the view, typically from a nib.
  
      NSURL *url = [NSURL URLWithString:@"http://www.oscurodrago.it/tools/crypto.php"];
      NSData *data = [NSData dataWithContentsOfURL:url];
  
      NSData *outData = [[NSData alloc] initWithData:[self TripleDES:data encryptOrDecrypt:kCCDecrypt key:@"f968f8e82961489a8b14b345"]];
  
      NSLog(@"%@", data );
  
      // NSData *encriptdata = [self TripleDES:data encryptOrDecrypt:kCCEncrypt key:@"f968f8e82961489a8b14b345"];
  
      NSLog(@"%@", [outData length] );
  }
  
  - (void)viewDidUnload
  {
      [super viewDidUnload];
      // Release any retained subviews of the main view.
  }
  
  - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  {
      if ([[UIDevice currentDevice] userInterfaceIdiom] == UIUserInterfaceIdiomPhone) {
          return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
      } else {
          return YES;
      }
  }
  
  @end
  </code></pre>