File tree 2 files changed +136
-0
lines changed
2 files changed +136
-0
lines changed Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ #include < string.h>
3
+ #include < stdlib.h>
4
+ #include < time.h>
5
+ using namespace std ;
6
+ void encrypt (char x[]);
7
+ void decrypt (char y[]);
8
+ void R ();
9
+ int r;
10
+ main ()
11
+ {
12
+ char a [100 ];
13
+ cout<<" Enter text to be encrypted" <<endl;
14
+ cin.getline (a,100 );
15
+ cout<<" Key used for encryption will be: " <<endl;
16
+ R ();
17
+ cout<<" The encrypted text is: " <<endl;
18
+ encrypt (a);
19
+ cout<<a<<endl;
20
+ cout<<" The decrypted text is:" <<endl;
21
+ decrypt (a);
22
+ cout<<a<<endl;
23
+
24
+ }
25
+ void R ()
26
+ {
27
+ srand (time (NULL ));
28
+ r=rand () %26 +1 ;
29
+ cout<<r<<endl;
30
+ }
31
+ void encrypt (char x[])
32
+ {
33
+ for (int i=0 ; x[i]!=' \0 ' ; i++)
34
+ {
35
+ x[i]=x[i]+r;
36
+ }
37
+ }
38
+ void decrypt (char y[])
39
+ {
40
+ for (int j=0 ; y[j]!=' \0 ' ; j++)
41
+ {
42
+ y[j]=y[j]-r;
43
+ }
44
+ }
45
+
Original file line number Diff line number Diff line change
1
+ #include < iostream>
2
+ #include < string.h>
3
+ #include < stdlib.h>
4
+ #include < time.h>
5
+ #include < fstream>
6
+ using namespace std ;
7
+ void encrypt (char x[],int l);
8
+ void decrypt (char y[], int l);
9
+ int read (char f[]);
10
+ void R ();
11
+ int r;
12
+ char s[100 ];
13
+ main ()
14
+ {
15
+ int L,a,sl;
16
+ cout<<" Press 1 to encrypt and decrypt file OR press 2 to encypt and decrypt text entered by user" <<endl;
17
+ cin>>a;
18
+ {
19
+ if (a==1 )
20
+ {
21
+ char t[100 ];
22
+ cout<<" Enter file name you wish to encrypt" <<endl;
23
+ cin>>t;
24
+ cout<<" Key used for encryption will be: " <<endl;
25
+ R ();
26
+ L=read (t);
27
+ cout<<" Length of this text file is: " <<L-1 <<endl;
28
+ encrypt (s,L);
29
+ decrypt (s,L);
30
+ }
31
+ if (a==2 )
32
+ {
33
+ char u[100 ];
34
+ cout<<" Enter text you wish to encrypt: " <<endl;
35
+ cin>>u;
36
+ cout<<" Key used for encryption will be: " <<endl;
37
+ R ();
38
+ encrypt (s,L);
39
+ decrypt (s,L);
40
+ }
41
+ }
42
+ }
43
+ void R ()
44
+ {
45
+ srand (time (NULL ));
46
+ r=rand () %26 +1 ;
47
+ cout<<r<<endl;
48
+ }
49
+ int read (char f[])
50
+ {
51
+ int i=0 ;
52
+ int len=0 ;
53
+ ifstream myfile (f);
54
+ if (myfile.is_open ())
55
+ {
56
+ myfile>>noskipws;
57
+ while ( !myfile.eof ())
58
+ {
59
+ myfile >> s[i];
60
+ cout<<s[i];
61
+ i++;
62
+ len++;
63
+ }
64
+ cout<<endl;
65
+ }
66
+ else
67
+ {
68
+ cout << " File does not exist" <<endl;
69
+ exit (1 );
70
+ }
71
+ return len;
72
+ }
73
+
74
+ void encrypt (char x[], int l)
75
+ {
76
+ for (int i=0 ; i<=l; i++)
77
+ {
78
+ x[i]=x[i]+r;
79
+ }
80
+ cout<<" Encrypted Text is" <<endl;
81
+ cout<<x<<endl;
82
+ }
83
+ void decrypt (char y[], int l)
84
+ {
85
+ for (int j=0 ; j<=l; j++)
86
+ {
87
+ y[j]=y[j]-r;
88
+ }
89
+ cout<<" Decrypted Text is" <<endl;
90
+ cout<<y<<endl;
91
+ }
You can’t perform that action at this time.
0 commit comments