Cocoa AES加密NSData和Bytearrays

时间:2011-08-11 02:19:57

标签: cocoa encryption encoding aes nsdata

我正在使用以下代码加密cocoa中的文件:

- (NSData *)AES256EncryptWithKey:(NSString *)key
{
   // 'key' should be 32 bytes for AES256, will be null-padded otherwise
   char keyPtr[kCCKeySizeAES256 + 1]; // room for terminator (unused)
   bzero( keyPtr, sizeof( keyPtr ) ); // fill with zeroes (for padding)

   // fetch key data
   [key getCString:keyPtr maxLength:sizeof( keyPtr ) encoding:NSUTF8StringEncoding];

   NSUInteger dataLength = [self length];

       //See the doc: For block ciphers, the output size will always be less than or 
   //equal to the input size plus the size of one block.
   //That's why we need to add the size of one block here
   size_t bufferSize = dataLength + kCCBlockSizeAES128;
   void *buffer = malloc( bufferSize );

   size_t numBytesEncrypted = 0;
   CCCryptorStatus cryptStatus = CCCrypt( kCCEncrypt, kCCAlgorithmAES128,     kCCOptionPKCS7Padding,
                                      keyPtr, kCCKeySizeAES256,
                                      NULL /* initialization vector (optional) */,
                                      [self bytes], dataLength, /* input */
                                      buffer, bufferSize, /* output */
                                      &numBytesEncrypted );
   if( cryptStatus == kCCSuccess )
   {
      //the returned NSData takes ownership of the buffer and will free it on     deallocation
      return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
   }

   free( buffer ); //free the buffer
   return nil;
}

并为连接到文件写了这个:

    NSString *filePath = [[NSBundle mainBundle] pathForResource:@"foo" ofType:@"rtf"];
    NSData *data = [NSData dataWithContentsOfFile:filePath];
    NSString *key = [withFileKey stringValue];
    NSString *newStr = [[NSString alloc] initWithData:data     encoding:NSUTF8StringEncoding];

    NSString *encrypted = [newStr AES256EncryptWithKey:key];

    NSLog(@"File encryption:%@", encrypted);

    [filePathName setStringValue:filePath];


    if (!data) {

        NSLog(@"Unable to read file");

}

基本上我所做的就是首先获取用户想要的文件的文件路径。然后将文件中的数据转换为字符串。然后使用AES256EncryptWithKey:方法加密该字符串。但是,当我解密一个纯文本文件时,它会返回一堆像字体一样的垃圾和所有东西,然后是我编写的几行。像这样:

\ ansicpg1252 \ cocoartf1138 \ cocoasubrtf100 {\ fonttbl \ f0 \ fswiss \ fcharset0 Helvetica; \ f1 \ fnil \ fcharset0 Menlo-Bold;} {\ colortbl; \ red255 \ green255 \ blue255;} \ margl1440 \ margr1440 \ vieww10800 \ viewh8400 \ viewkind0 \ PARD \ tx720 \ tx1440 \ tx2160 \ tx2880 \ tx3600 \ tx4320 \ tx5040 \ tx5760 \ tx6480 \ tx7200 \ tx7920 \ tx8640 \ pardirnatural

\ f0 \ fs24 \ cf0您好我的名字是bobby bob \ \ \ PARD \ tx560 \ pardeftab560 \ pardirnatural

\ f1 \ b \ fs22 \ cf0 \ CocoaLigature0 YAY!\ 我真棒!}

我不应该获取数据然后加密(转换为字节),然后转换加密数据并将其转换为字符串以显示?我试过这样的东西,但它没有用。 :(

类似的东西:

NSData *encryptedData = [data AES256EncryptWithKey:yourkey];

然后:

NSString *convertData = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];

? 非常感谢您的帮助。谢谢!

1 个答案:

答案 0 :(得分:0)

您的代码显示为加密foo.rtf的硬编码。这看起来像一个RTF文件。您正在谈论的“纯文本文件”在哪里?

编辑我们就此进行了大量讨论,因此我写了一篇关于如何correctly use CCCrypt()的博文。