我想使用无填充的CFB加密创建AES 128,即
样本1:秘密数据是" HELLO WORLD" 键: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 明文(未加密):48 45 4C 4C 4F 20 57 4F 52 4C 44 加密文字:25 60 52 25 0B 90 06 AF 1C E6 2B
但我收到加密的十六进制字符串==即没有字符串。 我的加密成功但没有加密数据有什么建议吗?
这是代码:
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];
}
答案 0 :(得分:4)
我已经通过使用这种方法解决了这个问题
- (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];
}