dragon8899 2017-01-15 11:38
浏览 192

如何在iOS Objective-C中实现php openssl_encrypt()方法?

I want to implement php's openssl_encrypt() method in iOS Objective-C. Therefore I tried this code:

    #import <CommonCrypto/CommonHMAC.h>
    #import <CommonCrypto/CommonCryptor.h>
    - (void)viewDidLoad {
       [super viewDidLoad];
    NSData *dataIn     = [@"123456" dataUsingEncoding:NSISOLatin1StringEncoding];

    NSString *key = @"ygXa6pBJOWSAXXX/J6POVTjvJpMIiPAMQiTMjBrcOGw=";
    NSData *decodedKeyData = [[NSData alloc] initWithBase64EncodedString:key options:0];


    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(@"iv string is %@  %lu" , ivStr , ivStr.length);
    } else {
        NSLog(@"iv string failed for some reason");
    }

    NSData *iv         = [[NSData alloc] initWithBase64EncodedString:ivStr options:0];

    // setup key
    unsigned char cKeyR[kCCKeySizeAES256];
    bzero(cKeyR, sizeof(cKeyR));
    [decodedKeyData getBytes:cKeyR length:kCCKeySizeAES256];
    // setup iv
    char cIv[kCCBlockSizeAES128];
    bzero(cIv, kCCBlockSizeAES128);
    if (iv) {
        [iv getBytes:cIv length:kCCBlockSizeAES128];
    }
    // setup output buffer
    size_t bufferSize = [dataIn length] + kCCBlockSizeAES128;
    void *buffer = malloc(bufferSize);
    // do encrypt
    size_t encryptedSize = 0;
    CCCryptorStatus cryptStatus = CCCrypt(
                                          kCCEncrypt,
                                          kCCAlgorithmAES128,
                                          kCCOptionPKCS7Padding,
                                          cKeyR,
                                          kCCKeySizeAES192,
                                          cIv,
                                          [dataIn bytes],
                                          [dataIn length],
                                          buffer,
                                          bufferSize,
                                          &encryptedSize
                                          );

    NSData *encrypted = [NSData dataWithBytesNoCopy:buffer length:encryptedSize];
    NSString *encStr = [encrypted base64EncodedStringWithOptions:0]; 
}

But it is not same openssl_encrypt() method in php. I have checked iv, key and another methods. length and bytes output is correct but when use output in another method it is wrong.

  • 写回答

1条回答 默认 最新

  • dqm4675 2017-01-15 12:37
    关注
    1. decodedKeyData is 32 bytes )(256-bits) but the key size is specified as kCCKeySizeAES192.

    2. Just use randomBytes as the IV, there is no point in converting it is Base64 and back.

    评论

报告相同问题?

悬赏问题

  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?