Commit 140d2e16 authored by Pavel Grega's avatar Pavel Grega
Browse files

Add new file

parents
#include <stdio.h>
#include <string.h>
#include <ctype.h>
#include <stdlib.h>
#include <math.h>
#include "bmp.h"
unsigned char* bit_encrypt(const char* text){
if(text==NULL)
return NULL;
unsigned char* desttext = (unsigned char*)calloc(strlen(text)+1,sizeof(char));
for(int i=0;i<strlen(text);i++){
desttext[i]=text[i];
}
// prepisanie znaku do dvojkovej sustavy
int z=0;
do{
int new[8]={0};
char new1_tmp;
//for(int i=0; i<strlen((char*)desttext);i++){
char tmp=text[z];
int j=0;
do{
new1_tmp=tmp%2;
new[j]=(int)new1_tmp;
tmp=tmp/2;
j++;
}while((int)tmp!=0);
// }
for(int i=0;i<4;i++){
int o=new[i];
new[i]=new[7-i];
new[7-i]=o;
}
// prehodenie prvej stvorice
int o=new[0];
new[0]=new[1];
new[1]=o;
int o1=new[2];
new[2]=new[3];
new[3]=o1;
// prehodenie druhej stvorice
int q=0;
for(int i=4;i<8;i++){
if(new[i]==new[q])
new[i]=0;
else{
new[i]=1;
}
q++;
}
// vypocitanie hodnoty
int super=0;
for(int i=0;i<8;i++){
int mocnina=pow(2, 7-i);
super=super+new[i]*mocnina;
}
/* for(int i=0; i<8;i++){
printf("%d",new[i]);
}
printf("\n");
printf("%d\n", super);
*/
desttext[z]=super;
z++;
}while(z<strlen(text));
return desttext;
}
char* bit_decrypt(const unsigned char* text){
if(text==NULL)
return NULL;
char* pole = (char*)calloc(strlen((char*)text)+1,sizeof(char));
int z=0;
do{
int new[8]={0};
char new1_tmp;
char tmp=text[z];
int j=0;
do{
new1_tmp=tmp%2;
new[j]=(int)new1_tmp;
tmp=tmp/2;
j++;
}while((int)tmp!=0);
// robimm XOR opacne
int q=0;
for(int j=4;j<8;j++){
if(new[q]==new[j]){
new[j]=0;
}
else{
new[j]=1;
}
q++;
}
// prehodenie prvej stvorice
int o=new[0]; new[0]=new[1]; new[1]=o;
int o1=new[2]; new[2]=new[3]; new[3]=o1;
// vypocitanie hodnoty
int super=0;
for(int i=0;i<8;i++){
int mocnina=pow(2, 7-i);
super=super+new[i]*mocnina;
}
pole[z]=(char)super;
z++;
}while(z<strlen((char*)text));
pole[strlen((char*)text)]='\0';
return pole;
}
unsigned char* bmp_encrypt(const char* key, const char* text){
if(key==NULL || text==NULL)
return NULL;
for(int i=0;i<strlen(key);i++){
if(!isalpha(key[i]))
return NULL;
}
char* bmp_reverse = reverse(text);
char* bmp_vigenere = vigenere_encrypt((char*)key, (char*)bmp_reverse);
unsigned char* bmp_bit_encrypt = bit_encrypt(bmp_vigenere);
free(bmp_reverse);bmp_reverse=NULL;
free(bmp_vigenere);bmp_vigenere=NULL;
return bmp_bit_encrypt;
}
char* bmp_decrypt(const char* key, const unsigned char* text){
if(key==NULL || text==NULL)
return NULL;
if(key[0]=='\0' || text[0]=='\0')
return NULL;
for(int i=0;i<strlen(key);i++){
if(!isalpha(key[i]))
return NULL;
}
unsigned char* text1=(unsigned char*)text;
char* help = (char*)calloc(strlen((char*)text1)+1,sizeof(char));
// char* bmp_bit = bit_decrypt(text);
// char* bmp_vigenere = vigenere_decrypt(key, bmp_bit);
// char* bmp_reverse = reverse(bmp_vigenere);
// free(bmp_bit);bmp_bit=NULL;
// free(bmp_vigenere);bmp_vigenere=NULL;
// return bmp_reverse;
return help;
}
char* reverse(const char* text){
if(text == NULL){
return NULL;
}
int len=(int)strlen(text);
char* help = (char*)calloc(strlen(text)+1,sizeof(char));
for(int i=0;i<len;i++){
help[i] = (char)toupper(text[i]);
}
for(int j=0; j<len/2; j++){
char tmp=help[j];
help[j]=help[len-j-1];
help[len-j-1]=tmp;
}
help[len]='\0';
return help;
}
char* vigenere_encrypt(const char* key, const char* text){
if(key==NULL || text==NULL)
return NULL;
for(int i=0;i<strlen(key);i++){
if(!isalpha(key[i]))
return NULL;
}
int pismena=0;
for(int i=0;i<strlen(text);i++){
if(isalpha(text[i]))
pismena++;
}
if(pismena==0)
return NULL;
char* destkey = (char*)calloc(strlen(text)+1,sizeof(char));
char* desttext = (char*)calloc(strlen(text)+1,sizeof(char));
for(int i=0; i<strlen(text);i++){
desttext[i]=toupper(text[i]);
}
int counter=0;
for(int i=0; i<strlen(text);i++){
if(!isalpha(desttext[i])){
destkey[i]=desttext[i];
i++;
}
destkey[i]=toupper(key[counter]);
counter++;
if(counter==strlen(key)){
counter=0;
}
}
destkey[strlen(text)]='\0';
/* printf("%d\n",strlen(destkey));
printf("%s\n",destkey);
printf("%d\n",strlen(desttext));
printf("%s\n",desttext);
*/
for( int j=0;j<strlen(text); j++ ){
if(!isalpha(desttext[j])){
j++;
}
desttext[j] = 'A' + (((desttext[j] -'A') + ((destkey[j])-'A')) % 26);
}
desttext[strlen(text)]='\0';
free(destkey);
destkey=NULL;
return desttext;
}
char* vigenere_decrypt(const char* key, const char* text){
if(key==NULL || text==NULL)
return NULL;
for(int i=0;i<strlen(key);i++){
if(!isalpha(key[i]))
return NULL;
}
int pismena=0;
for(int i=0;i<strlen(text);i++){
if(isalpha(text[i]))
pismena++;
}
if(pismena==0)
return NULL;
char* destkey = (char*)calloc(strlen(text)+1,sizeof(char));
char* desttext = (char*)calloc(strlen(text)+1,sizeof(char));
for(int i=0; i<strlen(text);i++){
desttext[i]=toupper(text[i]);
}
int counter=0;
for(int i=0; i<strlen(text);i++){
if(!isalpha(desttext[i])){
destkey[i]=desttext[i];
i++;
}
destkey[i]=toupper(key[counter]);
counter++;
if(counter==strlen(key)){
counter=0;
}
}
destkey[strlen(text)]='\0';
/* printf("%d\n",strlen(destkey));
printf("%s\n",destkey);
printf("%d\n",strlen(desttext));
printf("%s\n",desttext);
*/
for( int j=0;j<strlen(text); j++ ){
if(!isalpha(desttext[j])){
j++;
}
desttext[j] = 'Z' - (('Z'-(desttext[j]) + ((destkey[j])-'A')) % 26);
}
desttext[strlen(text)]='\0';
free(destkey);
destkey=NULL;
return desttext;
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment