を生成した後、私はOpenSSLを使用して32バイトのbase64文字列を生成しようとしていたが、それは常に32バイトの文字列を生成していないと、時々、出力が正しく非印字可能な文字のランダムなnバイトBase64文字列
#include <openssl/rand.h>
#include <openssl/bio.h>
#include <openssl/evp.h>
#include <openssl/buffer.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int Base64Encode(const unsigned char* buffer, unsigned char** b64text) { //Encodes a binary safe base 64 string
BIO *bio, *b64;
BUF_MEM *bufferPtr;
b64 = BIO_new(BIO_f_base64());
bio = BIO_new(BIO_s_mem());
bio = BIO_push(b64, bio);
BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL); //Ignore newlines - write everything in one line
BIO_write(bio, buffer, strlen(buffer));
BIO_flush(bio);
BIO_get_mem_ptr(bio, &bufferPtr);
BIO_set_close(bio, BIO_NOCLOSE);
BIO_free_all(bio);
*b64text=bufferPtr->data;
return (0); //success
}
int main() {
unsigned char buffer[35], *base64EncodeOutput;
int ret = RAND_bytes(buffer, 32);
buffer[32]='\0'; // Null terminate
(void)Base64Encode(buffer, &base64EncodeOutput);
(void)printf("Return value of the operation was: %d\n%s\n", ret, base64EncodeOutput);
return EXIT_SUCCESS;
}
を表示文字化けしていません時には出力は、長さも、32バイトではありません
I6YaDVSRPw5Ux+2paY4u4ToMKtZXQoBj`�
そして:
コンパイルとgcc rand_str.c -lcrypto && ./a.out | tail -1
で実行されているが、時々のようなものを生成します。 openssl rand -base64 32
は、私は違った何をする必要があります:
私の目標は、このコマンドが何を複製するのですか? STRLENは、時々所望の値(32)未満を返すよう
お返事ありがとうございます。 '%44s'と'%.44s'の違いは何ですか? – smac89
%44sは最低44文字、%.44sは最大44文字を出力します。文字列が最小文字列よりも短い場合、文字列は空白で埋められます。最大値よりも長い場合、切り捨てられます。 – Alcaro
ちょうど新しいことを学びました。ありがとう – smac89