Closed whisper-bye closed 8 years ago
You should be using TLS instead.
On Sat, Apr 30, 2016 at 10:58 AM, whisper-bye notifications@github.com wrote:
I created an app with CocoaAsyncSocket, I want to decrypt the data form the server
func socket(sock: GCDAsyncSocket!, didReadData data: NSData!, withTag tag: Int) { // here to decrypt... }
everything works fine, but often garbled
I am using CCCrypt method, I guess it is because the read block size is incorrect, the didReadData method don't read data complete at once.
my question is, what the correct way to decrypt CocoaAsyncSocket with AES-256-CFB mode
— You are receiving this because you are subscribed to this thread. Reply to this email directly or view it on GitHub https://github.com/robbiehanson/CocoaAsyncSocket/issues/425
@chrisballinger Thanks for your answer, But I am using a private protocol, the server returns encrypted data. I just want to know the right way to decrypt the data, Could you please give me some advice? thx!
This is what I do (Objective-C and RNCryptor, with AES256):
(void)socket:(GCDAsyncSocket )sock didReadData:(NSData )data withTag:(long)tag { dispatch_async(dispatch_get_main_queue(), ^{ @autoreleasepool {
NSData *strData = [data subdataWithRange:NSMakeRange(0, [data length] - 2)]; // strip CR LF
NSData *no64Data = [[NSData alloc] initWithBase64EncodedData:strData options:nil];
NSError *error;
NSData *decryptedData = [RNDecryptor decryptData:no64Data
withPassword:self.passwordStr
error:&error];
if (error) {
NSLog(@"DECRYPTION: %@", [error localizedDescription]);
}
// NSLog(@"decrypted data: %@", decryptedData);
// NSString *str = [[NSString alloc] initWithData:decryptedData encoding:NSUTF8StringEncoding];
// NSLog(@"decrypted string: %@", str);
if(decryptedData.length > 0)
{
NSError* error;
NSDictionary* dictionary = [NSJSONSerialization
JSONObjectWithData:decryptedData
options:kNilOptions
error:&error];
// Work on your data here
// ....
// Ready for next message
[self writeToSocket:sock dataString:@"OK" withTag:ECHO_MSG];
}
else
{
// Error
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
@"No valid data. Check your encryption options.", @"Message",
@NO, @"Success",
nil];
[self writeToSocket:sock dataString:[dict JSONStringRepresentation] withTag:ECHO_MSG];
}
}
}); }
(void)writeToSocket:(GCDAsyncSocket )sock dataString:(NSString )str withTag:(long)tag { NSMutableData data = nil; NSError error; data = [NSMutableData dataWithData:[RNEncryptor encryptData:[str dataUsingEncoding:NSUTF8StringEncoding] withSettings:kRNCryptorAES256Settings password:self.passwordStr error:&error]]; if (error) { NSLog(@"ENCRYPTION: %@", [error localizedDescription]); return; } data = [NSMutableData dataWithData:[data base64EncodedDataWithOptions:nil]]; [data appendData:[@"\r\n" dataUsingEncoding:NSUTF8StringEncoding]];
[sock writeData:data withTimeout:-1 tag:tag]; }
Best regards, Dirk
Am 30.04.2016 um 21:42 schrieb whisper-bye notifications@github.com<mailto:notifications@github.com>:
@chrisballingerhttps://github.com/chrisballinger Thanks for your answer, But I am using a private protocol, the server returns encrypted data. I just want to know the right way to decrypt the data, Could you please give me some advice? thx!
— You are receiving this because you are subscribed to this thread. Reply to this email directly or view it on GitHubhttps://github.com/robbiehanson/CocoaAsyncSocket/issues/425#issuecomment-215989633
thx all, I have figured it out, because I release the context
I created an app with CocoaAsyncSocket, I want to decrypt the data form the server
everything works fine, but often garbled
I am using CCCrypt method, I guess it is because the read block size is incorrect, the didReadData method don't read data complete at once.
my question is, what the correct way to decrypt CocoaAsyncSocket with AES-256-CFB mode