0

i need encrypt and decrypt with php a string code generated by that ios method explained here https://stackoverflow.com/a/9479657/721253

for encrypt with php i use that script

$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));

and on IOs for decript i use that

NSURL *url = [NSURL URLWithString:@"http://oscurodrago.it/tools/crypto.php"];
NSData *data = [NSData dataWithContentsOfURL:url];
NSData *outData = [self TripleDES:data encryptOrDecrypt:kCCDecrypt key:@"f968f8e82961489a8b14b345"];

ccStatus return decode error -> if (ccStatus == kCCDecodeError) NSLog( @"DECODE ERROR");

update that is viewcontroller.m where i'm testing that script

#import <CommonCrypto/CommonCryptor.h>
#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) & ~(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,
                       &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
Community
  • 1
  • 1
oscurodrago
  • 788
  • 1
  • 8
  • 18

1 Answers1

1

You have to make the 'outData' to base64 encoded string. Then only you can print the NSData. Try before that, data is not empty by printing the 'outData' length by

NSLog(@"%d",[outData length]);

See this link for converting NSData to base64 string.

rakeshNS
  • 4,227
  • 4
  • 28
  • 42
  • i'm not very good with ios but `[outData length]` return 0 and i tought http://stackoverflow.com/a/9479657/721253 already decrypt/encrypt base64 – oscurodrago Mar 22 '12 at 16:41
  • What did you get when printed NSLog(@"%d",[data length]); If this too 0 you are not getting any data from server. – rakeshNS Mar 22 '12 at 17:02
  • that is stamp of NSLog(@"%@",data); dosen't seem enpity 2012-03-22 18:06:38.973 Cryptdata[1012:f803] <39744d56 306b6a38 716d6e4e 44726f44 414a584c 36355777 34517676 6c6d6339 64566c56 42533973 7038306f 63796e69 75797979 425a6d4b 5377472f 7358706e 5a724546 38356f52 4b7a5269 38683639 65737a4c 476d6c65 78563048 524b3337> – oscurodrago Mar 22 '12 at 17:08