usickey.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. #include <crypt.h>
  2. #include <unistd.h>
  3. #include <string.h>
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <errno.h>
  7. void print_arr(char *buf,int n)
  8. {
  9. int i=0;
  10. for (;i<n;i++) printf("%c",buf[i]);
  11. }
  12. /* This expands n symbols of inbuf to binary numerical representation in outbuf */
  13. void expand(char *inbuf,char *outbuf,int n)
  14. {
  15. int i=0;
  16. for (;i<n;i++)
  17. {
  18. char c=inbuf[i];
  19. int j=0;
  20. for (;j<8;j++)
  21. {
  22. outbuf[i*8+j]=c&(1<<(8-j-1))?1:0;
  23. }
  24. }
  25. }
  26. /* Not really base64 but very alike */
  27. void printable(char *inbuf,char *outbuf,int n)
  28. {
  29. int i=0;
  30. char *ptr=inbuf;
  31. for (;i<n/4;i++)
  32. {
  33. char num=ptr[3]*8+ptr[2]*4+ptr[1]*2+ptr[0];
  34. ptr+=4;
  35. outbuf[i]=num+'a';
  36. }
  37. }
  38. /* Returns nearest 8-divisible number, greater than given */
  39. int nearest8(int n)
  40. {
  41. return (n/8+1)*8; /* NB: a/b is INTEGER divistion */
  42. }
  43. int main(int argc,char **argv)
  44. {
  45. /* Setting encryption key */
  46. errno=0;
  47. char key[]="davyjones";
  48. char key_bin[8*8+1];
  49. expand(key,key_bin,8);
  50. key_bin[8*8]=0;
  51. setkey(key_bin);
  52. char key_ascii[16];
  53. char *cryptogramm=NULL;
  54. if (argc>=2)
  55. {
  56. /* Allocating the buffer with size divisible by 8 nearest and greater to given */
  57. int n=nearest8(strlen(argv[1]));
  58. char *name=calloc(n+1,sizeof(char));
  59. /* Copying the name */
  60. strcpy(name,argv[1]);
  61. /* Creating bit representation of message */
  62. char *bigbuf=calloc(n*8+1,sizeof(char));
  63. expand(name,bigbuf,n);
  64. int i=0;
  65. /* Loading portions of 8 bytes to encryptor */
  66. char *ptr=bigbuf;
  67. for (;i<n/8;i++)
  68. {
  69. encrypt(ptr,0);
  70. ptr+=64;
  71. }
  72. cryptogramm=calloc(n*2+1,sizeof(char));
  73. printable(bigbuf,cryptogramm,n*8);
  74. if (argc==2) print_arr(cryptogramm,n*2);
  75. }
  76. if (argc==3)
  77. {
  78. if (!strcmp(argv[2],cryptogramm)) return 0;
  79. else return 1;
  80. }
  81. return 0;
  82. }