如何解密作为服务器响应的文本。
我有以下用于使用 Rijndael AES 算法加密字符串的参数。
passPhrase = "Jxy37Kn@" saltValue = "M9!1lj5" hashAlgorithm = "SHA1" passwordIterations = 2 //整数 initVector = "@1B2c3D4e5F6g7H8" keySize = 256 //整数
我从服务器得到的文本是 jp9VG27FQYXh+Uvkc9meFw==
有人可以通过指向一些示例代码来帮助我在 iOS 中解密上述文本。
我使用下面的代码
const CCAlgorithm kAlgorithm = kCCAlgorithmAES128;
const NSUInteger kAlgorithmKeySize = kCCKeySizeAES256;
const NSUInteger kAlgorithmBlockSize = kCCBlockSizeAES128;
const NSUInteger kPBKDFRounds = 2;
- (NSData *)AESKeyForPassword:(NSString *)password
salt:(NSData *)salt {
NSMutableData *derivedKey = [NSMutableData dataWithLength:kAlgorithmKeySize];
int result = CCKeyDerivationPBKDF(kCCPBKDF2, // algorithm
password.UTF8String, // password
password.length, // passwordLength
salt.bytes, // salt
salt.length, // saltLen
kCCPRFHmacAlgSHA1, // PRF
kPBKDFRounds, // rounds
derivedKey.mutableBytes, // derivedKey
derivedKey.length); // derivedKeyLen
// Do not log password here
NSAssert(result == kCCSuccess,
@"Unable to create AES key for password: %d", result);
return derivedKey;
}
- (void) decryptText
{
NSData *data = [NSData dataFromBase64String:@"jp9VG27FQYXh+Uvkc9meFw=="];
NSData *iv = [@"@1B2c3D4e5F6g7H8" dataUsingEncoding:NSUTF8StringEncoding];
NSData *salt = [@"M9!1lj5" dataUsingEncoding:NSUTF8StringEncoding];
NSData *key = [self AESKeyForPassword:@"Jxy37Kn@" salt:salt];
size_t outLength;
NSMutableData *cipherData = [NSMutableData dataWithLength:data.length + kAlgorithmBlockSize];
CCCryptorStatus result = CCCrypt(kCCDecrypt, // operation
kAlgorithm, // Algorithm
kCCOptionECBMode, // options
key.bytes, // key
key.length, // keylength
iv.bytes,// iv
data.bytes, // dataIn
data.length, // dataInLength,
cipherData.mutableBytes, // dataOut
cipherData.length, // dataOutAvailable
&outLength); // dataOutMoved
if (result == kCCSuccess) {
cipherData.length = outLength;
}
else {
}
NSString *apptStr = [[NSString alloc] initWithData:cipherData encoding:NSASCIIStringEncoding];
NSLog(@"apptStr:%@",apptStr);
}
我在解密“#Ï¢K´xÞ#É¢ç”后得到这个,我认为这是不正确的。我在这里想念什么?