2

我正在加密一些我想发送到服务器的文本,我在加密它并在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);
    });

});
4

0 回答 0