-
Notifications
You must be signed in to change notification settings - Fork 8
/
a-basic-stream-cipher.cpp
59 lines (56 loc) · 1.23 KB
/
a-basic-stream-cipher.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <bits/stdc++.h>
using namespace std;
#define faster ios_base::sync_with_stdio(false);cin.tie(NULL);cout.tie(NULL)
#define mkp make_pair
#define pb push_back
typedef long long int ll;
typedef long double ld;
int main()
{
cout<<"Network Security Assignment"<<endl;
cout<<endl;
cout<<"Enter String"<<endl;
string s;
cin>>s;
transform(s.begin(), s.end(), s.begin(), ::tolower);
int n=s.length();
int key[n];
cout<<endl;
cout<<"Key Generation..."<<endl;
for(int i=0;i<n;i++)
key[i]=rand()%10;
cout<<endl;
cout<<"Generated Key"<<endl;
for(ll i=0;i<n;i++)
cout<<key[i];
cout<<endl;
cout<<endl;
cout<<"Encrypted String"<<endl;
string str="";
char l;
for(ll i=0;i<n;i++)
{
if(s[i]+key[i]>122)
l=(s[i]+key[i]-1)-'z'+'a';
else
l=(s[i]+key[i]);
str+=l;
}
cout<<str<<endl;
cout<<endl;
cout<<"Now decrypting..."<<endl;
cout<<endl;
string ss="";
cout<<"Decrypted string"<<endl;
char p;
for(ll i=0;i<n;i++)
{
if(str[i]-key[i]<97)
p=(str[i]-key[i]+1)+'z'-'a';
else
p=(str[i]-key[i]);
ss+=p;
}
cout<<ss<<endl;
return 0;
}