在ios 3des加密包含大量的null终止。当我将NSData转换为NSString时,将终止到第一个空终止? [英] In ios 3des encryption contain lots of null termination.when i convert NSData to NSString , it will endup to first null termination?

查看:161
本文介绍了在ios 3des加密包含大量的null终止。当我将NSData转换为NSString时,将终止到第一个空终止?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  ccStatus = CCCrypt(kCCEncrypt,// CCoperation op 
kCCAlgorithm3DES,// CCAlgorithm alg
kCCOptionPKCS7Padding,// kCCOptionPKCS7Padding,// kCCModeECB,// CCOptions
[_keyData bytes],// const void * key
kCCKeySize3DES,// 3DES key size length 24位
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];'

虽然它正在工作。但是当我把这个NSData转换成NSString时,因为NSString包含很多空终止,NSString会在第一个空终止结束,所以变量不能包含整个数据。但是我必须在服务器上发送加密的字符串。我可以做什么来将NSData转换为NSString。包含所有数据的字符串意味着(如果数据包含空终止,字符串不会在这种情况下结束)?



请帮助
提前感谢。




感谢您的回复,
查看加密字节是否包含

  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];当我们将这些NSData转换成NSString时,

它将在第一个\0(空终止)
结束,因为我们必须发送加密的NSString.so它正在产生问题。我不能发送base64string,因为服务器端不想要那个。他们要求加密字符串。



所以我现在做的,请帮助
并再次感谢你的回复先生,

解决方案

NSData code> NSString 不停止在空字节。一个 NSString 可以包含任意的Unicode字符,包括嵌入的NULL字符。



示例:

  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);

输出:

  Hello 

所以它 em>如果字符串只包含5个字符,并且转换在第一个空字节停止。但是这只是 NSLog 输出,实际上没有什么会丢失:

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

输出:

  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,

所以字符串包含所有数据,没有丢失。稍后,当您将字符串发送到服务器时,字符串可能会被截断。






备注:将加密的数据转换为字符串似乎对我来说是有问题的,因为数据以某种字符编码解释。在本例中,我选择了 NSASCIIStringEncoding ,但根据文档,该编码仅对ASCII值0 ... 127有效。



如果您添加关于加密数据发送到服务器的方式和格式的更多信息,我们可能会提出如何更好地解决这些问题的建议。


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,

解决方案

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.

这篇关于在ios 3des加密包含大量的null终止。当我将NSData转换为NSString时,将终止到第一个空终止?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆