-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvigenere_decrypt.c
More file actions
46 lines (34 loc) · 926 Bytes
/
vigenere_decrypt.c
File metadata and controls
46 lines (34 loc) · 926 Bytes
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
// Author - Michael Ibeh
// License - Apache Version 2.0
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include "CipherSolve.h"
void vigenere_decrypt(void){
char buffer[1024], key[1024], plaintext[1024],ciphertext[1024];
int message_len, keylen, i, j;
printf("What is the key?\n");
scanf("%s", buffer);
strcpy(key, buffer);
keylen = strlen(key);
for(i = 0; i < keylen; i++){
key[i] = tolower(key[i]);
key[i] -= 'a';
}
printf("What is the message you would like decrypted?\n");
scanf("%s", buffer);
strcpy(ciphertext, buffer);
message_len = strlen(ciphertext);
for(i = 0, j = 0; i < message_len; i++, j++){
j %= keylen;
plaintext[i] = tolower(ciphertext[i]);
plaintext[i] -= 'a';
plaintext[i] -= key[j];
if(plaintext[i] < 0)
plaintext[i] += 26;
plaintext[i] %= 26;
plaintext[i] += 'a';
}
plaintext[i] = '\0';
printf("The decrypted string is: %s\n", plaintext);
}