I Want to Create AES 128 using CFB Encryption with No Padding i.e.
Sample 1: secret data is "HELLO WORLD" key:10 A5 88 69 D7 4B E5 A3 74 CF 86 7C FB 47 38 59 IV:00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 Clear text (unencrypted):48 45 4C 4C 4F 20 57 4F 52 4C 44 encrypted text:25 60 52 25 0B 90 06 AF 1C E6 2B
But I am getting Encrypted Hex String == i.e. no string. My encryption get success but no encrypted data Any suggestion?
here is code:
NSData *commandData = [@"HELLO WORLD" dataUsingEncoding:NSASCIIStringEncoding];
calling Method [commandData AES128OperationWithCreate:kCCEncrypt key:@"10a58869d74be5a374cf867cfb473859" iv:@"00000000000000000000000000000000"]
@implementation NSData (AES)
- (NSData *)AES128OperationWithCreate:(CCOperation)operation key:(NSString *)key iv:(NSString *)iv{
char ivPtr[kCCBlockSizeAES128 + 1];
bzero(ivPtr, sizeof(ivPtr));
if (iv) {
[iv getCString:ivPtr maxLength:sizeof(ivPtr) encoding:NSUTF8StringEncoding];
}
char keyPtr[kCCKeySizeAES128 + 1];
bzero(keyPtr, sizeof(keyPtr));
[key getCString:keyPtr maxLength:sizeof(keyPtr) encoding:NSUTF8StringEncoding];
CCCryptorRef cryptor = NULL;
CCCryptorStatus result = CCCryptorCreateWithMode(operation,
kCCModeCFB,
kCCAlgorithmAES128,
ccNoPadding,
ivPtr,
keyPtr,
kCCKeySizeAES128,
NULL,
0,
0,
0,
&cryptor);
if (result != kCCSuccess) {
NSLog(@"encryptAESCTRData: createWithMode error: %@", @(result));
result = CCCryptorRelease(cryptor);
return nil;
}
size_t bufferLength = CCCryptorGetOutputLength(cryptor, [self length], true);
NSMutableData *buffer = [NSMutableData dataWithLength:bufferLength];
NSMutableData *cipherData = [NSMutableData data];
size_t outLength;
result = CCCryptorUpdate(cryptor,
[self bytes],
[self length],
[buffer mutableBytes],
[buffer length],
&outLength);
// FIXME: Release cryptor and return error
if (result != kCCSuccess) {
NSLog(@"encryptAESCTRData: CCCryptorUpdate error: %@", @(result));
result = CCCryptorRelease(cryptor);
return nil;
}
result = CCCryptorFinal(cryptor,
[buffer mutableBytes],
[buffer length],
&outLength);
// FIXME: Release cryptor and return error
if (result != kCCSuccess) {
NSLog(@"encryptAESCTRData: CCCryptorFinal error: %@", @(result));
result = CCCryptorRelease(cryptor);
return nil;
}
[cipherData appendBytes:buffer.bytes length:outLength];
return cipherData;
//return [NSData dataWithBytesNoCopy:(__bridge void * _Nonnull)(buffer) length:outLength freeWhenDone:YES];
}
I have solved the by using this method