我正在使用下面的代碼塊來加密文本字符串。 此代碼與AES256EncryptWithKey的方法相關,但是當我運行該項目時,單擊使用此代碼的按鈕時,會出現未捕獲的異常和警告。警告說:「NSString可能不響應:'-AES256EncryptWithKey'」。我能做什麼?是否可以在處理異常中放入一段代碼?或者是完全不同的東西?Objective-C Encryption混淆 - 有警告/錯誤,我該如何解決這個問題?
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;
}
感謝信提前。鏈接,教程,答案和其他任何東西都表示讚賞。
謝謝你的幫助,那應該解決我的問題! –