如何在Objective C中实现laravel function Crypt :: encrypt()? [英] How to implement laravel function Crypt::encrypt() in Objective C?

查看:252
本文介绍了如何在Objective C中实现laravel function Crypt :: encrypt()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从 laravel 执行 Crypt :: ecrypt('123456'); Objective C iOS 。所以首先我将这种加密的Laravel方法扩展为纯php:

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

然后将该代码的每一行都逐步转换为客观c。观看我的目标c代码:

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

代码结果包含192或188个字符的字符串,没关系。但是在目的地使用此代码时,响应会显示错误:

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.  

我更改了一些代码,并测试了这种方法。所有的功能是正确的,但我认为 CCCrypt 不符合 openssl_encrypt 。因为当我从php获取 openssl_encrypt 的结果,并将其设置为 encStr 时,总结果正常工作。

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.

谢谢

推荐答案

已解决

最后,我们经过太多时间研究关于 Laravel 加密之后,决定自己创建。 LaraCrypt 解决了问题。尝试这样:

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'

使用Swift语言进行Laravel加密

成功。

这篇关于如何在Objective C中实现laravel function Crypt :: encrypt()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆