C++中的OpenSSL加密/解密命令行

问题描述

你能告诉我用于加密文件的命令行 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,SEEK_SET);

    //Get file size
    fseek(keyp,SEEK_END);
    int ksize = ftell(keyp);
    //set back to normal
    fseek(keyp,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,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,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:```

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...