Iphone encryption not working

I have this encryption / decryption implemented and performed without errors, but I can not decrypt the data, and I'm not sure that the data is encrypted, kindly help.

- (NSData *)AES256EncryptWithKey:(NSString *)key {

char keyPtr[kCCKeySizeAES256+1]; 
bzero(keyPtr, sizeof(keyPtr));


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


NSUInteger dataLength = [strData length];
data = [[NSData alloc] init];
data = [strData dataUsingEncoding:NSUTF8StringEncoding];


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

size_t numBytesEncrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
                                      keyPtr, kCCKeySizeAES256,
                                      NULL /* initialization vector (optional) */,
                                      [data bytes], dataLength, /* input */
                                      [data bytes], bufferSize, /* output */
                                      &numBytesEncrypted);
if (cryptStatus == kCCSuccess) {
    return [NSData dataWithBytesNoCopy:data length:numBytesEncrypted];
}}

Decoding:

-(NSData *)AES256DecryptWithKey:(NSString *)key andForData:(NSData *)objDataObject {
char keyPtr[kCCKeySizeAES256+1];
bzero(keyPtr, sizeof(keyPtr)); 
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];

NSUInteger dataLength = [objDataObject length];
size_t bufferSize = dataLength + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);

size_t numBytesDecrypted = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,
                                      keyPtr, kCCKeySizeAES256,
                                      NULL,
                                      [objDataObject bytes], dataLength,
                                      [objDataObject bytes], bufferSize,
                                      &numBytesDecrypted);

if (cryptStatus == kCCSuccess) {
    return [NSData dataWithBytesNoCopy:objDataObject length:numBytesDecrypted];
}

}

I call the above methods as follows:

CryptoClass *obj = [CryptoClass new];
NSData *objData = [obj AES256EncryptWithKey:@"hell"];

NSData *val = [obj AES256DecryptWithKey:@"hell" andForData:objData ];

NSLog(@"decrypted string is : %@ AND LENGTH IS %d",[val description],[val length]);

Decryption does not occur at all, and encryption - I'm not sure about this, kindly help me here.

+3
source share
1 answer

I found out myself

full implementation that can execute ccats bytes:

http://pastie.org/966473

Thanks to everyone who helped.

+2
source

Source: https://habr.com/ru/post/1745573/


All Articles