使用ObjectiveC进行AES解密 [英] AES Decryption using ObjectiveC

查看:48
本文介绍了使用ObjectiveC进行AES解密的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用以下代码对加密的文件进行解密,该文件是使用JAVA应用程序加密的.

I use following code to decrypt a file encrypted, the file was encrypted using a JAVA application.

Cipher.h文件

Cipher.h file

#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文件

Cipher.m file

#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

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

This is how I decode the encrypted data:

- (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 错误.

Decryption isn't working successfully and it returns 4304 error.

推荐答案

我知道这是Java代码生成的.如果您对该代码有任何控制权,那么您应该了解它所使用的协议是非常不安全的.它不能正确生成密钥(MD5不是好的PBKDF),并且不能正确生成IV.再加上缺少HMAC,它容易受到多种攻击.有关如何正确设置这些密码的完整详细信息,请参见使用AES和CommonCrypto正确加密.RNCryptor 在此处输入链接描述以获取示例实现.

I know this was generated by Java code. If you have any control over that code, then you should understand that the protocol it's using is highly insecure. It does not correctly generate a key (MD5 is not a good PBKDF), and does not correctly generate an IV. Coupled with the lack of an HMAC, it is subject to several kinds of attack. See Properly encrypting with AES with CommonCrypto for full details on on how to set these correctly, and RNCryptorenter link description here for an example implementation.

对于您的特定问题,您在解密此加密的内容时遇到麻烦,还是在解密Java加密的内容时遇到麻烦?您可能与Java不匹配.

To your specific issue, are you having trouble decrypting things that this encrypted, or are you having trouble decrypting things that the Java encrypted? It's possible that you have a mismatch with the Java.

您应该检查更新"或最终"步骤中是否出现错误.如果执行更新"步骤,则说明配置错误.如果是最后一步,则应首先确保填充正确.文档的末尾应为PKCS#7填充.这意味着它应该以以下序列之一结尾(除非解密后的大小可以被16整除):

You should check whether the error is coming in the Update or Final step. If the Update step, then you've configured something incorrectly. If the Final step, you should start by making sure your padding is correct. The end of the document should be PKCS#7 padding. That means that it should end with one of the following sequences (unless the decrypted size is exactly divisible by 16):

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

整个加密数据的最终大小应被16整除.

The final size of the entire encrypted data should be divisible by 16.

这篇关于使用ObjectiveC进行AES解密的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆