我使用那个很棒的代码https://github.com/Gurpartap/AESCrypt-ObjC试图解密一些在OFB模式下用AES-128加密的数据。 加密文本在base64中,iv和密钥是十六进制!
所以我使用了以下功能:
+ (NSString *)decrypt:(NSString *)base64EncodedString password:(NSData *)password andIV:(NSData *)iv{
NSData *encryptedData = [NSData base64DataFromString:base64EncodedString];
NSData *decryptedData = [encryptedData decryptedAES256DataUsingKey:password andIV:iv error:nil];
return [[NSString alloc] initWithData:decryptedData encoding:NSUTF8StringEncoding];
}
- (NSData *) decryptedAES256DataUsingKey: (id) key andIV:iv error: (NSError **) error
{
CCCryptorStatus status = kCCSuccess;
NSData * result = [self decryptedDataUsingAlgorithm: kCCAlgorithmAES128
key: key
initializationVector:iv
options: kCCModeOFB
error: &status];
if ( result != nil )
return ( result );
if ( error != NULL )
*error = [NSError errorWithCCCryptorStatus: status];
return ( nil );
}
- (NSData *) decryptedDataUsingAlgorithm: (CCAlgorithm) algorithm
key: (id) key // data or string
initializationVector: (id) iv // data or string
options: (CCOptions) options
error: (CCCryptorStatus *) error
{
CCCryptorRef cryptor = NULL;
CCCryptorStatus status = kCCSuccess;
NSParameterAssert([key isKindOfClass: [NSData class]] || [key isKindOfClass: [NSString class]]);
NSParameterAssert(iv == nil || [iv isKindOfClass: [NSData class]] || [iv isKindOfClass: [NSString class]]);
NSMutableData * keyData, * ivData;
if ( [key isKindOfClass: [NSData class]] )
keyData = (NSMutableData *) [key mutableCopy];
else
keyData = [[key dataUsingEncoding: NSUTF8StringEncoding] mutableCopy];
if ( [iv isKindOfClass: [NSString class]] )
ivData = [[iv dataUsingEncoding: NSUTF8StringEncoding] mutableCopy];
else
ivData = (NSMutableData *) [iv mutableCopy]; // data or nil
#if !__has_feature(objc_arc)
[keyData autorelease];
[ivData autorelease];
#endif
// ensure correct lengths for key and iv data, based on algorithms
FixKeyLengths( algorithm, keyData, ivData );
status = CCCryptorCreate( kCCDecrypt, algorithm, options,
[keyData bytes], [keyData length], [ivData bytes],
&cryptor );
if ( status != kCCSuccess )
{
if ( error != NULL )
*error = status;
return ( nil );
}
NSData * result = [self _runCryptor: cryptor result: &status];
if ( (result == nil) && (error != NULL) )
*error = status;
CCCryptorRelease( cryptor );
return ( result );
}
我称之为:
// Do any additional setup after loading the view, typically from a nib.
NSString *hexString = @"d00f41f5495e9468fabdfe55d0bbf031";
NSData *keyData = [self dataFromHexString:hexString];
NSString *hexiv = @"921f3eb904e6b48e5ae26128ead7e434";
NSData *ivData = [self dataFromHexString:hexString];
NSString *encryptedData = [AESCrypt decrypt:@"vuFSz1QqLtj4akw=" password:keyData andIV:ivData];
NSLog(@"EncryptedData = %@",encryptedData);
其中vuFSz1QqLtj4akw =加密的“Hello World”,将hexString转换为nsdata的方法如下:
- (NSData *)dataFromHexString:(NSString *)string
{
NSMutableData *stringData = [[NSMutableData alloc] init] ;
unsigned char whole_byte;
char byte_chars[3] = {'\0','\0','\0'};
int i;
for (i=0; i < [string length] / 2; i++) {
byte_chars[0] = [string characterAtIndex:i*2];
byte_chars[1] = [string characterAtIndex:i*2+1];
whole_byte = strtol(byte_chars, NULL, 16);
[stringData appendBytes:&whole_byte length:1];
}
return stringData;
}
但所有这些都给我NULL加密数据!任何帮助将不胜感激