1 /*
2 * Program to encipher text using Blum-Goldwasser Probabalistic
3 * Public Key method
4 * See "Modern Cryptology - a tutorial" by Gilles Brassard.
5 * Published by Springer-Verlag, 1988
6 *
7 * Define RSA to use cubing instead of squaring. This is no longer
8 * provably as difficult to break as factoring the modulus, its a bit
9 * slower, but it is resistant to chosen ciphertext attack.
10 *
11 * Note: This implementation uses only the Least Significant Byte
12 * of the big random number x in encipherment/decipherment, as it has
13 * proven to be completely secure. However it is conjectured that
14 * that up to half the bytes in x (the lower half) are also secure.
15 * They could be used to considerably speed up this method.
16 *
17 * Requires: big.cpp
18 */
19
20 #include <iostream>
21 #include <fstream>
22 #include "big.h" /* include MIRACL system */
23 #include <cstring>
24
25 using namespace std;
26
27 // #define RSA
28
29 Miracl precision=100;
30
strip(char * name)31 void strip(char *name)
32 { /* strip off filename extension */
33 int i;
34 for (i=0;name[i]!='\0';i++)
35 {
36 if (name[i]!='.') continue;
37 name[i]='\0';
38 break;
39 }
40 }
41
main()42 int main()
43 { /* encipher using public key */
44 Big x,ke;
45 ifstream public_key("public.key");
46 ifstream input_file;
47 ofstream output_file,key_file;
48 char ifname[13],ofname[13];
49 BOOL fli;
50 char ch;
51 long seed,ipt;
52 miracl *mip=&precision;
53
54 mip->IOBASE=16;
55 public_key >> ke;
56 cout << "Enter 9 digit random number seed = ";
57 cin >> seed;
58 irand(seed);
59 x=rand(ke);
60 cout << "file to be enciphered = ";
61 cin >> ifname;
62 // cin.sync();
63 // cin.getline(ifname,13);
64 fli=FALSE;
65 if (strlen(ifname)>0) fli=TRUE;
66 if (fli)
67 { /* set up input file */
68 strcpy(ofname,ifname);
69 strip(ofname);
70 strcat(ofname,".blg");
71 input_file.open(ifname,ios::in);
72 if (!input_file)
73 {
74 cout << "Unable to open file " << ifname << "\n";;
75 return 0;
76 }
77 cout << "enciphering message" << endl;
78 }
79 else
80 { /* accept input from keyboard */
81 cout << "output filename = ";
82 cin >> ofname;
83 strip(ofname);
84 strcat(ofname,".blg");
85 cout << "input message - finish with cntrl z" << endl;
86 }
87 output_file.open(ofname,ios::binary|ios::out);
88 ipt=0;
89 forever
90 { /* encipher character by character */
91 #ifdef RSA
92 x=pow(x,3,ke);
93 #else
94 x=(x*x)%ke;
95 #endif
96 if (fli)
97 {
98 if (input_file.eof()) break;
99 input_file.get(ch);
100 }
101 else
102 {
103 if (cin.eof()) break;
104 cin.get(ch);
105 }
106 ch^=x[0]; /* XOR with last byte of x */
107 output_file << ch;
108 ipt++;
109 }
110 strip(ofname);
111 strcat(ofname,".key");
112 key_file.open(ofname);
113 key_file << ipt << "\n";
114 key_file << x << endl;
115 return 0;
116 }
117
118