I am trying to do a cross-component encryption working between different system. Currently, I can't really match the outputs. I am using openssl/aes/h library for C and javax.crypto for Java/Scala. I don't have much flexibility in changing the code in the C system, so that leaves me in the need of modifying the Java/Scala code.
#include <string.h>
#include <openssl/aes.h>
char key[] = "aaaaaaaaaaaaaaaa";
int main(){
unsigned char text[]="hello";
unsigned char enc_out[80];
unsigned char dec_out[80];
AES_KEY enc_key, dec_key;
AES_set_encrypt_key(key, 128, &enc_key);
AES_encrypt(text, enc_out, &enc_key);
return 0
}
The C code roughly looks like that.Using the AES_set_encrypt_key and AES_encrypt calls except there is padding with 0s when needed (when key is not 128 bits). The padding shouldnt matter for this simple example.
In the scala code I have:
def aes_encrypt(value: String, k: String): Array[Byte] = {
val cipher = Cipher.getInstance("AES")
val key = Arrays.copyOf(k.getBytes("UTF-8"), 16)
val secretKey: SecretKeySpec = new SecretKeySpec(key, 0, key.length, "AES")
cipher.init(Cipher.ENCRYPT_MODE, secretKey)
cipher.doFinal(value.getBytes("UTF-8"), 0, value.getBytes("UTF-8").length)
}
Using the key "aaaaaaaaaaaaaaaa" and message "hello" , in base64
The C code gives "XaiMOCo7KteRUkejpd8JLA=="
in Scala/Java "7GHRfFgKVdaAiwti36tqDQ=="
I am trying to match the outputs, but I have not been able to do it. I have tried different algorithm for the Scala code, but I am not sure how to match the C output.
I have tried to look into the documentation of AES_set_encrypt_key and AES_encrypt calls, but I couldn't dig too much out of it. I know that there are better user friendly calls from the openssl library like evp_encryption calls, but I can't get away from AES_set_encrypt_key and AES_encrypt