私の考えは、クライアントサーバーモデルでファイル暗号化を行うことであり、暗号化の目的でopenssl evpを使用しています。暗号文をテキスト ファイルに保存し、クライアントに送信する必要があります。しかし、ファイルに保存できない暗号文に無効な文字が存在するため、これを行うことができません。
これは暗号化のための私のコードです:
EVP_CIPHER_CTX_init(&ctx);
EVP_CipherInit_ex(&ctx, EVP_aes_256_ctr(), NULL, NULL, NULL,
do_encrypt);
OPENSSL_assert(EVP_CIPHER_CTX_key_length(&ctx) == 32);
OPENSSL_assert(EVP_CIPHER_CTX_iv_length(&ctx) == 16);
EVP_CipherInit_ex(&ctx, NULL, NULL, key, iv, do_encrypt);
//receive the file contents in chunks of 1024 bytes
while ((inlen = recv(connfd, inbuf, sizeof inbuf, 0)) > 0) {
fprintf(stdout,"\nReceived %d bytes",inlen);
fflush(stdout);
fprintf(stdout,"\nOriginal: %s",inbuf);
fflush(stdout);
//use encrypt_update() to encrypt the chunks
if(!EVP_CipherUpdate(&ctx, outbuf, &outlen, inbuf, inlen)) {
/* Error */
EVP_CIPHER_CTX_cleanup(&ctx);
return 0;
}
//write the encrypted text to out file
fprintf(stdout,"\nEncrypted: %s %d",outbuf, inlen);
fflush(stdout);
fwrite(outbuf, sizeof(char), outlen, fp);
//clear the buffer
memset(inbuf,0, strlen(inbuf));
memset(outbuf,0, strlen(outbuf));
}
//use encrypt_final() to encrypt the final letf out block of chunk is any
if(!EVP_CipherFinal_ex(&ctx, outbuf, &outlen)) {
/* Error */
EVP_CIPHER_CTX_cleanup(&ctx);
return 0;
}
//write the encrypted text to out file
fwrite(outbuf, sizeof(char), outlen, fp);
EVP_CIPHER_CTX_cleanup(&ctx); //cleanup
fclose(fp); //close the file
このリンクを参照して、復号化を伴う無効な文字の問題が報告され、解決されました。
openssl evp api (aes256cbc) を使用したファイルの暗号化に関する問題
誰かがここで私を助けてくれることを願っています。
前もって感謝します。