Files Classes Functions Hierarchy
00001 #include <cassert> 00002 #include <iostream> 00003 #include <fstream> 00004 using namespace std; 00005 00006 00007 #include <NTL/ZZ.h> 00008 using namespace NTL; 00009 00010 #include <aclock.h> 00011 #include <commandline.h> 00012 #include <primegen.h> 00013 #include <rsa.h> 00014 #include <rsatest.h> 00015 #include <streamconversion.h> 00016 00017 00018 int rsatest::unittest01() 00019 { 00020 cout << "Small RSA example with maths library." << endl; 00021 00022 ZZ p; 00023 conv(p,"47"); 00024 ZZ q; 00025 conv(q,"71"); 00026 ZZ n = p*q; 00027 ZZ n2 = (p-1)*(q-1); 00028 00029 ZZ e; 00030 conv(e,"79"); 00031 00032 ZZ d; 00033 InvMod(d,e,n2); 00034 00035 cout << "d=" << d << endl; 00036 00037 ZZ m; 00038 conv(m,"688"); 00039 cout << "m=" << m << endl; 00040 00041 ZZ c; 00042 PowerMod(c,m,e,n); 00043 cout << "c=" << c << endl; 00044 00045 ZZ m2; 00046 PowerMod(m2,c,d,n); 00047 cout << "D(c)=" << m2 << endl; 00048 00049 if (m!=m2) 00050 return 1; 00051 00052 return 0; 00053 } 00054 00055 int rsatest::unittest02() 00056 { 00057 cout << "Small RSA example with rsa classes." << endl; 00058 rsaE E; 00059 conv(E.e,"79"); 00060 conv(E.n,"3337"); //47*71=3337 00061 00062 string m1("688"); 00063 string c1; 00064 E(c1,m1); 00065 cout << "c1=" << c1 << endl; 00066 00067 rsaD D; 00068 conv(D.e,"79"); 00069 conv(D.p,"47"); 00070 conv(D.q,"71"); 00071 D.init(); 00072 00073 00074 string m2; 00075 D(m2,c1); 00076 cout << "m2=" << m2 << endl; 00077 00078 if (m1!=m2) 00079 return 1; 00080 00081 return 0; 00082 } 00083 00084 void rsatest::test01() 00085 { 00086 string s; 00087 ifstream f1("main.cpp"); 00088 asciitodig::forward(s,f1); 00089 ofstream f2("tmp11.txt"); 00090 asciitodig::reverse(f2,s); 00091 00092 digdiv d; 00093 d.blksz = 5; 00094 d.forward(s); 00095 // printv(d.v); 00096 ofstream f3("tmp12.txt"); 00097 string s2; 00098 d.reverse(s2); 00099 asciitodig::reverse(f3,s2); 00100 } 00101 00102 void rsatest::test02(int argc, char** argv) 00103 { 00104 commandline c(argc,argv); 00105 unsigned int nbits(200); 00106 c.mapvar(nbits,"nbits"); 00107 00108 largePrimeGenerator p; 00109 00110 aclock clk; 00111 clk.measure(); 00112 ZZ n; 00113 unsigned int imax=30; 00114 for ( unsigned int i=0; i<imax; ++i ) 00115 { 00116 p.findprime_sequentially(n,nbits); 00117 cout << n << endl; 00118 } 00119 clk.measure(); 00120 cout << "sequential time(s)=" << clk.diff_s() << endl; 00121 00122 for ( unsigned int i=0; i<imax; ++i ) 00123 { 00124 p.findprime_randomly(n,nbits); 00125 cout << n << endl; 00126 } 00127 clk.measure(); 00128 cout << "random time(s)=" << clk.diff_s() << endl; 00129 } 00130 00131
1.5.8