J'utilise OpenSSL pour créer une chaîne. Après cela, je veux encoder la chaîne cryptée avec l'algorithme base64 en utilisant également OpenSSL. Donc, j'ai trouvé le code suivant snipped: (bit.ly/adUSEw)OpenSSL - Comment déterminer la bonne longueur d'une chaîne chiffrée par RSA?
char *base64(const unsigned char *input, int length) {
BIO *bmem, *b64;
BUF_MEM *bptr;
b64 = BIO_new(BIO_f_base64());
bmem = BIO_new(BIO_s_mem());
b64 = BIO_push(b64, bmem);
BIO_write(b64, input, length);
BIO_flush(b64);
BIO_get_mem_ptr(b64, &bptr);
char *buff = (char*)malloc(bptr->length);
memcpy(buff, bptr->data, bptr->length - 1);
buff[bptr->length - 1] = 0;
BIO_free_all(b64);
return buff;
}
int main(int argc, char **argv) {
char *message = "TEST";
char *encryptedString = Encrypt(message);
if (encryptedString == NULL) {
return 0;
}
else {
char *output = base64(encryptedString, strlen(encryptedString));
cout << output << endl;
} }
J'ai remarqué que strlen (encryptedString) ne fonctionne pas correctement dans ce cas. Parfois, il renvoie la bonne longueur mais la plupart du temps pas. Alors, quelle est la bonne façon de déterminer la bonne longueur?
Muchas gracias! –