16,472
社区成员
发帖
与我相关
我的任务
分享
void CCCCdemoDlg::OnBnClickedEccdecrypt()
{
// TODO: 在此添加控件通知处理程序代码
this->UpdateData();
int buflen, strlen;
CString result;
strlen = this->m_ECCciphertext.GetLength();
if(strlen == 0){
this->m_ECCplaintext.Format("ERROR 011. The ciphertext is NULL!");
this->UpdateData(false);
return;
}
if(strlen % 2)
buflen = strlen / 2 + 1;
else
buflen = strlen / 2;
if(buflen <= ECIES_OVERHEAD){
this->m_ECCplaintext.Format("ERROR 012. The ciphertext is illegal!");
this->UpdateData(false);
return;
}
char *hextext = this->m_ECCciphertext.GetBuffer(strlen);
char *plainstr = (char *)malloc(buflen - ECIES_OVERHEAD);
char *cipherstr = (char *)malloc(buflen);
if((strlen > 624) || (Hextext2Bitstr(cipherstr, strlen, hextext) != 0)){ //624 = 256*2+56*2
this->m_ECCplaintext.Format("ERROR 013. The ciphertext is too long!");
this->UpdateData(false);
return;
}
if(ECIES_Decryption(plainstr, cipherstr, buflen - ECIES_OVERHEAD) < 0){
result.Format("ERROR 019. The ciphertext is illegal! Please check it again and the possibility of attack is not excluded.");
}
else{
result.Format("%s",plainstr);
}
this->m_ECCplaintext = result;
this->UpdateData(false);
free(plainstr);
free(cipherstr);
}
int Hextext2Bitstr(char *bitstr, int len, char *hextext)
{
int i = 0, j = 0;
char *lsData = NULL;
char *lsReply = NULL;
if(hextext == NULL){
return -1;
}
if(bitstr == NULL){
return -1;
}
lsData = new char[len];
lsReply = new char[len];
memcpy(lsData, hextext, len);
for(i = 0; i < len; i++){
if(lsData[i] >= 65 && lsData[i] <= 70){
lsReply[j] = lsData[i] - 55;
i++;
if(lsData[i] >= 65 && lsData[i] <= 70){
lsReply[j] = ((lsReply[j] << 4) | ( lsData[i] - 55));
j++;
}
else if (lsData[i] >= 48 && lsData[i] <= 57){
lsReply[j] = ((lsReply[j] << 4) | ( lsData[i] - 48));
j++;
}
}
else if (lsData[i] >= 48 && lsData[i] <= 57){
lsReply[j] = lsData[i] - 48;
i++;
if((lsData[i] >= 65 && lsData[i] <= 70)){
lsReply[j] = ((lsReply[j] << 4) | (lsData[i] - 55));
j++;
}
else if (lsData[i] >= 48 && lsData[i] <= 57){
lsReply[j] = ((lsReply[j] << 4) | ( lsData[i] - 48));
j++;
}
}
if(len == 1){
j++;
}
}
//对于奇数长度字符串,尾部二进制转换最后一个字节没有左移,产生4bit未用空间。
lsReply[j] = 0x00;
memcpy(bitstr, lsReply, j+1);
delete []lsReply;
delete []lsData;
return 0;
}