0

你能告诉我用于加密文件的命令行 OpenSSL 命令的 C++ 等效项是什么吗?

openssl enc -nosalt -aes-256-cbc -kfile c:\temp\key -in c:\temp\binary.png -out c:\temp\binary.enc

并再次解密该文件:

openssl enc -nosalt -aes-256-cbc -kfile c:\temp\key -d -in c:\temp\binary.enc -out c:\tmp\binary.png

上面的两行都在命令行上工作,但在 c++ 中不行,我试过这个:

   FILE* ifp = fopen( "c:\\tmp\\binary.png", "r");
    FILE* ofp = fopen( "c:\\tmp\\binary.enc", "w");
    FILE* keyp = fopen( "c:\\tmp\\key", "r");

    //Get file size
    fseek(ifp, 0L, SEEK_END);
    int fsize = ftell(ifp);
    //set back to normal
    fseek(ifp, 0L, SEEK_SET);

    //Get file size
    fseek(keyp, 0L, SEEK_END);
    int ksize = ftell(keyp);
    //set back to normal
    fseek(keyp, 0L, SEEK_SET);

    int outLen1 = 0; int outLen2 = 0;
    unsigned char* indata = (unsigned char *)malloc(fsize);
    unsigned char* outdata = (unsigned char *)malloc(fsize*2);
    unsigned char* kdata = (unsigned char* )malloc( ksize );


    //Read File
    fread(indata,sizeof(char),fsize, ifp);//Read Entire File
    fread(kdata,sizeof(char),ksize, keyp);

    //Set up encryption
    EVP_CIPHER_CTX* ctx = EVP_CIPHER_CTX_new();
    EVP_EncryptInit(ctx,EVP_aes_256_cbc(),kdata, NULL ); //no IV
    EVP_EncryptUpdate(ctx,outdata,&outLen1,indata,fsize);
    EVP_EncryptFinal(ctx,outdata + outLen1,&outLen2);
    fwrite(outdata,sizeof(char),outLen1 + outLen2,ofp);


    ::fclose( ifp );
    ::fclose( ofp );
    ::fclose( keyp );

我从 C++ 代码中获得了一个加密文件,但是当我尝试通过命令行对其进行解密时,我得到:

18784:error:0606506D:digital envelope routines:EVP_DecryptFinal_ex:wrong final block length:.\crypto\evp\evp_enc.c:532:```
4

0 回答 0