请原谅我对 Objective-C 的缺乏经验。我只玩了几个星期。
我正在尝试测试 Apple 加密和解密数据的方法(在本例中为 NSString)。最终目标是让用户在文本区域中输入内容,然后对其进行加密。
我在 Xcode 中使用一个基本的单视图应用程序并添加到这两个文件中(从这里):
NSDataEncryption.h
#import <Foundation/Foundation.h>
@interface NSData (AES256)
- (NSData *)AES256EncryptWithKey:(NSString *)key;
- (NSData *)AES256DecryptWithKey:(NSString *)key;
@end
和 NSDataEncryption.m
#import "NSDataEncryption.h"
#import <CommonCrypto/CommonCryptor.h>
@implementation NSData (AES256)
- (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;
}
- (NSData *)AES256DecryptWithKey:(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 numBytesDecrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
keyPtr, kCCKeySizeAES256,
NULL /* initialization vector (optional) */,
[self bytes], dataLength, /* input */
buffer, bufferSize, /* output */
&numBytesDecrypted);
if (cryptStatus == kCCSuccess) {
//the returned NSData takes ownership of the buffer and will free it on deallocation
return [NSData dataWithBytesNoCopy:buffer length:numBytesDecrypted];
}
free(buffer); //free the buffer;
return nil;
}
@end
基于这个问题,我正在调用这样的方法:
#include "NSDataEncryption.h"
//...
NSString * key = @"ThisIsAKey";
NSDataEncryption *encryptionClass = [[NSDataEncryption alloc] init]; //Errors: "Use of undeclared identfier 'encryptionClass'" and "Use of undeclared identifer 'NSDataEncryption'"
NSData * newData = [encryptionClass AES256EncryptionWithKey:key]; //Errors: "Use of undeclared identfier 'encryptionClass'" and "Use of undeclared identifer 'NSDataEncryption'"
我试过把它放在 main() 和另一个类(ViewController)的新函数中:
- (IBAction)someFunctionName { code here }
大问题:为什么 Xcode 不接受 NSDataEncryption 作为一个类,也不让我调用它的函数 AES256EnryptionWithKey?我应该在应用程序的其他地方执行加密吗?
谢谢!