2012-07-14 12 views
13

La pregunta es sobre la aplicación iOS5. Tengo un controlador de vista donde tengo algunos UITextFields. Me gustaría encriptar datos usando AES-256.iOS 5: cifrado de datos AES-256 EncryptWithKey: no encontrado

De hecho, no sé cuáles son los requisitos previos que tengo que agregar para hacer el cifrado y el descifrado. He ido a través de otros mensajes, pero demasiada explicación lo estropeó.

me dejan amablemente saber qué y todos los paquetes, los archivos de cabecera tengo que incluir para cifrar los datos usando un algoritmo AES-256

Chandra

+0

mira esto: http: //stackoverflow.com/questions/1400246/a es-encryption-for-an-nsstring-on-the-iphone – Adam

+0

@Adam: Los paquetes y archivos de encabezado que se incluirán no están en la lista. – Chandu

+0

Esta respuesta proporciona una solución completa: http://stackoverflow.com/a/5078432/730701 – Adam

Respuesta

37

refieren una categoría siguiente.

Preguntas frecuentes: ¿Qué es una categoría?

En resumen, Cocoa API para agregar el método. ampliar brevemente la clase.

Más información,

CustomizingExistingClasses

Category

Archivo-Nuevo-Cocoa Touch - Objective-C categoría

Si desea utilizar una categoría, su clase agrega un #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 por ser innecesariamente claro. –

+0

¿es inteligente, o hay una forma de hacerlo directamente con nsstrings, sin nsdata involucrado? – Esqarrouth

+0

No puedo decir lo impresionado que estoy con esta solución. –

Cuestiones relacionadas