Objective-C AES 128未加密

时间:2015-01-12 15:01:25

标签: ios objective-c encryption

我使用AES 128加密文本“a”

我发现消息来源下面是互联网。

我尝试更改密钥但总是重复。

总是结果是8e4a3d4beb92d54c7e95f67d41daed59

NSString *key = @"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa";
//  NSString *key = @"00000000000000000000000000000000";
plainText = @"a";

cipherData = [ [ plainText dataUsingEncoding:NSASCIIStringEncoding] AES128EncryptWithKey:key];

NSString * str = [self hexEncode : cipherData ];
NSLog( @"str = %@", str );

- (NSData*) iAESEncrypt:(NSString *)key keySize:(int)keySize {
    if(key == nil)
        return nil;

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

    [key getCString: keyPtr maxLength: sizeof(keyPtr) encoding:NSUTF8StringEncoding];
    size_t numBytesEncrypted = 0x00;

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

    CCCryptorStatus result = CCCrypt( kCCEncrypt,
                                     kCCAlgorithmAES128,
                                     kCCOptionECBMode | kCCOptionPKCS7Padding,
                                     keyPtr,
                                     keySize,
                                     NULL /*iv*/,
                                     [self bytes], [self length],
                                     buffer, bufferSize,
                                     &numBytesEncrypted );

    if( result == kCCSuccess )
        return [NSData dataWithBytesNoCopy:buffer length:numBytesEncrypted];
    else
        NSLog(@"iAESEncrypt FAIL!");

    free(buffer);
    return nil;
}

-(NSString *) hexEncode:(NSData*)data
    {
        NSMutableString *hex = [NSMutableString string];
        unsigned char *bytes = (unsigned char *)[data bytes];
        char temp[3];
        NSUInteger i = 0;

        for (i = 0; i < [data length]; i++)
        {
            temp[0] = temp[1] = temp[2] = 0;
            (void)sprintf(temp, "%02x", bytes[i]);
            [hex appendString:[NSString stringWithUTF8String:temp]];
        }
        return hex; 
    }



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

    return [self iAESEncrypt:key keySize:kCCKeySizeAES128];
}

2 个答案:

答案 0 :(得分:0)

您的key变量有32个长度,可能是十六进制数据。首先,您必须将其转换为NSData,长度为kCCKeySizeAES128(16个字节)。

您可以在https://stackoverflow.com/a/7318062/296651使用hex decoder代码或感谢十六进制转换代码:

....
key = [key stringByReplacingOccurrencesOfString:@" " withString:@""];
NSMutableData *commandToSend= [[NSMutableData alloc] init];
unsigned char whole_byte;
char byte_chars[3] = {'\0','\0','\0'};
int i;
for (i=0; i < [key length]/2; i++) {
    byte_chars[0] = [key characterAtIndex:i*2];
    byte_chars[1] = [key characterAtIndex:i*2+1];
    whole_byte = strtol(byte_chars, NULL, 16);
    [commandToSend appendBytes:&whole_byte length:1];
}


size_t numBytesEncrypted = 0x00;

NSUInteger dataLength = [self length];
size_t     bufferSize = dataLength + kCCBlockSizeAES128;
void      *buffer     = malloc(bufferSize);
memset(buffer, 0, bufferSize);

CCCryptorStatus result = CCCrypt( kCCEncrypt,
                                 kCCAlgorithmAES128,
                                 kCCOptionECBMode | kCCOptionPKCS7Padding,
                                 [commandToSend bytes],
                                 keySize,
                                 NULL /*iv*/,
                                 [self bytes], [self length],
                                 buffer, bufferSize,
                                 &numBytesEncrypted );
....

答案 1 :(得分:0)

您总是使用空字符串进行加密。

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

sizeof(keyPtr)总是4 ... 使用

[key getCString: keyPtr maxLength: keySize encoding:NSUTF8StringEncoding];

getCString:返回一个BOOL结果..在你的情况下,它总是假的,但发布的代码不会检查它。

several AES实施,这是正确的。我可以建议使用AES256而不是128.它更安全。