Pages

Sunday, March 30, 2014

Hill Cipher algorithm in C with arrays

Ok, I thought that would be nice to post the algorithm for Hill cipher but you know its my work and I can say that I work a lot on that application. So, I knew there is another algorithm on internet, HERE, but is less efficient than the other one which I wrote. And I explain why is less efficient!
Despite the fact that the code is much easier this algorithm stores the message in an array and the number of rows and colls of the KEY matrix must be equal with the number of elements of the array.
For example, if you have a message from 10 characters you must have a square matrix ( KEY) with 10 rows and 10 colls. And I think thats annoying always to create new keys for different messages.
But if somebody want to play sending encrypted messages to his friends, this is the code! All you have to do is to compile it!
-------------------------------------------------------------------
main.c

  1 #include <stdio.h>
2 #include <stdlib.h>
3 #include "def.c"
4 #include "criptare.c"
5 #include "decriptare.c"
6 #include "functii.c"
7 void criptare_mesaj(){
8 /*-----------------initializare fisiere--------------------------*/
9 fkey=fopen("key.txt","r");
10 fcript=fopen("encrypted_message.txt","w");
11 /*------------------CRIPTARE-------------------------------------*/
12 B.vectr = (char *) malloc(1);
13 if (!B.vectr) {
14 printf("insufficient memory.
"
);
15 exit(EXIT_FAILURE);
16 }
17 B.vectr[0] = 0;
18 k = 0;
19 while ((caract = getchar()) != EOF) {
20 aux.vectr = (char *) realloc(B.vectr, n + 2);
21 if (!aux.vectr) {
22 free(B.vectr);
23 printf("Redimension cannot be done!
"
);
24 exit(EXIT_FAILURE);
25 } else {
26 B.vectr = aux.vectr;
27 }
28 B.vectr[n] = caract;
29 B.vectr[n + 1] = 0;
30 n++;
31 }
32 // printf("%s %d", B.vectr,n);
33 K.lin=K.col=C.elem=D.elem=V.elem=A.elem=n-1;
34 K.matr=(double **)malloc(K.lin*sizeof(double*));
35 C.vector=(double *)malloc(C.elem*sizeof(double));
36 D.vector=(double *)malloc(D.elem*sizeof(double));
37 for(i=0;i<K.lin;i++)
38 if((K.matr[i]=(double *)malloc(K.col*sizeof(double)))==NULL)
39 {
40 printf("
insufficient memory"
);
41 exit(1) ;
42 }
43 else
44 if(C.vector==NULL || D.vector==NULL )
45 {
46 printf("
insufficient memory"
);
47 exit(1) ;
48 }
49 citire_cheie(K);
50 initializare_criptare(D,B);
51 afisare(D);
52 free(B.vectr);
53 criptare(D,C,K);
54 afisare(C);
55 modulo(C,d);
56 afisare_fisier(d);
57 free(K.matr);
58 free(D.vector);
59 free(C.vector);
60 fclose(fkey);
61 fclose(fcript);
62 }
63
64 void decriptare_mesaj(){
65 /*-----------------initializare fisiere--------------------------*/
66 fkey=fopen("second_key.txt","r");
67 fcript=fopen("encrypted_message.txt","r");
68 fdecript=fopen("decrypted_message.txt","w");
69 /*--------------------------decriptare----------------------------*/
70
71 fscanf(fcript,"%d",&C.elem);
72 D.elem=K.lin=K.col=n=C.elem;
73 B.elemente=n;
74 K.matr=(double **)malloc(K.lin*sizeof(double*));
75 B.vectr=(char *)malloc(B.elemente*sizeof(char));
76 C.vector=(double *)malloc(C.elem*sizeof(double));
77 D.vector=(double *)malloc(D.elem*sizeof(double));
78 for(i=0;i<K.lin;i++)
79 if((K.matr[i]=(double *)malloc(K.col*sizeof(double)))==NULL)
80 {
81 printf("
insufficient memory"
);
82 exit(1) ;
83 }
84 else
85 if(B.vectr==NULL || C.vector==NULL || D.vector==NULL)
86 {
87 printf("
insufficient memory"
);
88 exit(1) ;
89 }
90 citire_mesaj_decriptare(C);
91 citire_cheie(K);
92 decriptare(D,C,K);
93 modulo_decriptare(D,d);
94 initializare_decriptare(B,d);
95 free(K.matr);
96 free(D.vector);
97 free(C.vector);
98 free(B.vectr);
99 fclose(fkey);
100 fclose(fcript);
101 fclose(fdecript);
102 }
103
104 int main()
105 {int opt;
106 printf("Choose your operation! 1= encrypt 2=decrypt");
107 scanf("%d",&opt);
108 switch(opt){
109 case 1:{
110 printf("Enter message:
"
);
111 criptare_mesaj();}break;
112 case 2:{
113 printf("Decrypt was successfully done! Please ignore first character!");
114 decriptare_mesaj();}break;
115 }
116 return 0;
117 }

-------------------------------------------------------------------
criptare.c

 1 //initializarea criptarii a auxiliarului
2 void initializare_criptare(Array D, Caractere B){
3 for(i=0;i<D.elem;i++)
4 D.vector[i]=Tabel[B.vectr[i]];
5
6 }
7
8 //criptare propiu-zisa
9 void criptare(Array D, Array C, Matrice K){
10 for(i=0;i<K.lin;i++){
11 C.vector[i]=0;
12 for(j=0;j<K.col;j++)
13 C.vector[i]+=K.matr[i][j]*D.vector[j];
14 }
15 }
16
17
18 //aplicare modulo
19 void modulo(Array C, long d[500])
20 {
21 for(i=0;i<C.elem;i++)
22 d[i]=(long)C.vector[i];
23 for(i=0;i<C.elem;i++)
24 d[i]=d[i]%29;
25 }

-------------------------------------------------------------------
decriptare.c

 1 //citire mesaj
2 void citire_mesaj_decriptare(Array C)
3 {
4 for(i=0;i<C.elem;i++)
5 fscanf(fcript,"%lf",&C.vector[i]);
6
7 }
8 //void decriptare
9 void decriptare(Array D, Array C, Matrice K){
10 for(i=0;i<K.lin;i++){
11 D.vector[i]=0;
12 for(j=0;j<K.col;j++)
13 D.vector[i]+=K.matr[i][j]*C.vector[j];
14 }
15 }
16 //aplicare modulo
17 void modulo_decriptare(Array D, long d[500])
18 {
19 for(i=0;i<C.elem;i++)
20 d[i]=(long)D.vector[i];
21 for(i=0;i<C.elem;i++)
22 d[i]=d[i]%29;
23 }
24 //
25 //initializare decriptare
26 void initializare_decriptare(Caractere B, long d[500]){
27 for(i=0;i<B.elemente;i++)
28 B.vectr[i]=TabelI[d[i]];
29 fputs(B.vectr,fdecript);
30 }

-------------------------------------------------------------------
def.c

 1 typedef struct{
2 double *vector;
3 int elem;
4 }Array;
5 typedef struct{
6 char *vectr;
7 int elemente;
8 }Caractere;
9 typedef struct{
10 double **matr;
11 int lin;
12 int col;
13 }Matrice;
14 Matrice K;
15 Array V,C,D,A;//A= auxiliar
16 Caractere B,aux;//B= vector de caractere
17 int i,n,j,caract,k;
18 double m;
19 long d[500];//vectorul de modulo
20 const signed char Tabel[128] = {
21 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
22 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
23 28,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,26,-1,
24 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,27,
25 -1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,
26 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,
27 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,
28 -1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1};
29 const char TabelI[29]={A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,X,Y,Z,.,?, };
30
31 FILE *fkey,*fcript,*fdecript;

-------------------------------------------------------------------
functii.c

 1 //afisare mesaj
2 void afisare(Array A)
3 {
4 for(i=0;i<A.elem;i++)
5 printf("%.0lf ",A.vector[i]);
6
7 }
8 //citire cheie
9 void citire_cheie(Matrice k){
10 for(i=0;i<K.lin;i++)
11 for(j=0;j<K.col;j++)
12 fscanf(fkey,"%lf",&K.matr[i][j]);
13 }
14 //afisare_fisier
15 void afisare_fisier(long d[500]){
16 fprintf(fcript,"%d
"
,n-1);
17 for(i=0;i<n-1;i++)
18 fprintf(fcript,"%ld ",d[i]);
19 }

-------------------------------------------------------------------
PS: The efficient algorithm for Hill Cipher was written in C code and a video with it was published HERE!



Related Posts by Categories

0 comments:

Post a Comment