C++如何实现RSA数据加密的算法?
收起
知与谁同
2018-07-18 15:57:54
1913
0
1
条回答
写回答
取消
提交回答
-
void CRSAEncriptDlg::OnOK()
{
UpdateData(); //选取素数p和q
int p = 43;
int q = 59;
//计算n
int n = p*q;
int cn = (p-1)*(q-1); //选取b
int b = 5;
//选取a,使a*b-cn*x=1
int a =1949;
//将明文以两个字符为一组进行分组,以00表示a,01表示b,03表示c,......
if (m_Info.IsEmpty())
{
MessageBox("请输入加密信息");
return;
}
int len = m_Info.GetLength();
if (len %2 != 0)
{
MessageBox("输入的字符数必须为偶数");
return;
}
//存储明文的数字化格式
int iData[100];// = new int(len);
int index = 0;
CString str;
for (int i = 0 ; i< len; i++,index++)
{
int one = m_Info[i]-97;
int two = m_Info[i+1]-97;
if (two<10)
str.Format("%i0%i",one,two);
else
str.Format("%i%i",one,two);
iData[index] = atoi(str);
i++;
}
//对明文数字进行加密
//c= E(m) = m^b mod n m为明文数字
m_Encript = "";
for (i = 0 ; i< index ; i++)
{
iData[i] = ((UINT64) pow(iData[i],b)) % n;
if (i != index-1)
str.Format("%i-",iData[i]);
else
str.Format("%i",iData[i]);
m_Encript+=str;
}
UpdateData(FALSE);
}
2019-07-17 22:56:36