2

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

我尝试使用加密字符串创建一个数组,我使用 EVP API 进行加密。这很好用,但是当我尝试在 foo 循环中使用 encrypt 函数时,控制台什么也没给我。

这是我的加密功能:

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;

     }

我错过了什么?再次使用 encrypt2 是不可能的吗?非常感谢你。

4

1 回答 1

4

正如您所说,主要问题在于您的 encrypt() 函数,以及您如何调用它。您正在使用 malloc() 在函数内分配内存,并且永远不会释放它,这是内存泄漏(无论如何 malloc 在 c++ 中是禁止的)。您也没有为您的 ctx 运行清理功能。并且您的 encrypt_final 正在覆盖输出缓冲区的第一部分。所以,这里有一个清理过的 encrypt() 和一个匹配的 decrypt():

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;
}
于 2012-09-12T21:17:48.627 回答