使用Objective C进行AES解密

时间:2012-05-22 11:57:11

标签: iphone objective-c ios encryption

我使用以下代码解密加密的文件,该文件使用JAVA应用程序加密。

Cipher.h文件

#import <Foundation/Foundation.h>
#import <CommonCrypto/CommonCryptor.h>
#import <CommonCrypto/CommonDigest.h>

@interface Cipher : NSObject {
    NSString *cipherKey;
}

@property (retain) NSString *cipherKey;

- (Cipher *) initWithKey:(NSString *) key;

- (NSData *) encrypt:(NSData *) plainText;
- (NSData *) decrypt:(NSData *) cipherText;

- (NSData *) transform:(CCOperation) encryptOrDecrypt data:(NSData *) inputData;  

+ (NSData *) md5:(NSString *) stringToHash; 

@end

Cipher.m文件

#import "Cipher.h"

@implementation Cipher 

@synthesize cipherKey;

- (Cipher *) initWithKey:(NSString *) key {  
    self = [super init];  
    if (self) {  
        [self setCipherKey:key];  
    }  
    return self;  
} 

- (NSData *) encrypt:(NSData *) plainText {  
    return [self transform:kCCEncrypt data:plainText];  
}  

- (NSData *) decrypt:(NSData *) cipherText {  
    NSData *returnData = [[NSData alloc] init];
    returnData = [self transform:kCCDecrypt data:cipherText];

    return returnData;  
}  

- (NSData *) transform:(CCOperation) encryptOrDecrypt data:(NSData *) inputData {  

    // kCCKeySizeAES128 = 16 bytes  
    // CC_MD5_DIGEST_LENGTH = 16 bytes  
    NSData* secretKey = [Cipher md5:cipherKey];  

    CCCryptorRef cryptor = NULL;  
    CCCryptorStatus status = kCCSuccess;  

    uint8_t iv[kCCBlockSizeAES128];  
    memset((void *) iv, 0x0, (size_t) sizeof(iv));  

    status = CCCryptorCreate(encryptOrDecrypt, kCCAlgorithmAES128, kCCOptionPKCS7Padding,  
                             [secretKey bytes], kCCKeySizeAES128, iv, &cryptor);  

    if (status != kCCSuccess) {  
        return nil;  
    }  

    size_t bufsize = CCCryptorGetOutputLength(cryptor, (size_t)[inputData length], true);  

    void * buf = malloc(bufsize * sizeof(uint8_t));  
    memset(buf, 0x0, bufsize);  

    size_t bufused = 0;  
    size_t bytesTotal = 0;  

    status = CCCryptorUpdate(cryptor, [inputData bytes], (size_t)[inputData length],  
                             buf, bufsize, &bufused);  

    if (status != kCCSuccess) {  
        free(buf);  
        CCCryptorRelease(cryptor);  
        return nil;  
    }  

    bytesTotal += bufused;  

    status = CCCryptorFinal(cryptor, buf + bufused, bufsize - bufused, &bufused);  

    NSLog(@"Status-3: %d", status);
    if (status != kCCSuccess) {
        free(buf);  
        CCCryptorRelease(cryptor);  
        return nil;  
    }  
    bytesTotal += bufused;  

    CCCryptorRelease(cryptor);  

    return [NSData dataWithBytesNoCopy:buf length:bytesTotal];  
}  

+ (NSData *) md5:(NSString *) stringToHash {  

    const char *src = [stringToHash UTF8String];  

    unsigned char result[CC_MD5_DIGEST_LENGTH];  

    CC_MD5(src, strlen(src), result);  

    return [NSData dataWithBytes:result length:CC_MD5_DIGEST_LENGTH];  
} 

@end

这是我解码加密数据的方式:

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory1 = [paths objectAtIndex:0];
    NSString *getImagePath = [documentsDirectory1 stringByAppendingPathComponent:@"Blue.jpg"];
    NSData *objNSData1 = [NSData dataWithContentsOfFile:getImagePath];

    Cipher *objCipher= [[Cipher alloc] initWithKey:@"1234567891234567"];

    NSData *decryptedData = [[NSData alloc] init];

    decryptedData = [objCipher decrypt:objNSData1];

    NSLog(@"%@", decryptedData);

    myImage.image = [UIImage imageWithData:decryptedData];

}

解密无法成功运行,并返回4304错误。

1 个答案:

答案 0 :(得分:2)

我知道这是由Java代码生成的。如果您对该代码有任何控制权,那么您应该了解它所使用的协议是非常不安全的。它没有正确生成密钥(MD5不是一个好的PBKDF),并且没有正确生成IV。再加上缺乏HMAC,它会遭受多种攻击。有关如何正确设置这些内容的详细信息,请参阅Properly encrypting with AES with CommonCrypto;有关示例实现,请参阅RNCryptor enter link description here

对于您的具体问题,您是否在解密此加密的内容时遇到问题,或者您在解密Java加密的内容时遇到问题?您可能与Java不匹配。

您应该检查更新或最终步骤中是否出现错误。如果更新步骤,那么您已经错误地配置了一些内容。如果是最后一步,您应该首先确保填充正确。文档的结尾应该是PKCS#7填充。这意味着它应该以下列序列之一结束(除非解密的大小可以被16整除):

01
02 02
03 03 03
04 04 04 04
...

整个加密数据的最终大小应该可以被16整除。