我使用以下代码来解密加密的文件,该文件是使用 JAVA 应用程序加密的。
密码.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
密码.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
错误。