Last active
December 4, 2015 17:48
-
-
Save serhii-shnurenko/94795f6ed314ac5af074 to your computer and use it in GitHub Desktop.
Some basic encryption algorithms
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
//STUDYING MATERIAL | |
//shift encryption is not secure | |
char* encrypt(int k, char* message){ | |
char* temp=malloc(strlen(message)+1); | |
int i; | |
for(i=0;i<strlen(message);i++){ | |
temp[i]=(message[i]+k+26-'a')%26+'a'; | |
} | |
temp[i]=NULL; | |
return temp; | |
} | |
char* decrypt(int k, char* cipher){ | |
char* temp=malloc(strlen(cipher)+1); | |
int i; | |
for(i=0;i<strlen(cipher);i++){ | |
temp[i]=(cipher[i]-'a'+26-k)%26+'a'; | |
} | |
temp[i]=NULL; | |
return temp; | |
} | |
int main(){ | |
char* message = "shiftcipherisnotsecure"; | |
int key = 25; | |
printf("Shift encryption of %s with key %d: ",message,key); | |
char* cipher = encrypt(key,message); | |
printf(cipher); | |
printf("\n\nDecrypting cipher: %s \n",cipher); | |
message = decrypt(key,cipher); | |
printf("Result is %s\n",message); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <stdio.h> | |
//STUDYING MATERIAL | |
//vigenere encryption is not secure too | |
//encription function | |
char* encrypt(char* message, char* key){ | |
char* result; | |
result= malloc(strlen(message)+1); | |
int keyLength = strlen(key); | |
int i; //message index | |
int k=0; //key index | |
for(i=0;i<strlen(message);i++){ | |
result[i]=(message[i]+key[k]-2*'a')%26+'a'; | |
if(++k==keyLength) | |
k=0; | |
} | |
result[i] = message[i]; | |
return result; | |
} | |
char* decrypt(char* cipher, char* key){ | |
char* result; | |
result= malloc(strlen(cipher)+1); | |
int keyLength = strlen(key); | |
int i; //cipher index | |
int k=0; //key index | |
for(i=0;i<strlen(cipher);i++){ | |
result[i]=(cipher[i]-key[k]+2*26)%26+'a'; | |
if(++k==keyLength) | |
k=0; | |
} | |
//filling with last nullterminated symbol | |
result[i] = cipher[i]; | |
return result; | |
} | |
int main(){ | |
char* message = "thegreaestday"; | |
char* key = "security"; | |
char* cipher = encrypt(message,key); | |
printf("%s\n",cipher); | |
printf("%s\n",decrypt(cipher,key)); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment