Frage

I have to use Blowfish algorithm in my code for encryption and decryption. After calling the decryption method, blowfishDecrypt, I am getting the value in NSData but it give me always null when I convert it to NSString.

I am using the following code :

-(void)methodCalled
{
   syncTime=@"c7c937169084b20c3ff882dcda193a59";
   NSData* data = [syncTime dataUsingEncoding:NSUTF8StringEncoding];
   NSData* data2 = [@"R=U!LH$O2B#" dataUsingEncoding:NSUTF8StringEncoding];
   NSData* dycryptData=[self blowfishDecrypt:data usingKey:data2];

   // prints <0eec37b6 2b76c2df cdf72356 0f033ed8 d6bd37dd 5223bf66 5c318ebe 07f3cf71>
   NSLog(@"%@",dycryptData);

   NSString *dSync=[[NSString alloc] initWithBytes:[dycryptData bytes] 
                                     length:[dycryptData length] 
                                     encoding:NSUTF8StringEncoding];

   // prints (null)
   NSLog(@"Sync timeis %@",dSync);
} 



-(NSData *)blowfishDecrypt:(NSData *)messageData 
                  usingKey:(NSData *)secretKeyData {

    NSMutableData *decryptedData = [messageData mutableCopy];
    BLOWFISH_CTX ctx;
    Blowfish_Init (&ctx, (unsigned char*)[secretKeyData bytes], [secretKeyData length]);

    NSRange aLeftRange, aRightRange;
    NSData *aLeftBox, *aRightBox;
    unsigned long dl = 0, dr = 0;

    for (int i = 0; i< [decryptedData length]; i += 8) { // Divide data into octets...
        // …and then into quartets
        aLeftRange = NSMakeRange(i, 4);
        aRightRange = NSMakeRange(i + 4, 4);

        aLeftBox = [decryptedData subdataWithRange:aLeftRange];
        aRightBox = [decryptedData subdataWithRange:aRightRange];

        // Convert bytes into unsigned long
        [aLeftBox getBytes:&dl length:sizeof(unsigned long)];
        [aRightBox getBytes:&dr length:sizeof(unsigned long)];

        // Decipher
        Blowfish_Decrypt(&ctx, &dl, &dr);

        // Put bytes back
        [decryptedData replaceBytesInRange:aLeftRange withBytes:&dl];
        [decryptedData replaceBytesInRange:aRightRange withBytes:&dr];
    }

    return decryptedData;
}

Blowfish library code can be found eg. here

War es hilfreich?

Lösung

HINT#1 //general answer

NSString provides an initializer for this purpose. You can see more info using the docs here.

NSString * dSync = [[NSString alloc] initWithData: dycryptData 
                                      encoding:NSUTF8StringEncoding];

Assuming you use ARC.

HINT#2 // the answer for this particular question

I tried your code and confirm the above NSString conversion returns null. So why it is not working? dycryptData is stream of bytes represented as hex, so I tried the following and received the desired result:

int dycryptData_len = [dycryptData length];
NSMutableString *dSync_hex = [NSMutableString stringWithCapacity:dycryptData_len*2];
const unsigned char *dycryptData_bytes = [dycryptData bytes];
for (int i = 0; i < dycryptData_len; ++i) {
    [dSync_hex appendFormat:@"%02x", dycryptData_bytes[i]];
}

NSLog(@"dSync_hex=%@",dSync_hex);

I can see this result in log output:

dSync_hex=0eec37b62b76c2dfcdf723560f033ed8d6bd37dd5223bf665c318ebe07f3cf71

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top