我刚开始拿起C,我正在使用代码中的RSA密码.但是,这行代码让我很困惑.积分在此处从此站点发送给作者.
char* intmsg = new char[strlen(msg)*3 + 1];
Run Code Online (Sandbox Code Playgroud)
这是可以找到该行的方法.
inline void encrypt(char* msg,FILE* fout)
{
/* This function actually does the encrypting of each message */
unsigned int i;
int tmp;
char tmps[4];
char* intmsg = new char[strlen(msg)*3 + 1];
/* Here, (mpz_t) M is the messsage in gmp integer
* and (mpz_t) c is the cipher in gmp integer */
char ciphertext[1000];
strcpy(intmsg,"");
for(i=0;i<strlen(msg);i++)
{
tmp = (int)msg[i];
/* print it in a 3 character wide format */
sprintf(tmps,"%03d",tmp);
strcat(intmsg,tmps);
}
mpz_set_str(M,intmsg,10);
/* free memory claimed by intmsg */
delete [] intmsg;
/* c = M^e(mod n) */
mpz_powm(c,M,e,n);
/* get the string representation of the cipher */
mpz_get_str(ciphertext,10,c);
/* write the ciphertext to the output file */
fprintf(fout,"%s\n",ciphertext);
}
Run Code Online (Sandbox Code Playgroud)
该代码行实际上不是C,它是C++.
char* intmsg = new char[strlen(msg)*3 + 1];
Run Code Online (Sandbox Code Playgroud)
意味着为给定数量的字符动态分配具有空间的内存块,比msg字符串的原始长度大3倍+ 1 .
C等效将是
char* intmsg = malloc(strlen(msg)*3 + 1);
Run Code Online (Sandbox Code Playgroud)
要解除分配该内存块,delete []intmsg可以在C++中使用,而如果malloc在C中使用,则可以使用free(intmsg);
| 归档时间: |
|
| 查看次数: |
322 次 |
| 最近记录: |