Saturday, 15 June 2013

objective c - ObjC AES128 Encryption with binary key -


I'm trying to encrypt data with AES128, I have the right to be treated as a key text string production meets, but only to treat the key as trying binary trash

implementation I'm using it:

  - (NSData *) AES128EncryptWithKey :. (NSString *) key {four keyPtr [kCCKeySizeAES256 + 1]; Berboo (keypets, keyboards); [Key getCString: keyPtr maxLength: sizeof (keyPtr) encoding: NSUTF8StringEncoding]; NSUntiger DataLong = [Self length]; Size_t BufferSecurity = DataLanguage + KCCBlockSizeAES128; Zero * buffer = molok (buffer size); NSLog (@ "keyPtr:% s", keyPtr); Size_t numBytesEncrypted = 0; CCCryptorStatus cryptStatus = CCCrypt (kCCEncrypt, kCCAlgorithmAES128, 0x00, keyPtr, kCCKeySizeAES256, zero, [self-bytes], dataLength, buffer, bufferSize, & amp; numBytesEncrypted); If (cryptStatus == kCCSuccess) {return [NSData dataWithBytesNoCopy: buffer length: numBytesEncrypted]; } Free (buffer); Return zero; }  

Encryption is actually what I need.

With my implementation I get a right encryption for the same plane key but I do not find a way to treat the key as the hex is not working as the NSData key To get the code tried to modify the code above but, I get garbage only after encryption. Of

I have searched and not found an answer on this specific topic, I hope I was right, TIA :)

I do not think you mean" hex ", which is a string representation. I think that you mean "binary", then just provide the key as NSData :

  - (NSData *) AES128EncryptWithKey: (NSData *) Key {NSAssert ([key length]> = kCCKeySizeAES256, @ "key is too short"); NSUntiger DataLong = [Self length]; Size_t BufferSecurity = DataLanguage + KCCBlockSizeAES128; NSMutableData * Encrypted = [NSMutableData dataWithCapacity: Buffer Size]; Size_t numBytesEncrypted = 0; CCCryptorStatus cryptStatus = CCCrypt (kCCEncrypt, kCCAlgorithmAES128, 0x00, [key bytes], kCCKeySizeAES256, zero, [self bytes], dataLength, [encrypted mutableBytes], bufferSize, & amp; numBytesEncrypted; If (cryptStatus == kCCSuccess) {[encrypted setLength: numBytesEncrypted]; Encrypted return; } Return zero; }  

Note:. Your code contained a memory leak and I think you were not returning encrpypted data properly


No comments:

Post a Comment