-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunctions.c
More file actions
79 lines (66 loc) · 1.98 KB
/
functions.c
File metadata and controls
79 lines (66 loc) · 1.98 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "vgn.h"
#define limit 50 //If your letter or sentence has more than 50 character, you should increase this.
//APLHABETINDEX function finds a word's row in the alphabet. It is only finds one letter's index.
//alphabet is the string of alphabet
//word is a word whose i. letter needed.
//i is the index of queue
//THIS FUNCTION'S OUTPUT IS AN INDEX NUMBER OF A LETTER
int alphabetindex(char alphabet[], char word[], int i)
{
int a, index;
for(a=0; a<strlen(alphabet); a++)
{
if(word[i] == alphabet[a])
{
index= a;
}
}
return index;
}
//ENCRYPTION function encrypting a word by Method of Vigenere Cipher
//alphabet is the string of alphabet
//pass is the word which is wanted encrypting.
//key is key word
//THIS FUNCTION'S OUTPUT IS ENCRYPTED WORD'S POINTER
char* encryption(char alphabet[], char pass[], char key[])
{
int passindex=0, keyindex=0, stindex=0;
int i;
char store[limit];
for (i=0; i<strlen(pass); i++)
{
passindex= alphabetindex(alphabet, pass, i);
keyindex= alphabetindex(alphabet,key, i%strlen(key));
stindex= (passindex+keyindex) % 26;
store[i] = alphabet[stindex];
}
char *p;
p = malloc(sizeof(char)*strlen(pass));
strcpy(p, store);
return p;
}
//DECRYPTION function solves the secret word which was encrypted by Cipher.
//alphabet is the string of alphabet
//store is encrypted world
//key is key word
//THIS FUNCTION'S OUTPUT IS DECRYPTED WORD'S POINTER
char* decryption(char alphabet[], char store[], char key[])
{
int passindex=0, keyindex=0, stindex=0;
int i;
char password[limit];
for (i=0; i<strlen(store); i++)
{
keyindex= alphabetindex(alphabet, key, i%strlen(key));
stindex= alphabetindex(alphabet, store, i);
passindex= ((stindex-keyindex) +26) % 26;
password[i] = alphabet[passindex];
}
char *p;
p = malloc(sizeof(char)*strlen(store));
strcpy(p, password);
return p;
}