晚上好,我正在构建一个java项目,在这个项目中,它通过JNI与Intel SGX飞地进行通信,并看到它发送的由enclave密封的信息。
然而,解密信息会返回我无法理解的信息,在这一点上,我相信这是由于大小的差异,但我并不完全理解它。
所以,我知道Sizeof(char*)相当于1字节,就像sizeof(jbyte)一样。但是,sizeof(jchar)相当于2个字节。
在获得了这些知识之后,我决定通过使用JByteArray来实现密封(或加密)功能,以避免这个问题。这个byte[]应该用UTF-8还是UTF-16?它对整体功能有影响吗?
以下是我所做工作的一个例子:
JNIEXPORT jbyteArray JNICALL Java_sgxUtils_SgxFunctions_jni_1sgx_1seal_1info
(JNIEnv * env, jobject, jbyteArray jArray){
//Create a char* from the given byte array.
jbyte *b = env->GetByteArrayElements(jArray,NULL);
int StringSize = env->GetArrayLength(jArray);
char* c = (char*)malloc(sizeof(char) * StringSize);
memcpy(c,b,StringSize);
//Calculate the size of the encryption.
size_t sealed_size = sizeof(sgx_sealed_data_t) + StringSize;
//Create a uint8_t pointer to store the sealed information
uint8_t* sealed_data = (uint8_t*)malloc(sealed_size);
sgx_status_t ecall_status; //Store the status of the ecall.
seal(global_eid, &ecall_status,(uint8_t*)&c,StringSize, sgx_sealed_data_t*)sealed_data,sealed_size);
//Produce the jbyteArray to return:
jbyte* jresult = (jbyte*)calloc(sizeof(jbyte),sealed_size);
for(int i = 0 ; i < sealed_size ;i++){
jresult[i] = (jbyte)sealed_data[i];
}
jbyteArray jArray2 = env->NewByteArray(sealed_size+1);
env->SetByteArrayRegion(jArray2,0,sealed_size,jresult);
return jArray2;使用uint8_t会影响密封吗?我不知道它可能是什么。我在java中接收这个byte[]的方式如下:
byte[] c = sgx.jni_sgx_seal_info("toEncrypt".getBytes(Charset.forName("UTF-8")));
String sealed = new String(c,StandardCharsets.UTF_8);有人知道什么会影响这件事吗?解封(解密)后的输出当前为�|(�。
发布于 2022-02-13 16:17:49
您不能将随机字节(如加密产生的)转换为UTF-8 (或者许多多字节编码、8位单字节编码都可以)。字符串很可能会被破坏,因为有描述非法字符的字节序列,它们将被替换为0xFFFE或� (即unicode替换字符)。
因此,您需要保留byte[],在解密字节数组而不是字符串之前,不要将其转换为字符串。
https://stackoverflow.com/questions/71102411
复制相似问题