English 中文(简体)
AES 使用目标解密
原标题:AES Decryption using ObjectiveC

我按照代码解密加密的文件, 该文件是使用 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 错误 。

最佳回答

我知道这是由 Java 代码生成的。 如果您对这个代码有任何控制, 那么您应该理解它所使用的协议非常不安全。 它没有正确生成密钥( MD5 不是一个好的 PBKDF ), 没有正确生成一个密钥 。 加上缺少 HMAC, 它会受到多种攻击 。 见 < a href=" http:// robnapier. net/ blog/aes- commoncripto-564" rel="nofolpol" resolpolation > properly 加密使用通用的 Crypto AES < / a > 来详细描述如何正确设置这些密钥, Rncryptor < a href=" https://gitub. com/rnapier/ Rncryptor" rel= " nofolpol" 链接描述在这里 < / a > 。

对于你的具体问题,你是否无法解密加密的东西,或者你无法解密 Java 加密的东西?你可能与 Java 不符。

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

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

全部加密数据的最后大小应除以16。

问题回答

暂无回答




相关问题
Extend Contacts application on Android to provide encryption

I want to encrypt individual contacts stored by the Contacts application on Android based on user s preference. So, I am thinking I ll have to hook/extend the Contacts application before the it stores ...

Make md5 strong

Im making a website that will intergrate with game that only support md5 hashing metod (atm). Which ofc is not especially safe anymore. But how could i make it stronger? Should I just generate long ...

How to Pack/Encrypt/Unpack/Decrypt a bunch of files in Java?

I m essentially trying to do the following on a Java/JSP-driven web site: User supplies a password Password is used to build a strongly-encrypted archive file (zip, or anything else) containing a ...

Thread & Queue vs Serial performance

I though it ll be interesting to look at threads and queues, so I ve written 2 scripts, one will break a file up and encrypt each chunk in a thread, the other will do it serially. I m still very new ...

Convert PHP encryption code to C#

I m trying to convert this piece of code from PHP to C#. It s part of a Captive Portal. Could somebody explain what it does? $hexchal = pack ("H32", $challenge); if ($uamsecret) { $newchal = ...

Encryption: how to have 1 iv despite multiple fields

I ve been stuck trying to arrive at a best solution for this for a while. I know that an initialization vector has to be unique for each item being encrypted. So if I m encrypting an address and I ...

热门标签