2012-07-14 32 views
13

Câu hỏi liên quan đến ứng dụng iOS5. Tôi có một bộ điều khiển xem nơi tôi có một số UITextFields. Tôi muốn mã hóa dữ liệu bằng AES-256.iOS 5: Mã hóa dữ liệu AES-256 EncryptWithKey: không tìm thấy

Thực tế, tôi không biết các gói tiền đề mà tôi phải thêm vào để thực hiện mã hóa và giải mã là gì. Tôi đã đi máng bài khác nhưng quá nhiều lời giải thích sai lầm nó lên.

Vui lòng cho tôi biết những gì và tất cả các gói, tập tin tiêu đề tôi phải bao gồm mã hóa dữ liệu sử dụng AES-256

Chandra

+0

Kiểm tra này ra: http: //stackoverflow.com/questions/1400246/a es-encryption-cho-an-nsstring-on-the-iphone – Adam

+0

@Adam: Các gói và tệp tiêu đề cần đưa vào không được liệt kê. – Chandu

+0

Câu trả lời này cung cấp giải pháp hoàn chỉnh: http://stackoverflow.com/a/5078432/730701 – Adam

Trả lời

37

giới thiệu một loại sau.

Câu hỏi thường gặp: Danh mục là gì?

Tóm lại, API Cocoa để thêm phương thức. mở rộng một thời gian ngắn.

biết thêm thông tin,

CustomizingExistingClasses

Category

File-New-Cocoa Touch - Objective-C loại

Nếu bạn muốn sử dụng một thể loại, lớp của bạn thêm #import "NSData + Encryption.h"

enter image description here

enter image description here

//NSData+Encryption.h

@interface NSData (Encryption) 
- (NSData *)AES256EncryptWithKey:(NSString *)key; 
- (NSData *)AES256DecryptWithKey:(NSString *)key; 
@end 

//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 
+8

+1 vì không rõ ràng. –

+0

là thông minh hay có cách nào để thực hiện việc này trực tiếp với nsstrings, không có nsdata liên quan? – Esqarrouth

+0

Tôi không thể nói đủ ấn tượng với giải pháp này. –

Các vấn đề liên quan