在Objective-C中加密解密nodejs

时间:2013-09-12 16:12:06

标签: objective-c node.js cryptography

我正在加密我要发送到服务器的一些文本,我在加密它时没有问题,并且在Objective-C中解密它但是当我将它发送到nodejs服务器时,通过解密它的结果永远不会加密数据总是一样的...我认为问题是我如何使用加密库,这是我的Xcode代码:

    NSString * key =@"1234567890123456";
NSString * url = @"http://flystory.herokuapp.com/register";


NSString *post = @"hola mundo!!!!!!!!!!";
NSData *postData = [post dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSLog(@"%@",[[NSString alloc] initWithData:postData encoding:NSASCIIStringEncoding]);
NSError *e;
CCCryptorStatus err;
postData = [postData dataEncryptedUsingAlgorithm:kCCAlgorithmAES128 key:key options:kCCOptionECBMode error:&err];
NSLog(@"%@",[[NSString alloc] initWithData:postData encoding:NSASCIIStringEncoding]);


NSString *postLength = [NSString stringWithFormat:@"%d", [postData length]];
NSMutableURLRequest *request = [[NSMutableURLRequest alloc] init];

[request setURL:[NSURL URLWithString:url]];
[request setHTTPMethod:@"post"];
[request setValue:postLength forHTTPHeaderField:@"Content-Length"];
[request setValue:@"body" forHTTPHeaderField:@"Content-Type"];
[request setHTTPBody:postData];

[NSURLConnection sendAsynchronousRequest:request
                                   queue:[NSOperationQueue mainQueue]
                       completionHandler:^(NSURLResponse *r, NSData *d, NSError *e) {
                           if (e) NSLog(@"%@",e.description);
                           else [self handleRespondedData:d];
                       }];

postData = [postData decryptedDataUsingAlgorithm:kCCAlgorithmAES128 key:key options:kCCOptionECBMode error:&err];
NSLog(@"%@",[[NSString alloc] initWithData:postData encoding:NSASCIIStringEncoding]);

加密我在https://github.com/Gurpartap/AESCrypt-ObjC

中使用NSData + CommonCrypto.h / m中包含的NSData扩展

我的Node.JS代码如下:

    var express = require("express");
    var app = express(express.bodyParser());
    //...
    app.post("*", function(request, response) {
var body = '';


    request.setEncoding('hex');
    request.on('data', function (data) {
        body += data;
        var crypto=require('crypto');
        var decipher=crypto.createDecipher('aes-128-ecb', '1234567890123456');
        decipher.setAutoPadding(auto_padding=false);
        var enc = decipher.update(body, 'hex', 'utf8') + decipher.final('utf8');            


        console.log('encrypted: ' + body);
        console.log('decrypted: ' + enc);

    });

    request.on('end', function () {

        // use POST
        route(handle, request.path, response, body);
    });

});

0 个答案:

没有答案
相关问题