与Objective-C加密混淆-出现警告/错误,如何修复?
我正在使用下面的代码块加密文本字符串。 此代码与AES256EncryptWithKey的方法有关,但当我运行项目并单击使用此代码的按钮时,会出现未捕获的异常和警告。警告说:“NSString可能不会响应:'-AES256EncryptWithKey'”。我能做什么?是否可以将一段处理异常的代码放入其中?还是完全不同与Objective-C加密混淆-出现警告/错误,如何修复?,objective-c,xcode,cocoa-touch,aes,Objective C,Xcode,Cocoa Touch,Aes,我正在使用下面的代码块加密文本字符串。 此代码与AES256EncryptWithKey的方法有关,但当我运行项目并单击使用此代码的按钮时,会出现未捕获的异常和警告。警告说:“NSString可能不会响应:'-AES256EncryptWithKey'”。我能做什么?是否可以将一段处理异常的代码放入其中?还是完全不同 NSString *Input = [Inputbox text]; [Input AES256encryptWithKey]; 以下是我使用的与AES256Encry
NSString *Input = [Inputbox text];
[Input AES256encryptWithKey];
以下是我使用的与AES256EncryptWithKey相关的代码:
@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];
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;
}
先谢谢你。链接、教程、答案和任何其他内容都非常感谢。应该说,错误似乎在第一行
@implementation NSString (AES256)
然后在您的方法中,使用
dataWithEncoding
方法将字符串键
转换为NSData
。这是因为NSString
实际上不响应该消息-您的代码向NSData
添加了一个类别
请记住,加密对二进制数据有效,因此您必须将潜在的unicodeNSString
转换为定义良好的二进制编码(例如UTF-8)
最快的转换是使用NSString
dataWithEncoding
方法。您编辑的版本也无法工作-NSString不包含字节
方法。好的,您是对的。再次编辑答案。好的,现在去睡觉。显然,我太累了。