evp_crypt 在 c 中的 for 循环中不起作用

2024-03-08

我是新来的,如果我做错了什么,请原谅。

我尝试使用加密字符串创建一个数组,我使用 EVP API 进行加密。这工作正常,但是当我尝试在 foo 循环中使用加密函数时,控制台没有给我任何信息。

这是我的加密函数:

char *encrypt(char *key, char *iv, char * source){

    //char *target;
    int in_len, out_len;
    EVP_CIPHER_CTX ctx;
    in_len=strlen((const char *)source);
    unsigned char *target = (unsigned char *) malloc(in_len);
        //printf("This is the text before ciphering: %s\n",source);
        //printf("The length of the string is: %d\n",in_len);
        //starting the encryption process
        EVP_CIPHER_CTX_init(&ctx);
        EVP_EncryptInit_ex(&ctx,EVP_aes_128_cbc(),NULL,(unsigned char*) key,(unsigned char*)iv);
        EVP_EncryptUpdate(&ctx,target,&out_len,(unsigned char*)source,in_len);
        EVP_EncryptFinal_ex(&ctx,target,&out_len);
        target[out_len] = '\0';

        //EVP_CIPHER_CTX_cleanup(&ctx);


        return ((char *)target);
}

并在主循环中:

int main(){
    char source[17]="Shahababamamaaaa";
    char key[17]="ahardtobreakkey1";
    char iv[17] = "veryinterestingv";
     int rows = 1280;
     int cols = (3*800)/16;
        char *encrypted=encrypt(key, iv, source);
        printf("encrypted: %s\n", encrypted);
        char *encrypted2;
        encrypted2=encrypt(key, iv, encrypted);
        printf("encrypted2: %s\n", encrypted2);
        char *mx[rows];
        char *in, *temp;
        in = (char *) malloc ( cols * sizeof(char) );
        temp =(char *) malloc ( strlen(encrypted) );
        int i, j;

        for (i=0; i<5; i++){
            strcpy(in,encrypted);
            for(j=0;j<3;j++){
                    printf("in: %s\n", in);
                    strcpy(temp, encrypted2);
                    printf("temp: %s\n", temp);
                    memset(encrypted2,0x00, strlen(encrypted));
                    encrypted2=encrypt(key, iv,temp);
                    printf("encrypted2 nach j=%d : %s\n",j, encrypted2);

                    mx[i]=in;
            }

        }
        printf("Stele 0 Inhalt %s\n",mx[0]);
        printf("Laenge von 1 %d\n", strlen(mx[0]));

        //system ("PAUSE");
        free(in);
        return 0;

     }

我缺少什么?是否可以再次使用加密? 非常感谢。


正如您所说,主要问题在于您的 encrypt() 函数,而且还在于您如何调用它。您正在使用 malloc() 在函数内分配内存,并且从不释放它,这是内存泄漏(无论如何 malloc 在 c++ 中都是禁忌)。您也没有为您的 ctx 运行清理功能。并且您的 encrypt_final 正在覆盖输出缓冲区的第一部分。因此,这是一个清理后的 encrypt() 和一个匹配的解密():

int encrypt(unsigned char *key, 
        unsigned char *iv, 
        unsigned char * source, 
        unsigned char* target, 
        int in_len) // Need an in length.  Not all input is going to be
                    // zero-terminated, for example if we're reading from a file

{

    int out_len; // Return the output length.  Because it also won't be null
                 // terminated, and may contain null characters inline

    int final_out_len; // So that we don't overwrite out_len with the final call
    EVP_CIPHER_CTX ctx;

    EVP_CIPHER_CTX_init(&ctx);
    EVP_EncryptInit_ex(&ctx,EVP_aes_128_cbc(),NULL,key,iv);
    EVP_EncryptUpdate(&ctx,target,&out_len,source,in_len);
    EVP_EncryptFinal_ex(&ctx,target+out_len,&final_out_len);
    EVP_CIPHER_CTX_cleanup(&ctx);
    return out_len+final_out_len; // need to sum these together, because both
                                  // encrypt calls wrote data
}

并解密:

int decrypt(unsigned char *key, 
        unsigned char *iv, 
        unsigned char * source, 
        unsigned char* target, 
        int in_len)
{

    int out_len=0,final_out_len=0;
    EVP_CIPHER_CTX ctx;
    EVP_CIPHER_CTX_init(&ctx);
    EVP_DecryptInit_ex(&ctx,EVP_aes_128_cbc(),NULL,key,iv);
    EVP_DecryptUpdate(&ctx,target,&out_len,source,in_len);
    EVP_DecryptFinal_ex(&ctx,target+out_len,&final_out_len);
    EVP_CIPHER_CTX_cleanup(&ctx);
    //Just to be nice, we'll add a zero at the end of the decrypted string
    target[out_len+final_out_len] = 0;
    return out_len+final_out_len;
}

将它们放在一起(循环,以证明你的概念):

int _tmain(int argc, _TCHAR* argv[])
{
    unsigned char key[] = {0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
    unsigned char ivec[] = {1,2,3,4,5,6,7,8};
    char *raw_buffer = "This is a test string";
    int raw_count = strlen(raw_buffer);
    for (int i=0; i<5; i++){
        unsigned char *decrypted_buffer = new unsigned char[raw_count+64];
        unsigned char *encrypted_buffer = new unsigned char[raw_count+64];
        int final_len = encrypt(key,ivec,(unsigned char*)raw_buffer,(unsigned char*)encrypted_buffer,raw_count);
        int dec_len = decrypt(key,ivec,(unsigned char*)encrypted_buffer,(unsigned char*)decrypted_buffer,final_len);
        printf("raw_count: %i\nfinal_len: %i\ndec_len: %i\n",raw_count,final_len,dec_len);
        printf("Original str: \n%s\n",raw_buffer);
        printf("Encrypted: \n%s\n", encrypted_buffer);
        printf("Decrypted:\n%s\n\n\n", decrypted_buffer);
        delete[] decrypted_buffer;
        delete[] encrypted_buffer;
    }
    char c;
    c=getchar();
    return 0;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

evp_crypt 在 c 中的 for 循环中不起作用 的相关文章

  • EF Core 返回 null 关系,直到直接访问

    我有一些如下所示的模型 public class Mutant public long Id get set Relations public long OriginalCodeId get set public virtual Origi
  • 未找到 DEADLINE 调度策略

    我想在 C 中实现 DEADLINE 调度策略 我知道该功能已实现Linux 3 14 10我正在使用 Ubuntu 14 04Linux 3 17 0 031700 lowlatency 201410060605 SMP PREEMPT这
  • 并行运行多个任务

    我有一个代理列表 每个代理都会访问不同的站点并从站点中提取所需的数据 目前它一次只做一个 但我希望同时运行 10 20 个任务 这样它就可以一次性从 20 个站点下载 而不是只下载一个 这是我目前正在做的事情 private async T
  • 如何在 C++ 中为指针“this”赋值

    在函数中 如何分配this一个新的价值 您可以分配对象this点于 this XY 但你不能分配直接值this this XY Error Expression is not assignable
  • 从结构调用 C++ 成员函数指针

    我找到了有关调用 C 成员函数指针和调用结构中的指针的信息 但我需要调用结构内部存在的成员函数指针 但我无法获得正确的语法 我在类 MyClass 的方法中有以下代码片段 void MyClass run struct int MyClas
  • 如何在 C# 中以编程方式将行添加到 DataGrid?

    正如标题所述 我正在尝试使用 C 以编程方式将行添加到 DataGrid 但我似乎无法使其工作 这是我到目前为止所拥有的 I have a DataGrid declared as dg in the XAML foreach string
  • C# 结构默认值

    我有一个方法 它接受一个包含许多具有基本数据类型的字段的结构 我想传递大部分默认值 但需要进行一些调整 但我了解结构声明中的基本字段不能包含默认值声明 例如struct S int a 42 现在是这样的 OptionsStruct opt
  • 加载 QPixmap 数据的更好方法

    更好的方法来做到这一点 没有QImage QImage image width height QImage Format RGB888 memcpy image bits m frameRGB gt data 0 height width
  • X 轴和 Z 轴上的 Quaternion.Slerp,无 Y 轴

    I am trying to rotate the Player about X Y and Z axis The Y axis should not move from last angle Example if I rotate 45
  • 使用任一默认捕获模式时,这是通过复制捕获还是 (*this) 通过引用捕获?是一样的吗?

    当我看到以下工作时我有点困惑 struct A void g void f g 但后来我发现this https stackoverflow com a 16323119 5825294答案非常详细地解释了它是如何工作的 本质上 它归结为t
  • MINIX内部碎片2

    我正在用 C 语言编写一些软件 它递归地列出给定目录中的所有文件 现在我需要计算出内部碎片 我花了很长时间研究这个问题 发现 ext2 上的内部碎片只发生在最后一个块中 我知道理论上你应该能够从索引节点号获得第一个和最后一个块地址 但我不知
  • 将 AutomationID 与 ListView 结合使用

    我正在尝试将 AutomationId 附加到列表视图中的项目 理想情况下 将项目名称绑定到显示的项目
  • 运行实体框架自定义工具,它有什么作用?

    在 Visual Studio 中 当使用实体框架并为 tt 和 Context tt 文件应用运行自定义工具时 它是什么以及它有什么作用 为什么它解决数据库同步问题 有时 为什么我应该在运行 tt 之前运行它 Context tt 它被称
  • 为什么这个位图图像在加载后会改变大小?

    快速提问 我有这个1000 1000位图图像 我使用这个例程来加载它 private BitmapSource initialBitmap new BitmapImage new Uri C Users Desktop Original b
  • MPI - 发送和接收列

    我需要从一个进程发送矩阵列并从另一个进程接收它 我尝试运行以下程序 但得到了一个奇怪的结果 至少我这么认为 仅复制矩阵的第一个元素 某些矩阵元素会发生意外变化 include
  • C++网络序列化[关闭]

    Closed 这个问题正在寻求书籍 工具 软件库等的推荐 不满足堆栈溢出指南 help closed questions 目前不接受答案 我正在寻找一种将 C 数据包序列化为网络流的解决方案 我在这里看到很多帖子提到人们 ACE 谷歌协议缓
  • 为什么我可以在另一个函数中定义一个函数?

    请参阅下面的代码 我在另一个函数中定义了一个函数 void test1 void void test2 void printf test2 n printf test1 n int main void test1 return 0 这个用法
  • 如何配置 qt Creator 以显示 C++ 代码而不是反汇编程序?

    昨天我做了很多事情 比如更新 GCC Clang 和重新安装 Qt Creator 今天 在逐步调试我的代码时 调试器显示的是反汇编代码 而不是我编写的 C 代码 紧迫F10 or F11 调试器正在进入汇编代码而不是 cpp nor h我
  • 稀疏矩阵超定线性方程组c/c++库

    我需要一个库来解决 Ax b 系统 其中 A 是一个非对称稀疏矩阵 每行有 8 个条目 而且可能很大 我认为实现双共轭梯度的库应该没问题 但我找不到一个有效的库 我尝试过 iml 但 iml sparselib 包中缺少一些标头 有小费吗
  • 将同步 zip 操作转换为异步

    我们有一个现有的库 其中一些方法需要转换为异步方法 但是我不确定如何使用以下方法执行此操作 错误处理已被删除 该方法的目的是压缩文件并将其保存到磁盘 请注意 zip 类不公开任何异步方法 public static bool ZipAndS

随机推荐