如何在Objective C中实现laravel函数Crypt :: encrypt()?

时间:2017-01-09 07:18:56

标签: php ios objective-c encryption aes

我需要从 laravel Objective C iOS 实施 Crypt :: ecrypt('123456'); 。所以首先我将用于加密的laravel方法扩展为纯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代码:

#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个字符串的字符串,这没关系。但在目的地使用此代码时,响应会显示错误:

Could not decrypt the data.  

我更改了一些代码,并测试了这些方法。所有功能都是正确的,但我认为 CCCrypt openssl_encrypt 不匹配。因为当我从php获得 openssl_encrypt 的结果并设置它而不是 encStr 时,总结果正常。

谢谢

2 个答案:

答案 0 :(得分:1)

serialize

y = 0函数 - 它看起来像特定于PHP,并且在非ASCII编码时工作不正确。如果用base64_encode替换它怎么办?当然,您也需要修改解密代码。

答案 1 :(得分:0)

<强>解决
最后我们经过太多时间研究 Laravel 加密,决定自己创建。 LaraCrypt 解决了这个问题。试试这个:

pod 'LaraCrypt'

Laravel Encryption with Swift language

成功。