Files Classes Functions Hierarchy
00001 #include <cassert> 00002 #include <fstream> 00003 using namespace std; 00004 00005 #include <NTL/ZZ.h> 00006 using namespace NTL; 00007 00008 #include <aclock.h> 00009 #include <commandline.h> 00010 #include <primegen.h> 00011 #include <rsa.h> 00012 #include <rsastate.h> 00013 #include <streamconversion.h> 00014 00015 00016 00017 00018 rsastate::rsastate(int argc, char** argv) 00019 { 00020 commandline cmd(argc,argv); 00021 cmd.readfile("read"); 00022 00023 bool encryptb(false); 00024 bool decryptb(false); 00025 bool generateb(false); 00026 bool generate2b(false); 00027 00028 cmd.mapvar(encryptb,"encrypt"); 00029 cmd.mapvar(decryptb,"decrypt"); 00030 cmd.mapvar(generateb,"generate"); 00031 cmd.mapvar(generate2b,"generate2"); 00032 00033 cmd.mapvar(e,"e"); 00034 cmd.mapvar(n,"n"); 00035 cmd.mapvar(p,"p"); 00036 cmd.mapvar(q,"q"); 00037 cmd.mapvar(file,"file"); 00038 00039 blocksize=0; 00040 cmd.mapvar(blocksize,"blocksize"); 00041 nbits=20; 00042 cmd.mapvar(nbits,"nbits"); 00043 00044 cmd.enablehelp(); 00045 //cmd.enablehelp(cout); 00046 00047 if (encryptb) { encrypt(); return; } 00048 if (decryptb) { decrypt(); return; } 00049 if (generateb) { generate(); return; } 00050 if (generate2b) { generate2(); return; } 00051 00052 displaymessage(); 00053 } 00054 00055 void rsastate::encrypt() 00056 { 00057 if ( file.empty() || n.empty() || e.empty() 00058 || (blocksize==0) ) 00059 { 00060 cout << "error: one or more is incorrect" << endl; 00061 cout << " " << SHOW(file.empty()) << endl; 00062 cout << " " << SHOW(n.empty()) << endl; 00063 cout << " " << SHOW(blocksize) << endl; 00064 00065 return; 00066 } 00067 00068 cout << "Encrypting File" << endl; 00069 00070 string s; 00071 { 00072 ifstream f1(file.c_str()); 00073 asciitodig().forward(s,f1); 00074 //f1 >> s; 00075 } 00076 00077 digdiv d; 00078 d.blksz = blocksize; 00079 d.forward(s); 00080 00081 rsaE E; 00082 conv(E.e,e.c_str()); 00083 conv(E.n,n.c_str()); 00084 00085 d.eval(E); 00086 //printv(d.v); 00087 00088 //d.reverse(s); 00089 { 00090 ofstream f2(file.c_str()); 00091 d.save(f2); 00092 } 00093 00094 } 00095 00096 00097 void rsastate::decrypt() 00098 { 00099 if ( file.empty() ) 00100 { 00101 cout << "error: " << SHOW(file.empty()) << endl; 00102 return; 00103 } 00104 00105 if ( e.empty() ) 00106 { 00107 cout << "error: " << SHOW(e.empty()) << endl; 00108 return; 00109 } 00110 00111 if ( p.empty() ) 00112 { 00113 cout << "error: " << SHOW(p.empty()) << endl; 00114 return; 00115 } 00116 00117 if ( q.empty() ) 00118 { 00119 cout << "error: " << SHOW(q.empty()) << endl; 00120 return; 00121 } 00122 00123 cout << "Decrypting File" << endl; 00124 00125 digdiv d; 00126 { 00127 ifstream f1(file.c_str()); 00128 d.restore(f1); 00129 } 00130 00131 rsaD D; 00132 conv(D.e,e.c_str()); 00133 conv(D.p,p.c_str()); 00134 conv(D.q,q.c_str()); 00135 D.init(); 00136 00137 d.eval(D); 00138 00139 //printv(d.v); 00140 00141 //cout << SHOW(d.v.size()) << endl; 00142 //cout << SHOW(d.blksz) << endl; 00143 00144 { 00145 ofstream f2(file.c_str()); 00146 string s; 00147 d.reverse(s); 00148 asciitodig().reverse(f2,s); 00149 //f2 << s; 00150 //cout << s << endl; 00151 } 00152 } 00153 00154 void rsastate::generate() 00155 { 00156 rsaGenKey G(nbits); 00157 } 00158 00159 void rsastate::generate2() 00160 { 00161 if ( p.empty() || q.empty() ) 00162 { 00163 cout << "error: one or more is incorrect" << endl; 00164 cout << " " << SHOW(p.empty()) << endl; 00165 cout << " " << SHOW(q.empty()) << endl; 00166 00167 return; 00168 } 00169 00170 rsaGenKey G(nbits,p,q); 00171 00172 } 00173 00174 00175 00176 void rsastate::displaymessage() const 00177 { 00178 cout << endl; 00179 cout << "RSA encryption/decryption program." << endl; 00180 cout << endl; 00181 cout << "You can take full control as seen in example 2 or work with files" << endl; 00182 cout << " and let the application generate the key. This is the easiest way," << endl; 00183 cout << " but you should manage the key length through nbits. Two files" << endl; 00184 cout << " \"encrypt.txt\" and \"decrypt.txt\" are generated. Keep \"decrypt.txt\"" << endl; 00185 cout << " secret. Because the algorithm is public key it does not matter if someone" << endl; 00186 cout << " has both the encrypted and decrypted files they will not be able to use this" << endl; 00187 cout << " to find your key. Its safe to have the encrypted file public." << endl; 00188 cout << endl; 00189 cout << "Example 1" << endl; 00190 cout << " $./main rsa generate=true nbits=1000" << endl; 00191 cout << "To encrypt a file:" << endl; 00192 cout << " $./main rsa read=encrypt.txt file=msg.txt" << endl; 00193 cout << "To decrypt a file:" << endl; 00194 cout << " $./main rsa read=decrypt.txt file=msg.txt" << endl; 00195 cout << endl; 00196 00197 00198 cout << "Example 2" << endl; 00199 cout << "To encrypt a file:" << endl; 00200 cout << " $./main rsa encrypt=true file=msg.txt n=3337 e=79 blocksize=3" << endl; 00201 cout << "To decrypt a file:" << endl; 00202 cout << " $./main rsa decrypt=true p=47 q=71 file=msg.txt e=79" << endl; 00203 cout << endl; 00204 cout << "Paranoid: Imagine that there is a back door in this system. Even" << endl; 00205 cout << " if you think I am an honest person and would not do this to you," << endl; 00206 cout << " the random library number generator could have been tampered with," << endl; 00207 cout << " so the primes you think are difficult for another to generate could" << endl; 00208 cout << " be found by a superior enemy. Whatever your condition there is another" << endl; 00209 cout << " way to generate the primes. generate2 takes two numbers p and q and " << endl; 00210 cout << " sequentially searches for the first prime after that number. " << endl; 00211 cout << " Set numbits to be large which just adds this large number to p and q." << endl; 00212 cout << endl; 00213 cout << "Example 3" << endl; 00214 cout << " $./main rsa generate2=true nbits=4000 p=84982438458382834841 q=83839388477473777221" << endl; 00215 cout << "On my P3 it takes less than 10 minutes to generate the key." << endl; 00216 cout << endl; 00217 00218 } 00219 00220
1.5.8