The java code is
Cipher c = Cipher.getInstance("AES/CBC/PKCS5Padding");
byte[] ivBytes = new byte[c.getBlockSize()];
String IV = CryptoUtils.hexEncode(ivBytes);
It's giving random 16 bytes response like
563ffcecaa43753bd09613095ad24a12.
How to write corresponding code into objective c?
I have some bunch of objective c code -
- (NSData *)createCipher:(NSString*)data WithKey:(NSString*)key {
NSData* result = nil;
// setup key
unsigned char cKey[kCCKeySizeAES256]; // room for terminator (unused)
bzero(cKey, sizeof(cKey)); // fill with zeroes (for padding)
[key getBytes:cKey length:kCCKeySizeAES256];
// setup iv
char cIv[kCCBlockSizeAES128];
bzero(cIv, kCCBlockSizeAES128);
if (iv) {
[iv getBytes:cIv length:kCCBlockSizeAES128];
}
// setup output buffer
size_t bufferSize = [data length] + kCCBlockSizeAES128;
void *buffer = malloc(bufferSize);
// do encrypt
size_t encryptedSize = 0;
CCCryptorStatus cryptStatus = CCCrypt(kCCEncrypt, //CCOperation op
kCCAlgorithmAES128, //CCAlgorithm alg
kCCOptionPKCS7Padding, //CCOptions
cKey, //const void *key
kCCKeySizeAES256, //size_t keyLength
cIv, // optional initialization vector
[data bytes], // optional per op and alg
[data length],
buffer, // data RETURNED here
bufferSize,
&encryptedSize);
if (cryptStatus == kCCSuccess) {
result = [NSData dataWithBytesNoCopy:buffer length:encryptedSize];
} else {
free(buffer);
NSLog(@"[ERROR] failed to encrypt|CCCryptoStatus: %d", cryptStatus);
}
return result;
}
However, this code is used to encrypt the data, now How to generate the IV ?