当前位置:编程学习 > C/C++ >>

C++如何实现RSA数据加密的算法?

答案: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);
}

上一个:推荐学习C++的一本书
下一个:谁能解释下这个C++什么意思

CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,