gpt4 book ai didi

objective-c - 在 Objective-c iOS 应用程序中,我应该在哪里调用计算函数?

转载 作者:行者123 更新时间:2023-11-29 04:35:18 30 4
gpt4 key购买 nike

请原谅我对 Objective-C 缺乏经验。我只玩了几个星期。

我正在尝试测试 Apple 的数据加密和解密方法(在本例中为 NSString)。最终目标是让用户在文本区域中输入内容,然后对其进行加密。

我在 Xcode 中使用基本的单 View 应用程序,并添加到这两个文件中(来自 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() 和另一个类(ViewController)的新函数中:

- (IBAction)someFunctionName { code here }

大问题:为什么 Xcode 不接受 NSDataEncryption 作为类,也不让我调用它的函数 AES256EnryptionWithKey?我应该在应用程序的其他地方执行加密吗?

谢谢!

最佳答案

NSDataEncryption 不是一个类。这是category在标准 NSData 类上。这意味着它使用两种方法“扩展”了 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];
}

关于objective-c - 在 Objective-c iOS 应用程序中,我应该在哪里调用计算函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11137978/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com