-
-
[旧帖]
[邀请码已发][原创]Crypto++大数运算
0.00雪花
-
发表于:
2009-5-25 20:35
1365
-
[旧帖] [邀请码已发][原创]Crypto++大数运算
0.00雪花
发一点菜鸟心得,希望高手们能不吝指教.
1.进制转换
#pragma comment (lib,"cryptlib.lib")
#include <integer.h> //Integer类的声明头文件。
#include <iostream>
using namespace CryptoPP; //使用名字空间CryptoPP
using namespace std; //使用名字空间std
int main(int argc, char* argv[])
{
Integer a;
cin>>a; //输入整数用后缀标识所用进制,h(16进制),o(8进制),b(2进制),不带后缀表示10进制。
cout<<"16进制:\t"<<hex<<a<<endl; //16进制输出数据。
cout<<"10进制:\t"<<dec<<a<<endl; //10进制输出数据。
cout<<"8进制:\t"<<oct<<a<<endl; //8进制输出数据。
cout<<"2进制:\t"; //2进制输出数据。
for(unsigned int i=0;i<a.BitCount();i++)
{
if(a.GetBit(a.BitCount()-1-i)) cout<<"1";
else cout<<"0";
}
cout<<"b\n";
return 0;
}
2.大数运算:
. . .
#include <nbtheory.h> //函数bool IsPrime(const Integer &p)的声明头文件。
. . .
Integer a("65646764313167497943131316454155");
Integer b("74646978643131316431312123");
Integer c("646768413134543132.2013216531213231263453511");
Integer d;
if(IsPrime(a)) //素数判定,非常强悍的一个函数。
cout<<"the first Integer is a prime\n";
else
cout<<"the first Integer isn't a prime\n";
d=a+b; //许多运算符被重载了,使用起来非常方便。
cout<<"a+b="<<d<<endl;
d=a-b;
cout<<"a-b="<<d<<endl;
d=a_times_b_mod_c(a,b,c); //乘积的模
cout<<"(a*b)mod c="<<d<<endl;
d=a_exp_b_mod_c (a,b,c) ; //幂的模
cout<<"(a^b)mod c="<<d<<endl;
. . .
3.Integer与CString。
在mfc应用程序中用Crypto++做大数运算时,我们常常需要从编辑框中输入数据到Integer变量,或者将Integer变量数据输出到编辑框中。
void CIntegerCStringDlg::OnTest()
{
// TODO: Add your control notification handler code here
UpdateData(1);
Integer a;
a=Integer(LPCTSTR(m_int));//将编辑框中的数据输入到Integer变量a中,m_int为编辑框关联的CString变量。
a=a.SquareRoot(); //平方根值,取整。
ostringstream buf; //string输出流
buf<<a; //将Integer变量a输出到buf流中,这里可以设置输出进制。跟cout用法相似。
m_int=(buf.str()).c_str(); //buf.str()返回的是一个string类,string::c_str()返回一个字符串指针。
UpdateData(0);
}
头文件增加的定义:
#pragma comment (lib,"cryptlib.lib") //加载lib文件的语句
#include <integer.h>
#include <sstream> //string流的声明文件。
sing namespace CryptoPP; //使用名字空间CryptoPP
using namespace std; //使用名字空间std
代码比较简单,注释已经很罗嗦了,就不浪费口水了.
[课程]FART 脱壳王!加量不加价!FART作者讲授!