Commit 4507457d authored by Marek Gera's avatar Marek Gera
Browse files

Update bmp.c

parent c85a9ff5
...@@ -11,38 +11,38 @@ unsigned char* bit_encrypt(const char* text){ ...@@ -11,38 +11,38 @@ unsigned char* bit_encrypt(const char* text){
unsigned char* desttext = (unsigned char*)calloc(strlen(text)+1,sizeof(char)); unsigned char* desttext = (unsigned char*)calloc(strlen(text)+1,sizeof(char));
strcpy(desttext, text); strcpy(desttext, text);
// prepisanie znaku do dvojkovej sustavy // prepisanie znaku do dvojkovej sustavy
int z=0; int current_character=0;
do{ do{
int new[8]={0}; int binary[8]={0};
char new1_tmp; char binary_tmp;
char tmp=text[z]; char tmp=text[current_character];
int j=0; int j=0;
do{ do{
new1_tmp=tmp%2; binary_tmp=tmp%2;
new[j]=(int)new1_tmp; binary[j]=(int)binary_tmp;
tmp=tmp/2; tmp=tmp/2;
j++; j++;
}while((int)tmp!=0); }while((int)tmp!=0);
for(int i=0;i<4;i++){ for(int i=0;i<4;i++){
int o=new[i]; int o=binary[i];
new[i]=new[7-i]; binary[i]=binary[7-i];
new[7-i]=o; binary[7-i]=o;
} }
// prehodenie prvej stvorice // prehodenie prvej stvorice
int o=new[0]; int o=binary[0];
new[0]=new[1]; binary[0]=binary[1];
new[1]=o; binary[1]=o;
int o1=new[2]; int o1=binary[2];
new[2]=new[3]; binary[2]=binary[3];
new[3]=o1; binary[3]=o1;
// prehodenie druhej stvorice // prehodenie druhej stvorice
int q=0; int q=0;
for(int i=4;i<8;i++){ for(int i=4;i<8;i++){
if(new[i]==new[q]) if(binary[i]==binary[q])
new[i]=0; binary[i]=0;
else{ else{
new[i]=1; binary[i]=1;
} }
q++; q++;
} }
...@@ -50,12 +50,12 @@ unsigned char* bit_encrypt(const char* text){ ...@@ -50,12 +50,12 @@ unsigned char* bit_encrypt(const char* text){
int super=0; int super=0;
for(int i=0;i<8;i++){ for(int i=0;i<8;i++){
int mocnina=pow(2, 7-i); int mocnina=pow(2, 7-i);
super=super+new[i]*mocnina; super=super+binary[i]*mocnina;
} }
desttext[z]=super; desttext[current_character]=super;
z++; current_character++;
}while(z<strlen(text)); }while(current_character<strlen(text));
return desttext; return desttext;
} }
...@@ -63,16 +63,16 @@ unsigned char* bit_encrypt(const char* text){ ...@@ -63,16 +63,16 @@ unsigned char* bit_encrypt(const char* text){
char* bit_decrypt(const unsigned char* text){ char* bit_decrypt(const unsigned char* text){
if(text==NULL || strlen(text)==0) if(text==NULL || strlen(text)==0)
return NULL; return NULL;
char* pole = (char*)calloc(strlen((char*)text)+1,sizeof(char)); char* decrypted_array = (char*)calloc(strlen((char*)text)+1,sizeof(char));
int z=0; int current_character=0;
do{ do{
int new[8]={0}; int binary[8]={0};
char new1_tmp; char binary_tmp;
char tmp=text[z]; char tmp=text[current_character];
int j=0; int j=0;
do{ do{
new1_tmp=tmp%2; new1_tmp=tmp%2;
new[j]=(int)new1_tmp; binary[j]=(int)binary_tmp;
tmp=tmp/2; tmp=tmp/2;
j++; j++;
}while((int)tmp!=0); }while((int)tmp!=0);
...@@ -80,29 +80,29 @@ do{ ...@@ -80,29 +80,29 @@ do{
// robimm XOR opacne // robimm XOR opacne
int q=0; int q=0;
for(int j=4;j<8;j++){ for(int j=4;j<8;j++){
if(new[q]==new[j]){ if(binary[q]==binary[j]){
new[j]=0; binary[j]=0;
} }
else{ else{
new[j]=1; binary[j]=1;
} }
q++; q++;
} }
// prehodenie prvej stvorice // prehodenie prvej stvorice
int o=new[0]; new[0]=new[1]; new[1]=o; int o=binary[0]; binary[0]=binary[1]; binary[1]=o;
int o1=new[2]; new[2]=new[3]; new[3]=o1; int o1=binary[2]; binary[2]=binary[3]; binary[3]=o1;
// vypocitanie hodnoty // vypocitanie hodnoty
int super=0; int super=0;
for(int i=0;i<8;i++){ for(int i=0;i<8;i++){
int mocnina=pow(2, 7-i); int mocnina=pow(2, 7-i);
super=super+new[i]*mocnina; super=super+binary[i]*mocnina;
} }
pole[z]=(char)super; decrypted_array[z]=(char)super;
z++; z++;
}while(z<strlen((char*)text)); }while(current_character<strlen((char*)text));
pole[strlen((char*)text)]='\0'; decrypted_array[strlen((char*)text)]='\0';
return pole; return decrypted_array;
} }
unsigned char* bmp_encrypt(const char* key, const char* text){ unsigned char* bmp_encrypt(const char* key, const char* text){
...@@ -129,10 +129,10 @@ char* bmp_decrypt(const char* key, const unsigned char* text){ ...@@ -129,10 +129,10 @@ char* bmp_decrypt(const char* key, const unsigned char* text){
if(!isalpha(key[i])) if(!isalpha(key[i]))
return NULL; return NULL;
} }
unsigned char* text1=(unsigned char*)text; unsigned char* unsigned_text=(unsigned char*)text;
char* help = (char*)calloc(strlen((char*)text1)+1,sizeof(char)); char* result = (char*)calloc(strlen((char*)unsigned_text)+1,sizeof(char));
return help; return result;
} }
char* reverse(const char* text){ char* reverse(const char* text){
...@@ -140,20 +140,20 @@ char* reverse(const char* text){ ...@@ -140,20 +140,20 @@ char* reverse(const char* text){
return NULL; return NULL;
} }
int len=(int)strlen(text); int len=(int)strlen(text);
char* help = (char*)calloc(strlen(text)+1,sizeof(char)); char* result = (char*)calloc(strlen(text)+1,sizeof(char));
for(int i=0;i<len;i++){ for(int i=0;i<len;i++){
help[i] = (char)toupper(text[i]); result[i] = (char)toupper(text[i]);
} }
for(int j=0; j<len/2; j++){ for(int j=0; j<len/2; j++){
char tmp=help[j]; char tmp=result[j];
help[j]=help[len-j-1]; result[j]=result[len-j-1];
help[len-j-1]=tmp; result[len-j-1]=tmp;
} }
help[len]='\0'; result[len]='\0';
return help; return result;
} }
char* vigenere_encrypt(const char* key, const char* text){ char* vigenere_encrypt(const char* key, const char* text){
......
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