How to decrypt a text got as a response from the server.
I have the below parameters that was used to encrypt the string with Rijndael AES algorithm.
passPhrase = "Jxy37Kn@" saltValue = "M9!1lj5" hashAlgorithm = "SHA1" passwordIterations = 2 //Integer initVector = "@1B2c3D4e5F6g7H8" keySize = 256 //Integer
The text that I get from the server is jp9VG27FQYXh+Uvkc9meFw==
Can someone please help me to decrypt the above text in iOS by pointing to some sample code.
I use the below code
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);
}
I get this after decrypt "#Ï¢K´xÞ#É¢ç" which I believe is incorrect. What am I missing here??