Déencryption AES avec ObjectiveC

J'utilise le code suivant pour déchiffrer un file chiffré, le file a été chiffré à l'aide d'une application JAVA.

Fichier Cipher.h

#import <Foundation/Foundation.h> #import <CommonCrypto/CommonCryptor.h> #import <CommonCrypto/CommonDigest.h> @interface Cipher : NSObject { NSSsortingng *cipherKey; } @property (retain) NSSsortingng *cipherKey; - (Cipher *) initWithKey:(NSSsortingng *) key; - (NSData *) encrypt:(NSData *) plainText; - (NSData *) decrypt:(NSData *) cipherText; - (NSData *) transform:(CCOperation) encryptOrDecrypt data:(NSData *) inputData; + (NSData *) md5:(NSSsortingng *) ssortingngToHash; @end 

Fichier Cipher.m

 #import "Cipher.h" @implementation Cipher @synthesize cipherKey; - (Cipher *) initWithKey:(NSSsortingng *) 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:(NSSsortingng *) ssortingngToHash { const char *src = [ssortingngToHash UTF8Ssortingng]; unsigned char result[CC_MD5_DIGEST_LENGTH]; CC_MD5(src, strlen(src), result); return [NSData dataWithBytes:result length:CC_MD5_DIGEST_LENGTH]; } @end 

Voici comment je décode datatables cryptées:

 - (void)viewDidLoad { [super viewDidLoad]; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSSsortingng *documentsDirectory1 = [paths objectAtIndex:0]; NSSsortingng *getImagePath = [documentsDirectory1 ssortingngByAppendingPathComponent:@"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]; } 

Le déchiffrement ne fonctionne pas correctement et renvoie l'erreur 4304 .

Je sais que cela a été généré par le code Java. Si vous avez un contrôle sur ce code, alors vous devez comprendre que le protocole qu'il utilise est très peu sûr. Il ne génère pas correctement une key (MD5 n'est pas un bon PBKDF), et ne génère pas correctement un IV. Couplé avec l'absence d'un HMAC, il est soumis à plusieurs types d'attaques. Voir Chiffrement correct avec AES avec CommonCrypto pour plus de détails sur la façon de les définir correctement, et RNCryptor entrer la description du lien ici pour un exemple d'implémentation.

Pour votre problème spécifique, avez-vous de la difficulté à déchiffrer des éléments chiffrés, ou avez-vous de la difficulté à déchiffrer des éléments chiffrés par Java? Il est possible que vous ayez une discordance avec Java.

Vous devez vérifier si l'erreur arrive dans l'étape Update ou Final. Si l'étape Mise à jour, vous avez mal configuré quelque chose. Si l'étape finale, vous devriez commencer en vous assurant que votre remplissage est correct. La fin du document doit être un remplissage PKCS # 7. Cela signifie qu'il devrait se terminer par l'une des séquences suivantes (sauf si la taille décryptée est exactement divisible par 16):

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

La taille finale de l'set des données cryptées doit être divisible par 16.