1

I need to implement Crypt::ecrypt('123456'); from laravel to Objective C iOS. So first i expanded laravel method for encryption like this to pure php:

public function enc($text,$key)
    {
        $key = (string)base64_decode($key);
        $iv = random_bytes(16);
        $value = \openssl_encrypt(serialize($text), 'AES-256-CBC', $key, 0, $iv);  
        $bIv = base64_encode($iv);
        $mac = hash_hmac('sha256', $bIv.$value, $key); 
        $c_arr = ['iv'=>$bIv,'value'=>$value,'mac'=>$mac];
        $json = json_encode($c_arr);
        $crypted = base64_encode($json);

        return $crypted; 
    }

https://github.com/reza-khalafi/LaravelCrypt/blob/master/laravelEncrypt.php

And then convert every line of this code to objective c step by step. watch my objective c code:

#import <CommonCrypto/CommonHMAC.h>
#import <CommonCrypto/CommonCryptor.h>


// First convert Base64 strings to data
NSString *stringIn = @"123456";
NSString *ser = [NSString stringWithFormat:@"s:%lu:\"%@\";",(unsigned long)stringIn.length,stringIn];
NSData *dataIn     = [ser dataUsingEncoding:NSUTF8StringEncoding];

//Make iv
uint8_t randomBytes[16];
NSMutableString *ivStr;
int result = SecRandomCopyBytes(kSecRandomDefault, 16, randomBytes);
if(result == 0) {
    ivStr = [[NSMutableString alloc] initWithCapacity:16];
    for(NSInteger index = 0; index < 8; index++)
    {
        [ivStr appendFormat: @"%02x", randomBytes[index]];
    }
    NSLog(@"uuidStringReplacement is %@", ivStr);
} else {
    NSLog(@"SecRandomCopyBytes failed for some reason");
}
NSData *iv         = [[NSData alloc] initWithBase64EncodedString:ivStr options:0];

//Iv base 64
NSString *bIV = [[ivStr dataUsingEncoding:NSUTF8StringEncoding] base64EncodedStringWithOptions:0];

//Key
NSString *key = @"9OsNt7h7vjhvOwzXLfdQQYcHxYTua1Fk";
NSData *decodedKeyData = [[NSData alloc] initWithBase64EncodedString:key options:0];
NSString *keyStr = [[NSString alloc] initWithData:decodedKeyData encoding:NSISOLatin1StringEncoding];

//Encryption
size_t         encryptBytes = 0;
NSMutableData *encrypted  = [NSMutableData dataWithLength:ser.length + kCCBlockSizeAES128];
CCCrypt(
        kCCEncrypt,
        kCCAlgorithmAES128,
        kCCOptionPKCS7Padding, //CBC is the default mode
        decodedKeyData.bytes,
        kCCKeySizeAES128,
        iv.bytes,
        dataIn.bytes,
        dataIn.length,
        encrypted.mutableBytes,
        encrypted.length,
        &encryptBytes
        );

encrypted.length = encryptBytes;
NSLog(@"encrypted hex:    %@", encrypted);
NSString *encStr = [encrypted base64EncodedStringWithOptions:0];
NSLog(@"encrypted Base64: %@", encStr);


//Combine two string
NSString *mixStr = [NSString stringWithFormat:@"%@%@",bIV,encStr];

//cHMAC
const char *cKey  = [keyStr cStringUsingEncoding:NSISOLatin1StringEncoding];
const char *cData = [mixStr cStringUsingEncoding:NSASCIIStringEncoding];
unsigned char cHMAC[CC_SHA256_DIGEST_LENGTH];
CCHmac(kCCHmacAlgSHA256, cKey, strlen(cKey), cData, strlen(cData), cHMAC);
NSMutableString *mac = [NSMutableString string];
for (int i=0; i<sizeof cHMAC; i++){
    [mac appendFormat:@"%02hhx", cHMAC[i]];
}

//Make dictionary
NSDictionary *dic = @{@"iv":bIV,@"value":encStr,@"mac":mac};

//Json
NSError  * err;
NSData   * jsonData = [NSJSONSerialization dataWithJSONObject:dic options:0 error:&err];
NSString * myString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];

//Result
NSString *lastEnc = [[myString dataUsingEncoding:NSUTF8StringEncoding] base64EncodedStringWithOptions:0];

NSLog(@"%@ %lu",lastEnc,(unsigned long)lastEnc.length);

Code result contain string with 192 or 188 char and that is ok. but when using this code in destination, response show me an error:

Could not decrypt the data.  

I changes some code, and test this methods. all of the function is correct but i think CCCrypt not match with openssl_encrypt. because when i get result of openssl_encrypt from php and set it instead of encStr, total result works correctly.

Thank you

reza_khalafi
  • 6,230
  • 7
  • 56
  • 82

2 Answers2

1
 $value = \openssl_encrypt(serialize($text), 'AES-256-CBC', $key, 0, $iv);  

serialize function - it looks like php-specific and it works incorrect with non ASCII encodings. what if you replace it with base64_encode? and of course you will need to modify decryption code too.

heximal
  • 10,327
  • 5
  • 46
  • 69
0

Solved:
Finally we make it after too much time research about Laravel encryption, Decided to create by own. LaraCrypt solved the problem. try this:

pod 'LaraCrypt'

Laravel Encryption with Swift language

Be successful.

reza_khalafi
  • 6,230
  • 7
  • 56
  • 82