Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdbool.h>
- #include <stdlib.h>
- #include "bmp.h"
- #define ALPHA "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
- void index_of_vigenere(const char arr[strlen(ALPHA)][strlen(ALPHA)], char key_char, char text_char, int *x, int *y){
- for (int i = 0; i < strlen(ALPHA); i++){
- // printf("%c %c \n", arr[i][0], text_char);
- if( arr[0][i] == text_char ){
- *x = i;
- break;
- }
- }
- for (int i = 0; i < strlen(ALPHA); i++){
- if( arr[i][0] == key_char ){
- *y = i;
- break;
- }
- }
- // printf("\n");
- }
- char vigenere_char_dec(const char arr[strlen(ALPHA)][strlen(ALPHA)], char key_char, char text_char){
- int x = 0;
- for (int i = 0; i < strlen(ALPHA); i++){
- if(arr[0][i] == key_char){
- x = i;
- break;
- }
- }
- for(int i = 0; i < strlen(ALPHA); i++){
- if( arr[i][x] == text_char){
- // printf("stlpec: %d riadok: %d znak: %c text: %c key: %c dec:%c\n",x,i, arr[i][x], text_char, key_char, arr[i][0]);
- return arr[i][0];
- }
- }
- return ' ';
- }
- char* reverse(const char* text){
- if(text == NULL){
- return NULL;
- }
- // MIREK
- // KERIM
- char *result = calloc(strlen(text)+1, sizeof *text);
- int size = strlen(text);
- for (int i = size-1; i >= 0; i--){
- if( text[i] >= 'a' && text[i] <= 'z' ){
- result[size-1-i] = text[i] - 32;
- } else {
- result[size-1-i] = text[i];
- }
- }
- result[strlen(text)] = '\0';
- return result;
- }
- char* vigenere_encrypt(const char* key, const char* text){
- if(key == NULL || text == NULL){
- return NULL;
- }
- char vigenere_arr[strlen(ALPHA)][strlen(ALPHA)];
- // vygenerovanie matrixu
- for (int i = 0; i < strlen(ALPHA); i++){
- for (int j = 0; j < strlen(ALPHA); j++){
- vigenere_arr[i][j] = ALPHA[i+j >= strlen(ALPHA) ? i+j-strlen(ALPHA) : i+j];
- }
- }
- // for (int i = 0; i < strlen(ALPHA); i++){
- // for (int j = 0; j < strlen(ALPHA); j++){
- // printf("%c ", matrix[i][j]);
- // }
- // printf("\n");
- // }
- // printf("\n%c\n", vigenere_arr[0][0]);
- // oprava kluca
- char right_key[strlen(key)];
- int wrong_letters = 0;
- for (int i = 0; i < strlen(key); i++){
- if((key[i] >= 'a' && key[i] <= 'z') || (key[i] >= 'A' && key[i] <= 'Z')){
- if(key[i] >= 'a' && key[i] <= 'z'){
- right_key[i-wrong_letters] = key[i]-32;
- } else {
- right_key[i-wrong_letters] = key[i];
- }
- } else {
- return NULL;
- wrong_letters++;
- }
- }
- right_key[strlen(key) - wrong_letters] = '\0';
- if(strlen(right_key) == 0){
- // free(right_key);
- return NULL;
- }
- // printf("%s %ld\n", right_key, strlen(right_key));
- // prirava textu
- char *encrypt_text = calloc(strlen(text) + 1, sizeof *text);
- for(int i = 0, key_pos = 0; i < strlen(text); key_pos++, i++){
- // //
- if((text[i] >= 'a' && text[i] <= 'z') || (text[i] >= 'A' && text[i] <= 'Z')){
- if(text[i] >= 'a' && text[i] <= 'z'){
- encrypt_text[i] = text[i]-32;
- } else {
- encrypt_text[i] = text[i];
- }
- int x, y;
- index_of_vigenere(vigenere_arr, right_key[key_pos], encrypt_text[i], &x, &y);
- // printf("%d %d %c ", x, y, encrypt_text[i]);
- encrypt_text[i] = vigenere_arr[x][y];
- } else {
- encrypt_text[i] = text[i];
- key_pos--;
- }
- // printf("%d %s %c\n", key_pos, right_key, right_key[key_pos]);
- // printf("%c", encrypt_text[i]);
- // automaticke riadenie key_pos
- if(key_pos >= strlen(right_key)-1){
- key_pos = -1;
- }
- }
- encrypt_text[strlen(text)] = '\0';
- return encrypt_text;
- }
- char* vigenere_decrypt(const char* key, const char* text){
- if(key == NULL || text == NULL){
- return NULL;
- }
- char vigenere_arr[strlen(ALPHA)][strlen(ALPHA)];
- // vygenerovanie matrixu
- for (int i = 0; i < strlen(ALPHA); i++){
- for (int j = 0; j < strlen(ALPHA); j++){
- vigenere_arr[i][j] = ALPHA[i+j >= strlen(ALPHA) ? i+j-strlen(ALPHA) : i+j];
- }
- }
- char right_key[strlen(key)];
- int wrong_letters = 0;
- for (int i = 0; i < strlen(key); i++){
- if((key[i] >= 'a' && key[i] <= 'z') || (key[i] >= 'A' && key[i] <= 'Z')){
- if(key[i] >= 'a' && key[i] <= 'z'){
- right_key[i-wrong_letters] = key[i]-32;
- } else {
- right_key[i-wrong_letters] = key[i];
- }
- } else {
- return NULL;
- wrong_letters++;
- }
- }
- right_key[strlen(key) - wrong_letters] = '\0';
- if(strlen(right_key) == 0){
- // free(right_key);
- return NULL;
- }
- printf("\nKLUC > %s\n", right_key);
- char *encrypt_text = calloc(strlen(text) + 1, sizeof *text);
- printf("[------]\n");
- for(int i = 0, key_pos = 0; i < strlen(text); key_pos++, i++){
- encrypt_text[i] = vigenere_char_dec(vigenere_arr, right_key[key_pos], text[i]);
- // printf("%c", vigenere_char_dec(vigenere_arr, right_key[key_pos], text[i]));
- if(text[i] == ' '){
- key_pos--;
- }
- // automaticke riadenie key_pos
- if(key_pos >= strlen(right_key)-1){
- key_pos = -1;
- }
- }
- return encrypt_text;
- }
- unsigned char* bit_encrypt(const char* text){
- unsigned char *dec = calloc(strlen(text)+1, sizeof *text);
- for(int i = 0; i < strlen(text); i++){
- int znak = text[i];
- //
- printf("%d\n", (int)text[i] );
- //
- int bin[] = {0,0,0,0,0,0,0,0};
- //128 64 32 16 8 4 2 1
- for(int j = 0; j < 8; j++){
- if(znak >= 128){
- znak -= 128;
- bin[0] = 1;
- } else if(znak >= 64){
- znak -= 64;
- bin[1] = 1;
- } else if(znak >= 32){
- znak -= 32;
- bin[2] = 1;
- } else if(znak >= 16){
- znak -= 16;
- bin[3] = 1;
- } else if(znak >= 8){
- znak -= 8;
- bin[4] = 1;
- } else if(znak >= 4){
- znak -= 4;
- bin[5] = 1;
- } else if(znak >= 2){
- znak -= 2;
- bin[6] = 1;
- } else if(znak >= 1){
- znak -= 1;
- bin[7] = 1;
- }
- }
- int fbin[2][4];
- fbin[0][0] = bin[1];
- fbin[0][1] = bin[0];
- fbin[0][2] = bin[3];
- fbin[0][3] = bin[2];
- fbin[1][0] = bin[4] ^ fbin[0][0];
- fbin[1][1] = bin[5] ^ fbin[0][1];
- fbin[1][2] = bin[6] ^ fbin[0][2];
- fbin[1][3] = bin[7] ^ fbin[0][3];
- int temp = 0;
- if(fbin[0][0] == 1){
- temp+=128;
- }
- if(fbin[0][1] == 1){
- temp+=64;
- }
- if(fbin[0][2] == 1){
- temp+=32;
- }
- if(fbin[0][3] == 1){
- temp+=16;
- }
- if(fbin[1][0] == 1){
- temp+=8;
- }
- if(fbin[1][1] == 1){
- temp+=4;
- }
- if(fbin[1][2] == 1){
- temp+=2;
- }
- if(fbin[1][3] == 1){
- temp+=1;
- }
- dec[i] = (char)temp;
- }
- return dec;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement