我正在尝试写入crypt->public_key->data
二进制文件。如果我使用 size ofsizeof(unsigned int)
作为 中的第二个参数fwrite()
,它可以工作。但是,它unsigned short *
在头文件中被声明为类型。我不确定它为什么会这样。此外,我在编写公钥和阅读它们时也遇到了问题。fwrite()
虽然我在and中使用了完全相同类型的大小fread()
。
编辑:似乎我的 crypt->public_key->data 的大小不正确,正如 usr2564301 所指出的那样。
*我不确定是否需要引用此代码的来源。但我会在此处添加 URL:https ://github.com/Varad0612/The-McEliece-Cryptosystem
来自 matrix.h 的代码
typedef struct matrix
{
int rows; //number of rows.
int cols; //number of columns.
unsigned short *data;
}*bin_matrix;
这是来自 mceliece.c 的代码
//Initialize the mceliece cryptosystem
mcc mceliece_init(int n0, int p, int w, int t)
{
FILE *publicKey, *privateKey;
mcc crypt;
crypt = (mcc)safe_malloc(sizeof(struct mceliece));
//crypt->code = qc_mdpc_init(n0, p, w, t);
//crypt->public_key = generator_matrix(crypt->code);
//printf("%d\n",crypt->public_key->rows);
//printf("%d\n",crypt->public_key->cols);
//Write public key into a binary file
/*publicKey = fopen("PublicKey.bin", "wb");
privateKey = fopen("PrivateKey.bin", "wb");
if(privateKey != NULL){
fwrite(crypt->code->row, n0*p*sizeof(unsigned short), n0 * p, privateKey);
fclose(privateKey);
}
else{
printf("Unable to write private key\n");
}
//Write public key into a binary file
if(publicKey != NULL){
fwrite(crypt->public_key->data, p*p*n0*sizeof(unsigned short), crypt->public_key->rows*crypt->public_key->cols, publicKey);
fclose(publicKey);
}
else{
printf("Unable to write public key\n");
}*/
//Read private key from a binary file
crypt->code = (mdpc)safe_malloc(sizeof(struct qc_mdpc));
crypt->code->n0 = n0;
crypt->code->p = p;
crypt->code->w = w;
crypt->code->t = t;
crypt->code->n = n0 * p;
crypt->code->r = p;
crypt->code->k = (n0 - 1) * p;
crypt->code->row = (unsigned short*)calloc(n0 * p, sizeof(unsigned short));
privateKey = fopen("PrivateKey.bin", "rb");
if(privateKey != NULL){
fread(crypt->code->row, p*n0*sizeof(unsigned short), p*n0, privateKey);
fclose(privateKey);
}
else
printf("Unable to read private key\n");
//Read public key from a binary file
/*crypt->public_key = (bin_matrix)safe_malloc(sizeof(struct matrix));
crypt->public_key->data = (unsigned short*)safe_malloc(p*p*n0*sizeof(unsigned short));
crypt->public_key->rows = p;
crypt->public_key->cols = n0*p;
publicKey = fopen("PublicKey.bin", "rb");
if(publicKey != NULL){
fread(crypt->public_key->data, p*p*n0*sizeof(unsigned short), crypt->public_key->rows*crypt->public_key->cols, publicKey);
fclose(publicKey);
}
else{
printf("Unable to read public key\n");
}*/
printf("Successful\n");
//printf("mceliece generated...\n");
return crypt;
}