-
Notifications
You must be signed in to change notification settings - Fork 0
/
crack_caesar_cipher.cpp
88 lines (83 loc) · 2.49 KB
/
crack_caesar_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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
/* Author: Syed Alfran Ali */
/* Implementing Caesar cipher Encryption and than Decrypting it. */
/* Implements any type of string characters either any special character or not, i.e. below program is not only constraint to alphabets both lower and upper case too. */
#include<bits/stdc++.h>
using namespace std;
int main(void)
{
int i;
char deCrypted[100];
cout << "Enter the string\n";
scanf("%[^\n]s",deCrypted);
int n = strlen(deCrypted);
int key = 3;
char enCrypted[n+1];
/* Encryption Process to Decrypt it further in the given code itself */
for(i=0;i<n;i++)
{
/* Handling for spaces */
if(deCrypted[i]==' ')
{
enCrypted[i] = ' ';
continue;
}
/* Handling for special symbols like @,#,$ ...*/
if((deCrypted[i] > 'Z' && deCrypted[i] < 'a') || deCrypted[i] < 'A')
{
enCrypted[i] = deCrypted[i];
}
if(deCrypted[i] > 'z' || (deCrypted[i] < 'a' && deCrypted[i] > 'Z'))
{
enCrypted[i] = deCrypted[i];
}
/* Handling for lower case alphabets */
if(deCrypted[i]<='z' && deCrypted[i]>='a')
{
enCrypted[i] = (deCrypted[i] + key);
if(enCrypted[i]>'z')
{
enCrypted[i] = deCrypted[i] - 'z' + 'a' + 2 ;
}
}
/* Handling for upper case alphabets */
if(deCrypted[i]<='Z' && deCrypted[i]>='A')
{
enCrypted[i] = deCrypted[i] + key;
if(enCrypted[i]>'Z')
{
enCrypted[i] = deCrypted[i] - 'Z' + 'A' + 2;
}
}
}
/* Printing out the Encrypted Text */
for(i=0;i<n;i++)
{
cout << enCrypted[i];
}
cout << endl;
/* Getting the --crack */
for(i=0;i<n;i++)
{
/* Handling for spaces */
if(deCrypted[i]==' ')
{
deCrypted[i] = ' ';
continue;
}
/* Handling the special character cases */
if(enCrypted[i]-deCrypted[i] < 0)
{
deCrypted[i] = enCrypted[i] + 26 - 3;
}
if(enCrypted[i]-deCrypted[i] > 0)
{
deCrypted[i] = enCrypted[i] - 3;
}
}
/* Printing the cracked value i.e. The Plain Text(Original)*/
for(i=0;i<n;i++)
{
cout << deCrypted[i] ;
}
cout << endl;
}