問題描述
問題是關(guān)于iOS5 應(yīng)用程序.我有一個視圖控制器,其中有一些 UITextFields.我想使用 AES-256 加密數(shù)據(jù).
Question is about iOS5 application. I have a view controller where I have some UITextFields. I would like to encrypt data using AES-256.
事實上,我不知道我必須添加哪些必備包才能進(jìn)行加密和解密.我已經(jīng)瀏覽過其他帖子,但太多的解釋搞砸了.
In fact, I don't know what are the prerequisite packages that I have to add to do encryption and decryption. I have gone trough other posts but too much explanation messed it up.
請告訴我使用 AES-256 加密數(shù)據(jù)時必須包含的所有包和頭文件
Kindly let me know what and all packages, header files I have to include to encrypt data using AES-256
錢德拉
推薦答案
參考以下類別.
常見問題解答:什么是類別?
FAQ: What is a category?
簡而言之,Cocoa API 添加方法.簡要擴(kuò)展類.
In short, Cocoa API to add the method. briefly expand class.
更多信息,
自定義現(xiàn)有類
類別
File-New-Cocoa Touch - Objective-C 類
File-New-Cocoa Touch - Objective-C category
如果你想使用一個類別,你的類添加一個#importNSData+Encryption.h"
If you want to use a category, your class add a #import"NSData+Encryption.h"
//NSData+加密.h
//NSData+Encryption.h
@interface NSData (Encryption)
- (NSData *)AES256EncryptWithKey:(NSString *)key;
- (NSData *)AES256DecryptWithKey:(NSString *)key;
@end
//NSData+Encryption.m
//NSData+Encryption.m
#import "NSData+Encryption.h"
#import <CommonCrypto/CommonCryptor.h>
@implementation NSData (Encryption)
- (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
這篇關(guān)于iOS 5:數(shù)據(jù)加密 AES-256 EncryptWithKey:未找到的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!