2011-06-30 96 views
1

如何將NSMutableData轉換爲NSString?如何將NSMutableData轉換爲NSString?

NSString *strData = @"Bonjour tout le monde, je voudrais vous présenter la Société Futur"; 
NSLog(@"before encryption : %@",strData); 
NSMutableData *objNSData = [NSMutableData dataWithData:[strData dataUsingEncoding:NSUTF16StringEncoding]]; 

objNSData = [objNSData EncryptAES:@"samplekey"]; 
    ///NSLog(@"encryption : %@", objNSData); 
    NSString * strData1= [[NSString alloc] initWithData:objNSData encoding:NSUTF16StringEncoding]; 

NSLog(@"encryption : %@", strData1); 
+0

看到我的更新答案 –

+0

看到我的最新答案.... –

+0

THX兄弟,但我需要與AES256工作不是64 – djboss

回答

0

請參閱本Link

對於不同的編碼,還有其他方法也..

轉換的NSString NSData的

NSString* str= @"teststring"; 
NSData* data=[str dataUsingEncoding:NSUTF8StringEncoding]; 

更新

進行加密和解密,請參閱本Link和下載源代碼

+0

我用這個代碼,但是當我要保存在加密數據,我想解密我有很多例外。可我將NSString轉換爲NSMutabedata? – djboss

+0

vonversion的結果是nullNSString * key = @「my password」; \t NSString * secret = @「text to encrypt」; \t \t NSData * plain = [secret dataUsingEncoding:NSUTF8StringEncoding]; \t NSData * cipher = [plain AES256EncryptWithKey:key]; \t // NSString * strData1 = [[NSString alloc] initWithData:cipher encoding:NSASCIIStringEncoding]; \t NSString * strData1 = [[NSString alloc] initWithData:cipher encoding:NSUTF8StringEncoding]; – djboss

2

嘗試用下面

NSString * strData1= [[NSString alloc] initWithData:myMutableData encoding:NSUTF8StringEncoding]; 
+0

當我使用NSString * strData1 = [[NSString alloc] initWithData:myMutableData encoding:NSUTF8StringEncoding]時,我可以將NSString轉換爲NSMutableData – djboss

+0

;我有加密:(null) – djboss

+0

NSString * strData1 = [[NSString alloc] initWithData:objNSData encoding:NSUTF8StringEncoding]; NSLog(@「encryption:%@」,strData1);我有加密:(null) – djboss