dtcyv3985 2014-09-01 07:00
浏览 37

在PHP中加密字符串并在Objective C中解密它

I have a large piece of text which I have encrypted using Rijndael 128 CBC mode and encoded using base64 in PHP (see code below). This data is posted onto a webpage which is then downloaded by my app:

<?php
    $keyT = "keyString";
    $key = md5($key);
    $key_size =  strlen($key);    
    $plaintext = "Text_Block";

    $iv_size = mcrypt_get_iv_size(MCRYPT_RIJNDAEL_128, MCRYPT_MODE_CBC);
    $iv = mcrypt_create_iv($iv_size, MCRYPT_RAND);

    $ciphertext = mcrypt_encrypt(MCRYPT_RIJNDAEL_128, $key,
                             $plaintext, MCRYPT_MODE_CBC, $iv);

    # prepend the IV
    $ciphertext = $iv . $ciphertext;

    # encode using base64
    $ciphertext_base64 = base64_encode($ciphertext);

    $data = $ciphertext_base64;
?>

Using <NSURLConnectionDataDelegate> I have set up a protocol that downloads the encrypted and encoded data into `downloadData (NSMutableData). Then in the -(void) connectionDidFinishLoading:(NSURLConnection *) connection I have:

     -(void) connectionDidFinishLoading:(NSURLConnection *) connection {
          NSLog(@"Succesfully downloaded data! received %d bytes.", [downloadData length]);
          NSString *dataText = [[NSString alloc] initWithData:downloadData encoding:NSUTF8StringEncoding];
          NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:dataText options:0];
          // Now lets say I have the key created in the php file:
          char void *key = @"key goes here";
          // This is where it stops for me, how do I extract the IV (which should be the first 16 characters) and how do I use this to decrypt the data?
    }
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀
    • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
    • ¥15 关于#hadoop#的问题
    • ¥15 (标签-Python|关键词-socket)
    • ¥15 keil里为什么main.c定义的函数在it.c调用不了