Pregunta sobre ios, iphone, encryption, objective-c – ¿Cómo cifrar el valor de NSString utilizando DES en iPhone?

7

Utilicé el siguiente código para cifrar el valor de NSString con la clave,

NSString *token = @"encryptValue";
NSString *key = @"encryptKey";

NSData * keyData = [key dataUsingEncoding: NSUTF8StringEncoding];
NSData * plainData = [token dataUsingEncoding: NSUTF8StringEncoding];
NSMutableData * encData = [NSMutableData dataWithLength: plainData.length + 256];
size_t bytesWritten = 0;
CCCryptorStatus ccStatus = CCCrypt (kCCEncrypt,
                    kCCAlgorithmDES,
                    kCCOptionPKCS7Padding,
                    [keyData bytes], 
                    kCCKeySizeDES,
                    NULL,
                    [plainData bytes],
                    [plainData length],
                    encData.mutableBytes,
                    encData.length,
                    &bytesWritten);
encData.length = bytesWritten;    
NSString *encrypted = [[NSString alloc]  initWithBytes:[encData bytes] length:[encData length] encoding: NSUTF8StringEncoding];  
NSLog(@"ENCRYPT : %@",encrypted); 

Pero estoy consiguiendo (nulo) es el resultado. ¿Hay algún problema? ¿Cómo cifrar el valor de NSString?

Tu respuesta

2   la respuesta
10

probablemente para DES sería similar, eche un vistazo esto funciona conmigo ... Crear una nueva categoríaAES enNSData
porNSData + AES.h

#import <Foundation/Foundation.h>
#import <CommonCrypto/CommonCryptor.h>

@interface NSData (AES)

- (NSData*) EncryptAES: (NSString *) key;
- (NSData *) DecryptAES: (NSString *) key;

@end

porNSData + AES.m

    #import "NSData+AES.h"
    @implementation NSData(AES)

    - (NSData*) EncryptAES: (NSString *) key
    {
        char keyPtr[kCCKeySizeAES256+1];
        bzero( keyPtr, sizeof(keyPtr) );

        [key getCString: keyPtr maxLength: sizeof(keyPtr) encoding: NSUTF16StringEncoding];
        size_t numBytesEncrypted = 0;

        NSUInteger dataLength = [self length];

        size_t bufferSize = dataLength + kCCBlockSizeAES128;
        void *buffer = malloc(bufferSize);

        CCCryptorStatus result = CCCrypt( kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
                                         keyPtr, kCCKeySizeAES256,
                                         NULL,
                                         [self bytes], [self length],
                                         buffer, bufferSize,
                                         &numBytesEncrypted );

        if( result == kCCSuccess )
            return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];

        return nil;
    }

    - (NSData *) DecryptAES: (NSString *) key
    {
        char  keyPtr[kCCKeySizeAES256+1];
        bzero( keyPtr, sizeof(keyPtr) );

        [key getCString: keyPtr maxLength: sizeof(keyPtr) encoding: NSUTF16StringEncoding];

        size_t numBytesEncrypted = 0;

        NSUInteger dataLength = [self length];

        size_t bufferSize = dataLength + kCCBlockSizeAES128;
        void *buffer_decrypt = malloc(bufferSize);    

        CCCryptorStatus result = CCCrypt( kCCDecrypt , kCCAlgorithmAES128, kCCOptionPKCS7Padding,
                                         keyPtr, kCCKeySizeAES256,
                                         NULL,
                                         [self bytes], [self length],
                                         buffer_decrypt, bufferSize,
                                         &numBytesEncrypted );

        if( result == kCCSuccess )
        return [NSData dataWithBytesNoCopy:buffer_decrypt length:numBytesEncrypted];

        return nil;
    }

    @end

Uso: ImportarNSData + AES.h

//Encryption
NSString *myString = @"This is the content";
NSString *key = @"my secret key";
NSData *data = [myString dataUsingEncoding:NSUTF8StringEncoding];
NSData *encryptedData = [data EncryptAES:key];

//Decryption
NSString *decryptedString = [[[NSString alloc] initWithData:[encryptedData DecryptAES:key] encoding:NSUTF8StringEncoding] autorelease]; // remove autorelease if using ARC
bien, pero su código podría perder búfer si el resultado! = kCCSuccess xingzhi.sg
Gracias por tu respuesta. ¿Dónde uso la clave secreta? bharath
Actualizaré mi respuesta para mostrarles el uso en un minuto. graver
Listo echar un vistazo graver
Gracias @graver. Lo he hecho de alguna otra manera. Gracias de nuevo por su valiosa respuesta. bharath
4

en.

- (NSString*) doCipher:(NSString*)encryptValue {

    const void *vplainText;
    size_t plainTextBufferSize = [encryptValue length];
    vplainText = (const void *) [encryptValue UTF8String];    
    CCCryptorStatus ccStatus;
    uint8_t *bufferPtr = NULL;
    size_t bufferPtrSize = 0;
    size_t movedBytes = 0;

    bufferPtrSize = (plainTextBufferSize + kCCBlockSizeDES) & ~(kCCBlockSizeDES - 1);
    bufferPtr = malloc( bufferPtrSize * sizeof(uint8_t));
    memset((void *)bufferPtr, 0x0, bufferPtrSize);

    Byte iv [] = {0x12, 0x34, 0x56, 0x78, 0x90, 0xab, 0xcd, 0xef};

    NSString *key = @"encryptKey";
    const void *vkey = (const void *) [key UTF8String];

    ccStatus = CCCrypt(kCCEncrypt,
                       kCCAlgorithmDES,
                       kCCOptionPKCS7Padding,
                       vkey, 
                       kCCKeySizeDES,
                       iv,
                       vplainText,
                       plainTextBufferSize,
                       (void *)bufferPtr,
                       bufferPtrSize,
                       &movedBytes);

    NSData *myData = [NSData dataWithBytes:(const void *)bufferPtr length:(NSUInteger)movedBytes];
    NSString *result = [myData base64Encoding]; 

   return result;
}
y como se desencripta? Hlung

Preguntas relacionadas