如何使用MPI修复C中AES的解密

如何使用MPI修复C中AES的解密,c,parallel-processing,aes,mpi,ipc,C,Parallel Processing,Aes,Mpi,Ipc,目前,我正在尝试使用MPI将特定的加密数据从一个处理器传递到另一个处理器,仅使用2个处理器来检查加密或解密是否正常工作。我有一个int值,它被转换成字符串进行加密,然后发送到处理器秩1。 我在处理器中正确地接收字符串,但当我调用解密函数时,它似乎没有给我明文。 我已从下载了AES代码 首先,我们必须安装需求。 成功安装需求后,我们运行简单的hello world MPI 你好,世界 #include <mpi.h> #include <stdio.h> int mai

目前,我正在尝试使用MPI将特定的加密数据从一个处理器传递到另一个处理器,仅使用2个处理器来检查加密或解密是否正常工作。我有一个int值,它被转换成字符串进行加密,然后发送到处理器秩1。 我在处理器中正确地接收字符串,但当我调用解密函数时,它似乎没有给我明文。 我已从下载了AES代码


首先,我们必须安装需求。

成功安装需求后,我们运行简单的hello world MPI

你好,世界

#include <mpi.h>
#include <stdio.h>

int main(int argc, char** argv) {
    // Initialize the MPI environment
    MPI_Init(NULL, NULL);

    // Get the number of processes
    int world_size;
    MPI_Comm_size(MPI_COMM_WORLD, &world_size);

    // Get the rank of the process
    int world_rank;
    MPI_Comm_rank(MPI_COMM_WORLD, &world_rank);

    // Get the name of the processor
    char processor_name[MPI_MAX_PROCESSOR_NAME];
    int name_len;
    MPI_Get_processor_name(processor_name, &name_len);

    // Print off a hello world message
    printf("Hello world from processor %s, rank %d out of %d processors\n",
           processor_name, world_rank, world_size);

    // Finalize the MPI environment.
    MPI_Finalize();
}
成功运行此代码后,您可以继续


并返回原始问题。

我认为您在项目中使用AES部分是错误的

通过执行一些更改和使用

AES_ECB_decrypt(&ctx, data_file);

AES_ECB_encrypt(&ctx, data_file);
工作

GitHub项目页面说明如何使用Enc和Dec函数

第二个重要变化是使用
struct AES\u ctx ctx
以上主键只能执行一次


这是代码的工作版本。

#include <stdio.h>
#include <string.h>
#include <mpi.h>
#include "aes.h"

/*
 * compile
 * mpicc mpi-hello.c aes.c -o mpi-hello
 * 
 * Run
 * mpirun -np 2  executable
 * 
 * 
 * */


unsigned long ToUInt(char* str)
{
    unsigned long mult = 1;
    unsigned long re = 0;
    int len = strlen(str);
    for(int i = len -1 ; i >= 0 ; i--)
    {
        re = re + ((int)str[i] -48)*mult;
        mult = mult*10;
    }
    return re;
}


int main(int argc, char** argv) {

    int size,rank=0;

    MPI_Init(NULL, NULL);

    // Get the number of processes
    int world_size;
    MPI_Comm_size(MPI_COMM_WORLD, &size);

    // Get the rank of the process
    int world_rank;
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);

    int data = 532123;
    uint8_t data_file[10];


     //start Crypto Section

    //~ key and iv
    ///

    uint8_t key[32] = { 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
                        0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4 
    };

    uint8_t iv[16]  = { 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
    };
    ///    

    struct AES_ctx ctx;


    printf("this is in main\n");

    if (rank == 0) {
        printf("data rank: 0: %d\n", data);
        //convert in to string
        sprintf( data_file, "%d", data);
        //string
        printf("data rank: 0: %s\n", data_file);

        ///Enc

        //set
        AES_init_ctx_iv(&ctx, key, iv); 

        //enc
        AES_CTR_xcrypt_buffer(&ctx, data_file, strlen(data_file));


        printf("Sending in 0 after crypt: %s\n", data_file);
        printf("strlen send: %d\n", strlen(data_file));

        MPI_Send(data_file,10,MPI_UINT8_T,1,0,MPI_COMM_WORLD);

    }else{

        MPI_Recv(data_file,10,MPI_UINT8_T,0,0,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
        printf("Recieved in 1 before: %s\n",data_file);
        printf("strlen recv: %d\n", strlen(data_file));

        ///Dec
        //set
        AES_init_ctx_iv(&ctx, key, iv); 

        //dec
        AES_CTR_xcrypt_buffer(&ctx, data_file, strlen(data_file));  

        printf("DEC at Rank 1 string: %s\n", data_file);
        printf("DEC at Rank 1 int: %u\n", ToUInt(data_file));
    }


    MPI_Finalize();
}
if (rank == 0) {
    printf("data rank: 0: %d\n", data);
    //convert in to string
    sprintf( data_file, "%d", data);
    //string
    printf("data rank: 0: %s\n", data_file);

    //~ key and iv
    ///

    uint8_t key[32] = { 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
                        0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4 
    };

    uint8_t iv[16]  = { 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
    };
    ///    
    ///Enc

    //set
    AES_init_ctx_iv(&ctx, key, iv); 

    //enc
    AES_CTR_xcrypt_buffer(&ctx, data_file, strlen(data_file));


    printf("Sending in 0 after crypt: %s\n", data_file);
    printf("strlen send: %d\n", strlen(data_file));

    MPI_Send(data_file,10,MPI_UINT8_T,1,0,MPI_COMM_WORLD);

}else{

    MPI_Recv(data_file,10,MPI_UINT8_T,0,0,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
    printf("Recieved in 1 before: %s\n",data_file);
    printf("strlen recv: %d\n", strlen(data_file));



    //~ key and iv
    ///

    uint8_t key[32] = { 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
                        0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4 
    };

    uint8_t iv[16]  = { 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
    };
    ///    
    ///Dec
    //set
    AES_init_ctx_iv(&ctx, key, iv); 

    //dec
    AES_CTR_xcrypt_buffer(&ctx, data_file, strlen(data_file));  

    printf("DEC at Rank 1 string: %s\n", data_file);
    printf("DEC at Rank 1 int: %u\n", ToUInt(data_file));
}
mpi hello.c

#include <stdio.h>
#include <string.h>
#include <mpi.h>
#include "aes.h"

/*
 * compile
 * mpicc mpi-hello.c aes.c -o mpi-hello
 * 
 * Run
 * mpirun -np 2  executable
 * 
 * 
 *
 * */

struct AES_ctx ctx;

unsigned long ToUInt(char* str)
{
    unsigned long mult = 1;
    unsigned long re = 0;
    int len = strlen(str);
    for(int i = len -1 ; i >= 0 ; i--)
    {
        re = re + ((int)str[i] -48)*mult;
        mult = mult*10;
    }
    return re;
}


int main(int argc, char** argv) {

    int size,rank=0;

    MPI_Init(NULL, NULL);

    // Get the number of processes
    int world_size;
    MPI_Comm_size(MPI_COMM_WORLD, &size);

    // Get the rank of the process
    int world_rank;
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);

    int data = 532123;
    uint8_t data_file[10];

    printf("this is in main\n");

    if (rank == 0) {
        printf("data rank: 0: %d\n", data);
        //convert in to string
        sprintf( data_file, "%d", data);
        //string
        printf("data rank: 0: %s\n", data_file);

        //encrypt data
        AES_ECB_encrypt(&ctx, data_file);

        printf("Sending in 0 after crypt: %s\n", data_file);
        printf("strlen send: %d\n", strlen(data_file));

        MPI_Send(data_file,16,MPI_UINT8_T,1,0,MPI_COMM_WORLD);

    }else{

        MPI_Recv(data_file,16,MPI_UINT8_T,0,0,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
        printf("Recieved in 1 before: %s\n",data_file);
        printf("strlen recv: %d\n", strlen(data_file));

        AES_ECB_decrypt(&ctx, data_file);

        printf("DEC at Rank 1 string: %s\n", data_file);
        printf("DEC at Rank 1 int: %u\n", ToUInt(data_file));
    }


    MPI_Finalize();
}

编辑2

在评论中应用请求。

#include <stdio.h>
#include <string.h>
#include <mpi.h>
#include "aes.h"

/*
 * compile
 * mpicc mpi-hello.c aes.c -o mpi-hello
 * 
 * Run
 * mpirun -np 2  executable
 * 
 * 
 * */


unsigned long ToUInt(char* str)
{
    unsigned long mult = 1;
    unsigned long re = 0;
    int len = strlen(str);
    for(int i = len -1 ; i >= 0 ; i--)
    {
        re = re + ((int)str[i] -48)*mult;
        mult = mult*10;
    }
    return re;
}


int main(int argc, char** argv) {

    int size,rank=0;

    MPI_Init(NULL, NULL);

    // Get the number of processes
    int world_size;
    MPI_Comm_size(MPI_COMM_WORLD, &size);

    // Get the rank of the process
    int world_rank;
    MPI_Comm_rank(MPI_COMM_WORLD, &rank);

    int data = 532123;
    uint8_t data_file[10];


     //start Crypto Section

    //~ key and iv
    ///

    uint8_t key[32] = { 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
                        0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4 
    };

    uint8_t iv[16]  = { 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
    };
    ///    

    struct AES_ctx ctx;


    printf("this is in main\n");

    if (rank == 0) {
        printf("data rank: 0: %d\n", data);
        //convert in to string
        sprintf( data_file, "%d", data);
        //string
        printf("data rank: 0: %s\n", data_file);

        ///Enc

        //set
        AES_init_ctx_iv(&ctx, key, iv); 

        //enc
        AES_CTR_xcrypt_buffer(&ctx, data_file, strlen(data_file));


        printf("Sending in 0 after crypt: %s\n", data_file);
        printf("strlen send: %d\n", strlen(data_file));

        MPI_Send(data_file,10,MPI_UINT8_T,1,0,MPI_COMM_WORLD);

    }else{

        MPI_Recv(data_file,10,MPI_UINT8_T,0,0,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
        printf("Recieved in 1 before: %s\n",data_file);
        printf("strlen recv: %d\n", strlen(data_file));

        ///Dec
        //set
        AES_init_ctx_iv(&ctx, key, iv); 

        //dec
        AES_CTR_xcrypt_buffer(&ctx, data_file, strlen(data_file));  

        printf("DEC at Rank 1 string: %s\n", data_file);
        printf("DEC at Rank 1 int: %u\n", ToUInt(data_file));
    }


    MPI_Finalize();
}
if (rank == 0) {
    printf("data rank: 0: %d\n", data);
    //convert in to string
    sprintf( data_file, "%d", data);
    //string
    printf("data rank: 0: %s\n", data_file);

    //~ key and iv
    ///

    uint8_t key[32] = { 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
                        0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4 
    };

    uint8_t iv[16]  = { 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
    };
    ///    
    ///Enc

    //set
    AES_init_ctx_iv(&ctx, key, iv); 

    //enc
    AES_CTR_xcrypt_buffer(&ctx, data_file, strlen(data_file));


    printf("Sending in 0 after crypt: %s\n", data_file);
    printf("strlen send: %d\n", strlen(data_file));

    MPI_Send(data_file,10,MPI_UINT8_T,1,0,MPI_COMM_WORLD);

}else{

    MPI_Recv(data_file,10,MPI_UINT8_T,0,0,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
    printf("Recieved in 1 before: %s\n",data_file);
    printf("strlen recv: %d\n", strlen(data_file));



    //~ key and iv
    ///

    uint8_t key[32] = { 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
                        0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4 
    };

    uint8_t iv[16]  = { 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
    };
    ///    
    ///Dec
    //set
    AES_init_ctx_iv(&ctx, key, iv); 

    //dec
    AES_CTR_xcrypt_buffer(&ctx, data_file, strlen(data_file));  

    printf("DEC at Rank 1 string: %s\n", data_file);
    printf("DEC at Rank 1 int: %u\n", ToUInt(data_file));
}
例如,您可以在if或else中更改密钥的第一个元素,即:
0x60->0x61
,然后重新编译并运行程序,查看接收方是否没有正确的密钥,他/她是否无法解码您的加密数据

[ToDo]

  • 添加有关AES的更多说明

您是否先在单处理器上验证AES加密/解密?请提供完整且可重复的错误。@GuillaumePetitjean是的,在单处理器上,如果加密后将if作用域内的其他内容移动,并删除MPI_发送和接收,然后使用mpirun-np 1 a运行,则效果非常好。在那里,它将正常运行在我看来,所有被改变的都是加密方法,我最初做的是CTR one,而为你们工作的是ECB。为什么它不同,因为在github中,它说相同的CTR函数可以用于加密和解密。MPI send和recv的第二个参数从10改为16,原因可能是因为第二个参数基本上是告诉MPI正在发送或接收的任何东西的计数,在这种情况下是大小为10的数据文件???我更新了答案。哦,非常感谢你的友好回复,我非常感谢还有一件事。我看到在你的实现中,你没有使用任何键或iv,为什么?比如,我不应该使用我生成并加密的密钥,并且可以传递给其他人,让他们解密。再次感谢你的帮助,我没有意识到密钥的大小,这很重要。谢谢你,你是最棒的@JayedArRabbi在原始aes.c文件中使用了标准键和iv值。请参阅gitHub项目。
if (rank == 0) {
    printf("data rank: 0: %d\n", data);
    //convert in to string
    sprintf( data_file, "%d", data);
    //string
    printf("data rank: 0: %s\n", data_file);

    //~ key and iv
    ///

    uint8_t key[32] = { 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
                        0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4 
    };

    uint8_t iv[16]  = { 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
    };
    ///    
    ///Enc

    //set
    AES_init_ctx_iv(&ctx, key, iv); 

    //enc
    AES_CTR_xcrypt_buffer(&ctx, data_file, strlen(data_file));


    printf("Sending in 0 after crypt: %s\n", data_file);
    printf("strlen send: %d\n", strlen(data_file));

    MPI_Send(data_file,10,MPI_UINT8_T,1,0,MPI_COMM_WORLD);

}else{

    MPI_Recv(data_file,10,MPI_UINT8_T,0,0,MPI_COMM_WORLD,MPI_STATUS_IGNORE);
    printf("Recieved in 1 before: %s\n",data_file);
    printf("strlen recv: %d\n", strlen(data_file));



    //~ key and iv
    ///

    uint8_t key[32] = { 0x60, 0x3d, 0xeb, 0x10, 0x15, 0xca, 0x71, 0xbe, 0x2b, 0x73, 0xae, 0xf0, 0x85, 0x7d, 0x77, 0x81,
                        0x1f, 0x35, 0x2c, 0x07, 0x3b, 0x61, 0x08, 0xd7, 0x2d, 0x98, 0x10, 0xa3, 0x09, 0x14, 0xdf, 0xf4 
    };

    uint8_t iv[16]  = { 0xf0, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7, 0xf8, 0xf9, 0xfa, 0xfb, 0xfc, 0xfd, 0xfe, 0xff
    };
    ///    
    ///Dec
    //set
    AES_init_ctx_iv(&ctx, key, iv); 

    //dec
    AES_CTR_xcrypt_buffer(&ctx, data_file, strlen(data_file));  

    printf("DEC at Rank 1 string: %s\n", data_file);
    printf("DEC at Rank 1 int: %u\n", ToUInt(data_file));
}