我应该在哪里调用Objective-c iOS应用程序中的计算函数?
问题描述:
请原谅我对Objective-C的经验不足。我只玩了几个星期。我应该在哪里调用Objective-c iOS应用程序中的计算函数?
我想测试出苹果的加密和解密数据的方法(在这种情况下是一个NSString)。最终目标是让用户在文本区域输入内容,然后对其进行加密。
我使用Xcode的基本单视图应用和在这两个文件中加入(从here):
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
基于this question,我打电话的方法是这样的:
#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()和新的功能在里面另一个类(视图控制器):
- (IBAction)someFunctionName { code here }
大问题:为什么不接受Xcode中作为NSDataEncryption一类,也不会让我调用它的函数AES256EnryptionWithKey?我应该在应用程序的其他地方执行加密吗?
谢谢!
答
NSDataEncryption不是一个类。这是标准NSData类上的category。这意味着它使用两种方法'扩展'NSData类:- (NSData *)AES256EncryptWithKey:(NSString *)key;
和- (NSData *)AES256DecryptWithKey:(NSString *)key;
。这两个都返回NSData并将一个NSString作为参数。
你叫他们像这样:
NSData *dataToBeEncrypted = [NSData data]; //Put your data here
NSString *key = @"ThisISAKEy";
NSData *newData = [dataToBeEncrypted AES256EncryptionWithKey:key];
您可以使用这些方法来加密/解密的字符串:
- (NSData*) encryptString:(NSString*)plaintext withKey:(NSString*)key {
return [[plaintext dataUsingEncoding:NSUTF8StringEncoding] AES256EncryptWithKey:key];
}
- (NSString*) decryptData:(NSData*)ciphertext withKey:(NSString*)key {
return [[[NSString alloc] initWithData:[ciphertext AES256DecryptWithKey:key]
encoding:NSUTF8StringEncoding] autorelease];
}
答
NSData *data = [myString dataUsingEncoding:NSUTF8StringEncoding];
NSData *encrypted = [data AES256EncryptionWithKey:@"RandomString"];
你所说的 “数据” 是什么意思?被加密的东西是字符串,对吗?此外,我收到错误“没有可见@interface为'NSData'在最后一行声明选择器'AES256EncryptionWithKey'”。 – Orchid
这些方法返回从另一个NSData对象创建的NSData对象。如果你想加密一个字符串,你需要先将它转换为NSData。在你关联的论坛上有这样的例子。我将它们添加到我的帖子中。但是,真的,你应该先看看那里。还要确保你包含正确的头文件。 – Rengers
啊!谢谢你,我误解了一些基本的Objective-C,并没有意识到它。 (把[dataToBeEncrypted AES ...]像Java中的点运算符,对吗?dataToBeEncrypted.Method();)好像我仍然可能会丢失头文件,因为我仍然收到错误消息“”不可见@ 'NSData'接口声明选择器'AES256EncryptionWithKey“。 NSDataEncryption.h是我需要包含的唯一东西吗? (这个文件实际上是粘贴在我的项目中的。) – Orchid