我有一大段文本,我使用Rijndael 128 CBC模式加密,并使用PHP中的base64进行编码(参见下面的代码)。此数据发布到网页上,然后由我的应用下载:
<?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;
?>
使用<NSURLConnectionDataDelegate>
我已经建立了一个协议,将加密和编码的数据下载到`downloadData(NSMutableData)中。然后在 - (void)connectionDidFinishLoading:(NSURLConnection *)连接我有:
-(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?
}