-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathC7E4CryptogramV2.c
140 lines (86 loc) · 3.78 KB
/
C7E4CryptogramV2.c
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/* Challanges Ch7E4 - CryptogramV2 */
// By Jim-Kristian Malmhaug | Date: 8 October 2013
//Notes: This encrytption program uses the standard adding as given beforehand, and a new fliping of letter technique.
//References:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
void encrypt(char [], int); //function prototype for encryption
void decrypt(char [], int); //function prototype for decryption
main() {
char myString[21] = {0};
int iSelection = 0;
int iRand;
srand(time(NULL));
iRand = ((rand() % 4) + 1); //random #, 1-4
while(iSelection != 4) {
system("clear");
printf("\n\n*** CRYPTOGRAM V2 ***\n\nWhat will You do?\n\n1. Encrypt Clear Text\n2. Decrypt Cipher Text\n3. Generate New Key\n4. Quit\n\n---> ");
scanf("%d", &iSelection);
switch(iSelection) {
case 1:
printf("\nEnter one word as clear text to encrypt (less than 22 characters): ");
scanf("%s", myString);
encrypt(myString, iRand);
break;
case 2:
printf("\nEnter cipher text to decrypt: ");
scanf("%s", myString);
decrypt(myString, iRand);
break;
case 3:
iRand = ((rand() % 4) + 1); //random #, 1-4
printf("\nNew Key Generated\n");
sleep(2);
break;
}
}
} //End of main()-function
/*************************************************
Function definition
encrypt()
*************************************************/
void encrypt(char sMessage[], int random) {
int x = 0;
char cTempChar = '\0';
//---------------------------------------------------encrypt the message by shifting each charcter ASCII value
while(sMessage[x]) {
sMessage[x] += random;
x++;
}
//---------------------------------------------------then the message is being flipped so that the last letter is the first letter
cTempChar = sMessage[x-1];
sMessage[x-1] = sMessage[0];
sMessage[0] = cTempChar;
x = 0;
printf("\nEncrypted message is: ");
//---------------------------------------------------print the encrypted message
while(sMessage[x]) {
printf("%c", sMessage[x]);
x++;
}
}
/*************************************************
Function definition
decrypt()
*************************************************/
void decrypt(char sMessage[], int random) {
int x = 0;
char cTempChar = '\0';
//----------------------------------------------------decrypt the message by shifting each character ASCII value
while(sMessage[x]) {
sMessage[x] = sMessage[x] - random;
x++;
}
//---------------------------------------------------then the message is being flipped so that the last letter is the first letter
cTempChar = sMessage[x-1];
sMessage[x-1] = sMessage[0];
sMessage[0] = cTempChar;
x = 0;
printf("\nDecrypted message is: ");
//----------------------------------------------------print the decrypted message
while(sMessage[x]) {
printf("%c", sMessage[x]);
x++;
}
}