In ios 3des encryption contain lots of null termin

2020-05-01 02:27发布

问题:

i have used this function for 3des encryption.

 ccStatus = CCCrypt(kCCEncrypt,              // CCoperation op
                       kCCAlgorithm3DES,        // CCAlgorithm alg
                       kCCOptionPKCS7Padding,  // kCCOptionPKCS7Padding,                    //kCCModeECB,              // CCOptions
                       [_keyData bytes],        // const void *key
                       kCCKeySize3DES,          // 3DES key size length 24 bit
                       vinitVec,              //iv,                      // const void *iv,
                       [dTextIn bytes],         // const void *dataIn 
                       [dTextIn length],        // size_t dataInLength
                       bufferPtr,               // void *dataOut
                       bufferPtrSize,           // size_t dataOutAvailable
                       &movedBytes);            // size_t *dataOutMoved
       NSData *myData = [NSData dataWithBytes:(const void *)bufferPtr length:(NSUInteger)movedBytes];'

Although it is working. but when i convert convert this NSData to NSString , because the NSString contain lots of null termination,NSString end up on first null termination, the variable is not able to contain the whole data. but i have to send encrypted string on the server. what can i do to convert NSData to NSString. string that contain all data means(if the data contain null termination. the string will not end up in that case)?

Please help Thanks in advance.


thanks for reply , look if the encrypted byte contain

 char bytes[] = { 'H', 'e', 'l', 'l', 'o', \0, 'W', 'o', 'r', 'l', 'd', \0 };
   NSData *data = [NSData dataWithBytes:bytes length:sizeof(bytes)];
   NSString *str = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
   NSLog(@"%@", str);

   NSString *sendtoserver=[NSString stringwithformat:@"<request>%@</request>",str];

when we convert these NSData to NSString. it will end on first \0 ( null termination) because we have to send encrypted NSString.so it is making problem. and i can't send the base64string because server side don't want that.they were asking for encrypting string.

so what i do now , please help and thanks again for reply sir,

回答1:

Converting NSData to NSString does not stop at null bytes. A NSString can contain arbitrary Unicode characters, including embedded "NULL" characters.

Example:

char bytes[] = { 'H', 'e', 'l', 'l', 'o', 0, 'W', 'o', 'r', 'l', 'd', 0 };
NSData *data = [NSData dataWithBytes:bytes length:sizeof(bytes)];
NSString *str = [[NSString alloc] initWithData:data encoding:NSASCIIStringEncoding];
NSLog(@"%@", str);

Output:

Hello

So it looks as if the string contains only 5 characters and the conversion stopped at the first null byte. But that is only the NSLog output, in fact nothing is lost:

for (int i = 0; i < [str length]; i++) {
    unichar c = [str characterAtIndex:i];
    NSLog(@"%2d, %3d, %c", i, c, c);
}

Output:

  0,  72, H
  1, 101, e
  2, 108, l
  3, 108, l
  4, 111, o
  5,   0, 
  6,  87, W
  7, 111, o
  8, 114, r
  9, 108, l
 10, 100, d
 11,   0, 

So the string contains all data and nothing is lost. Probably the string is truncated later, when you send it to the server.


REMARK: Converting the encrypted data to a string seems problematic to me, because the data is interpreted in some character encoding. I have chosen NSASCIIStringEncoding in this example, but according to the documentation this encoding is only valid for ASCII values 0…127.

If you add more information on how and in which format the encrypted data is sent to the server, we might be able to make suggestions how so solve this better.