I Want to Create AES 128 using CFB Encryption with

2019-01-15 20:19发布

问题:

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];
        }

回答1:

I have solved the by using this method

- (NSData *)AES128OperationWithEncriptionMode:(CCOperation)operation key:(NSData *)key iv:(NSData *)iv {


    CCCryptorRef cryptor = NULL;
    // 1. Create a cryptographic context.
    CCCryptorStatus status = CCCryptorCreateWithMode(operation, kCCModeCFB, kCCAlgorithmAES, ccNoPadding, [iv bytes], [key bytes], [key length], NULL, 0, 0, kCCModeOptionCTR_BE, &cryptor);

    NSAssert(status == kCCSuccess, @"Failed to create a cryptographic context.");

    NSMutableData *retData = [NSMutableData new];

    // 2. Encrypt or decrypt data.
    NSMutableData *buffer = [NSMutableData data];
    [buffer setLength:CCCryptorGetOutputLength(cryptor, [self length], true)]; // We'll reuse the buffer in -finish

    size_t dataOutMoved;
    status = CCCryptorUpdate(cryptor, self.bytes, self.length, buffer.mutableBytes, buffer.length, &dataOutMoved);
    NSAssert(status == kCCSuccess, @"Failed to encrypt or decrypt data");
    [retData appendData:[buffer subdataWithRange:NSMakeRange(0, dataOutMoved)]];

    // 3. Finish the encrypt or decrypt operation.
    status = CCCryptorFinal(cryptor, buffer.mutableBytes, buffer.length, &dataOutMoved);
    NSAssert(status == kCCSuccess, @"Failed to finish the encrypt or decrypt operation");
    [retData appendData:[buffer subdataWithRange:NSMakeRange(0, dataOutMoved)]];
    CCCryptorRelease(cryptor);

    return [retData copy];
}