fork download
  1. #include <stdint.h>
  2. //#include "tools.h"
  3.  
  4. typedef uint8_t u8;
  5. typedef uint16_t u16;
  6. typedef uint32_t u32;
  7. #define END_EXPE() do{}while(0);
  8. #define START_ENCRYPT() do{}while(0);
  9. #define START_DECRYPT() do{}while(0);
  10.  
  11. #define ROTATE_LEFT_8(x,n) ( ((x) << (n)) | ((x) >> (8-(n))) )
  12. #define ROTATE_LEFT_16(x,n) ( ((x) << (n)) | ((x) >> (16-(n))) )
  13. #define ROTATE_LEFT_32(x,n) ( ((x) << (n)) | ((x) >> (32-(n))) )
  14. #define ROTATE_LEFT_64(x,n) ( ((x) << (n)) | ((x) >> (64-(n))) )
  15.  
  16. #define ROTATE_RIGHT_8(x,n) ( ((x) >> (n)) | ((x) << (8-(n))) )
  17. #define ROTATE_RIGHT_16(x,n) ( ((x) >> (n)) | ((x) << (16-(n))) )
  18. #define ROTATE_RIGHT_32(x,n) ( ((x) >> (n)) | ((x) << (32-(n))) )
  19. #define ROTATE_RIGHT_64(x,n) ( ((x) >> (n)) | ((x) << (64-(n))) )
  20.  
  21.  
  22. void KeyExpansion ( u32 l[], u32 k[] )
  23. {
  24. u8 i;
  25. for ( i=0 ; i<25 ; i++ )
  26. {
  27. l[i+2] = ( k[i] + ROTATE_RIGHT_32(l[i], 8) ) ^ i;
  28. k[i+1] = ROTATE_LEFT_32(k[i], 3) ^ l[i+2];
  29. }
  30. }
  31.  
  32. void Encrypt ( u32 text[], u32 crypt[], u32 key[] )
  33. {
  34. u8 i;
  35. crypt[0] = text[0];
  36. crypt[1] = text[1];
  37.  
  38. for ( i=0 ; i<26 ; i++ )
  39. {
  40. crypt[0] = ( ROTATE_RIGHT_32(crypt[0], 8) + crypt[1] ) ^ key[i];
  41. crypt[1] = ROTATE_LEFT_32(crypt[1], 3) ^ crypt[0];
  42. }
  43. }
  44.  
  45. void Decrypt ( u16 text[], u16 crypt[], u16 key[] )
  46. {
  47. u8 i;
  48. crypt[0] = text[0];
  49. crypt[1] = text[1];
  50.  
  51. for ( i=0 ; i<26 ; i++ )
  52. {
  53. crypt[1] = ROTATE_RIGHT_32( crypt[0] ^ crypt[1], 3);
  54. crypt[0] = ROTATE_LEFT_32( (crypt[0] ^ key[25-i]) - crypt[1], 8 );
  55. }
  56. }
  57.  
  58. int main ()
  59. {
  60. u8 test = 11110000;
  61. u8 test2;
  62. u32 text[2];
  63. text[0] = 0x74614620;
  64. text[1] = 0x736e6165;
  65. u32 crypt[2] = {0};
  66. u32 l[26] = {0};
  67. u32 k[25] = {0};
  68. l[1] = 0x13121110;
  69. l[0] = 0x0b0a0908;
  70. k[0] = 0x03020100;
  71.  
  72.  
  73. Encrypt ( text, crypt, k );
  74. printf("%x %x\n%x %x\n\n\n", text[0], text[1], crypt[0], crypt[1]);
  75. Decrypt ( text, crypt, k );
  76. printf("%x %x\n%x %x\n\n\n", text[0], text[1], crypt[0], crypt[1]);
  77.  
  78. // printf("test %x = ", test);
  79. // test2= ROTATE_RIGHT_8(test,2);
  80. // printf("%x \n\n\n", test2);
  81.  
  82. return 0;
  83. }
Success #stdin #stdout 0.01s 5544KB
stdin
Standard input is empty
stdout
74614620 736e6165
5c12072a 967fd755


74614620 736e6165
10ad5300 967fd755