I have a Java code for encryption which looks like this
byte[] encrypt(byte[] clearData) {
byte[] passwordKey = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E,0x0f};
byte[] rawSecretKey = new byte[]{0x34, (byte) 0xA4, 0x16, 0x09, 0x77, (byte) 0x85, (byte) 0xB4, 0x31,
0x75, 0x12, (byte) 0x92, (byte) 0xDD, (byte) 0xCA, 0x15, (byte) 0xAB, (byte) 0xBA};
secretKey = new SecretKeySpec(passwordKey, "AES");
ivParameterSpec = new IvParameterSpec(rawSecretKey);
Cipher aesCipher = Cipher.getInstance("AES/CBC/PKCS5Padding");
aesCipher.init(Cipher.ENCRYPT_MODE, secretKey, ivParameterSpec);
byte[] encryptedData;
encryptedData = aesCipher.doFinal(clearData);
return encryptedData;
}
I have to port this code to JNI. I have build openssl and made JNI wrapper function which looks like this:
JNIEXPORT jbyteArray JNICALL Java_axon_voiceassistant_utils_JNIUtils_getcr(JNIEnv *env, jclass cls, jbyteArray srcData)
{
int srcLen=env->GetArrayLength(srcData);
unsigned char* indata = new unsigned char[srcLen];
env->GetByteArrayRegion (srcData, 0, srcLen, reinterpret_cast<jbyte*>(indata));
const unsigned char ukey[] = {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E,0x0f};
unsigned char iv [] = {0x34, 0xA4, 0x16, 0x09, 0x77,0x85, 0xB4, 0x31,
0x75, 0x12, 0x92,0xDD, 0xCA, 0x15, 0xAB, 0xBA};
const size_t encs_length = ((srcLen + AES_BLOCK_SIZE) / AES_BLOCK_SIZE) * AES_BLOCK_SIZE;
unsigned char enc_data[encs_length];
memset(enc_data, 0, sizeof(enc_data));
AES_KEY key;
memset(&key, 0, sizeof(AES_KEY));
AES_set_encrypt_key(ukey, 128, &key);
AES_cbc_encrypt(indata, enc_data, srcLen, &key, iv, AES_ENCRYPT);
jbyteArray bArray = env->NewByteArray(encs_length);
jboolean isCopy;
void *enc_copy = env->GetPrimitiveArrayCritical((jarray)bArray, &isCopy);
memcpy(enc_copy, enc_data, encs_length);
env->ReleasePrimitiveArrayCritical( bArray, enc_copy, 0);
return bArray;
}
But I don't get same results with the JNI version as with the Java version. What could be the problem?
You've zero padded your plaintext in your JNI code:
const size_t encs_length = ((srcLen + AES_BLOCK_SIZE) / AES_BLOCK_SIZE) * AES_BLOCK_SIZE;
unsigned char enc_data[encs_length];
memset(enc_data, 0, sizeof(enc_data));
But your Java code is using PKCS #7 padding. One of these will need to change.
Note: I think (based on some research) that AES_cbc_encrypt
does zero padding by default, so the step where you've done this yourself might be redundant.
To fix this, either implement PKCS #7 padding by hand in your JNI code (it's actually very easy), or consider using the higher-level EVP functions, which understand how to pad data.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.