Advertisement
Guest User

Untitled

a guest
Feb 28th, 2015
614
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.03 KB | None | 0 0
  1. /*
  2. ** encrypt.c for encrypt in /home/boulogne_q/Files/Elcrypt/sources/fonctions
  3. **
  4. ** Made by quentin boulogne
  5. ** Login <boulogne_q@epitech.net>
  6. **
  7. ** Started on Sat Feb 28 02:43:11 2015 quentin boulogne
  8. ** Last update Sat Feb 28 15:07:46 2015 quentin boulogne
  9. */
  10.  
  11. #include <stdlib.h>
  12. #include <stdio.h>
  13. #include <string.h>
  14. #include "Elcrypt.h"
  15.  
  16. char *substract_LSB(char *prim_key_64)
  17. {
  18. char *prim_key_56;
  19. int i;
  20. int j;
  21. int k;
  22.  
  23. prim_key_56 = malloc(sizeof(char) * 57);
  24. k = 0;
  25. j = 0;
  26. i = 0;
  27. while (i < 64)
  28. {
  29. while (k != 7)
  30. {
  31. prim_key_56[j] = prim_key_64[i];
  32. i++;
  33. j++;
  34. k++;
  35. }
  36. i++;
  37. k = 0;
  38. }
  39. return (prim_key_56);
  40. }
  41.  
  42. char *convertBaseVersion(char input, int base, int digits)
  43. {
  44. int i;
  45. int remainder;
  46. char digitsArray[17] = "0123456789ABCDEF";
  47. char *output;
  48.  
  49. output = malloc(sizeof(char) * 4096);
  50. i = digits;
  51. while (i > 0)
  52. {
  53. remainder = input % base;
  54. input = input / base;
  55. output[i - 1] = digitsArray[remainder];
  56. i--;
  57. }
  58. output[digits] = '\0';
  59. return (output);
  60. }
  61. char *str_2_bin(char *str)
  62. {
  63. char *ascii_2_bin;
  64. int i;
  65.  
  66. i = 0;
  67. ascii_2_bin = malloc(sizeof(char) * 4096 * 999999);
  68. while (str[i])
  69. {
  70. strcat(ascii_2_bin, convertBaseVersion(str[i], 2, 8));
  71. i++;
  72. }
  73. return (ascii_2_bin);
  74. // printf("%s\n", ascii_2_bin);
  75. }
  76.  
  77. int check_bloc_len(char *str)
  78. {
  79. int i;
  80.  
  81. i = 0;
  82. while (str[i] != '\0')
  83. i++;
  84. if (i == 64) //-- if bloc size = 64, return 1 --//
  85. return (1);
  86. else if (i / 64 == 0) //-- if bloc size if multiple of 64, return 2 --//
  87. return (2);
  88. else if (i / 64 != 0) //-- if bloc size if not a multiple of 64, return 3 --//
  89. return (3);
  90. }
  91.  
  92. char *get_first_half_bin_word(char *str)
  93. {
  94. int i;
  95. char *first_half_binword;
  96.  
  97. first_half_binword = malloc(sizeof(char) * 65);
  98. i = 0;
  99. while (i <= 31)
  100. {
  101. first_half_binword[i] = str[i];
  102. i++;
  103. }
  104. first_half_binword[i] = '\0';
  105. return (first_half_binword);
  106. }
  107. char *get_32_last_bit(char *str)
  108. {
  109. int i;
  110. int k;
  111. int j;
  112. char *result;
  113.  
  114. i = 0;
  115. result = malloc(sizeof(char) * 4096 * 99999);
  116. k = strlen(str);
  117. j = k - 32;
  118. while (str[j])
  119. {
  120. result[i] = str[j];
  121. i++;
  122. j++;
  123. }
  124. return (result);
  125.  
  126.  
  127.  
  128. }
  129.  
  130. int reseau_festel(char *str, char *key)
  131. {
  132. int i;
  133. char *key_rotate;
  134. char *groupe_1;
  135. char *groupe_2;
  136. char *first_half_binword;
  137. char *second_half_binword;
  138.  
  139. key_rotate = malloc(sizeof(char) * 4096 * 99999);
  140. groupe_1 = malloc(sizeof(char) * 4096 * 99999);
  141. groupe_2 = malloc(sizeof(char) * 4096 * 99999);
  142. printf("~~~ Clé 56bits : %s \n", key);
  143. key = rotate_key(key);
  144. printf("~~~ Clé 56bits rotate avec N = 1 : %s \n", key);
  145. key = get_32_last_bit(key);
  146. printf("~~~ Clé 56bits rotate (32 last bits) : %s\n", key);
  147. first_half_binword = get_first_half_bin_word(str);
  148. second_half_binword = get_second_half_bin_word(str);
  149. printf("~~~ Debut : \n~~~G1 : %s\n~~~ G2 : %s\n", first_half_binword, second_half_binword);
  150. groupe_2 = xor(key, first_half_binword);
  151. printf("~~~ Resultat : \n~~~ Gauche : %s\n~~~~ Droite : %s\n", second_half_binword, groupe_2);
  152. }
  153.  
  154. int encrypt_64(char *str, char *key)
  155. {
  156. char *first_half_binword;
  157. char *second_half_binword;
  158.  
  159. first_half_binword = get_first_half_bin_word(str);
  160. second_half_binword = get_second_half_bin_word(str);
  161. reseau_festel(str, key);
  162. }
  163. int do_encrypt(char *str, char *key)
  164. {
  165. char *bin_word;
  166. int bloc_type;
  167.  
  168. bin_word = malloc(sizeof(char) * 4096 * 9999);
  169. bin_word = str_2_bin(str); //-- First step : Put Ascii To Binary -- //
  170. bloc_type = check_bloc_len(bin_word); //-- Second step : Check bin len --//
  171. key = substract_LSB(key); //-- Third ste : Key Without Least Significant Bit --//
  172. //printf("KEY = %s\n", key);
  173. bloc_type == 1 ? encrypt_64(bin_word, key) : 0;
  174. // bloc_type == 2 ? encrypt_m64(bin_word) : 0;
  175. //bloc_type == 3 ? encrypt_nm64(bin_word) : 0;
  176. }
  177.  
  178. int main()
  179. {
  180. char Text[] = "Speciaux";
  181. char key[] = "0101010101010101010101010101010101010101010101010101010101010101";
  182. do_encrypt(Text, key);
  183. }
  184.  
  185. -UUU:**--F1 encrypt.c Bot L206 (C/l Abbrev) ---------------------------------------------------------------------------------------
  186. End of buffer
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement