我在小型加密应用程序中使用mbedtls库。
mbedtls API具有此功能:
int mbedtls_rsa_rsaes_pkcs1_v15_encrypt( mbedtls_rsa_context *ctx,
int (*f_rng)(void *, unsigned char *, size_t),
void *p_rng,
int mode, size_t ilen,
const unsigned char *input,
unsigned char *output );
我只想加密“ Hello world!”字符串帮助。 我是这样的:
static int myrand(void *rng_state, unsigned char *output, size_t len)
{
size_t use_len;
int rnd;
(void)rng_state;
while (len > 0) {
use_len = len;
if (use_len > sizeof(int))
use_len = sizeof(int);
rnd = rand();
memcpy(output, &rnd, use_len);
output += use_len;
len -= use_len;
}
return 0;
}
int mbed_rsa_pub_enc(uint8_t *from, unsigned int from_len,
uint8_t *to, unsigned int *to_len)
{
int res;
if ((res = mbedtls_rsa_rsaes_pkcs1_v15_encrypt(&rsa_key, myrand, NULL,
MBEDTLS_RSA_PUBLIC, (size_t)from_len,
(const unsigned char *)from,
(unsigned char *)to)) != 0) {
*to_len = 0;
EMSG("Public encription FAILED with err = -0x%X", res * (-1));
return TEE_CRYPTO_ERR;
}
*to_len = rsa_key.len;
EMSG("Public encryption success\n");
return TEE_CRYPTO_OK;
}
我已经在上面运行了代码,得到了:
Encrypted data: cb:26:1f:b6:62:38:be:33:90:a1:66:3c:13:27:2f:35:8c:f0:79:5d:af:fd:39:d0:00:00:00:00:49:00:00:00:24:05:f4:b4:24:05:f4:b4:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:4c:fb:f3:b4:48:00:00:00:10:00:00:00:48:65:6c:6c:6f:20:57:6f:72:6c:64:21:31:00:00:00:4c:05:f4:b4:4c:05:f4:b4:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:30
:00:00:00:60:00:00:00:00:00:00:00:00:00:00:00:cc:06:e1:b4:18:04:86:00:28:4c:86:00:48:4d:86:00:70:4d:86:00:90:4e:86:00:30:4f:86:00:d0:4f:86:00:70:50:86:00:10:51:86:00:00:00:00:00:00:00:00:00:01:00:00:00:40:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00:00
此输出以清晰的类型包含我的源字符串:
... 00:00:48:65:6c:6c:6f:20:57:6f:72:6c:64:21:31:00:00 ...
这是我的“ Hello world!”。 在我看来这是错误的。
我在哪里错了? 有人可以帮我吗?