How to implement laravel Crypt :: encrypt () function in Objective-C?

I need to implement Crypt :: ecrypt ('123456'); from laravel to Objective-C iOS . So first, I extended the 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 each line of this code to target c step by step. look at my c object 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); 

The result of the code contains a string with 192 or 188 char, and this is normal. but when using this code in the destination, the answer will show me an error:

 Could not decrypt the data. 

I am changing the code and checking out these methods. all functions are correct, but I think CCCrypt does not match openssl_encrypt . because when I get the openssl_encrypt result from php and set it instead of encStr , the final result works correctly.

thanks

+1
source share
2 answers

solvable :
Finally, we do this after too much research regarding Laravel encryption, we decided to create it ourselves. LaraCrypt solved the problem. try the following:

 pod 'LaraCrypt' 

Laravel encryption with Swift language

Be successful.

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

serialize function - it looks like php-specific, and it does not work correctly with encoding without ASCII. what if you replace it with base64_encode? and of course, you will also need to change the decryption code.

+1
source

Source: https://habr.com/ru/post/1233078/


All Articles