サーバーに送信したいテキストを暗号化していますが、問題なく暗号化し、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);
});
});