如何解密PHP中的帖子数据

时间:2013-01-05 07:34:25

标签: php objective-c post encryption

我在Objective-c中有一些数据加密,代码如下:

NSDictionary *jsonDic = [NSDictionary dictionaryWithObjectsAndKeys:@"1", @"key1", @"2", @"key2", nil]; 
SBJsonWriter *jsonWriter = [[SBJsonWriter alloc] init];
NSData *data = [jsonWriter dataWithObject:jsonDic];
[jsonWriter release];

NSString *key = @"mykey";   
const int len = [data length]*2 + 2048;
char* buffer = (char*)malloc(len);
memset(buffer, 0, len);
const char *iv = "12345";
size_t dataOut = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, 
                                      kCCAlgorithmDES,
                                      kCCOptionPKCS7Padding,
                                      [key UTF8String], 
                                      kCCKeySizeDES,
                                      iv, 
                                      [data bytes],
                                      [data length],
                                      buffer, 
                                      len,
                                      &dataOut);

NSData* retData = nil;
if(cryptStatus == kCCSuccess) {
    retData = [NSData dataWithBytes:buffer length:dataOut];
}

free(buffer);
return destData;

然后调用以下代码发布到php api

NSString *urlString = @"http://domain/api.php";
NSMutableURLRequest *urlPost = [NSMutableURLRequest requestWithURL:[NSURL URLWithString:url] cachePolicy:NSURLRequestUseProtocolCachePolicy timeoutInterval:20];
[urlPost setHTTPMethod:@"POST"];
[urlPost setHTTPBody:data];
NSURLConnection *urlConnection = [NSURLConnection connectionWithRequest:urlPost delegate:self];

我使用下面的PHP代码来阅读和解密:

define("KEY","mykey");
define("IV","12345");

$post_body = file_get_contents('php://input');
$dec_str   = mcrypt_decrypt(MCRYPT_DES, KEY, $post_body, MCRYPT_MODE_BCB, IV);
error_log($dec_str);

但我看不出正确的结果。经过一天一夜的搜索和尝试,仍然没有找出什么错。 谢谢你!

1 个答案:

答案 0 :(得分:1)

你有这个电话:

CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, 
                                      kCCAlgorithmDES,
                                      kCCOptionPKCS7Padding,
                                      [keyString UTF8String], 
                                      kCCKeySizeDES,
                                      iv, 
                                      [data bytes],
                                      [data length],
                                      buffer, 
                                      len,
                                      &dataOut);

那里的keyString变量是什么?我想你应该在那里写[key UTF8String];)