SlowerPhoton

Untitled

Jan 24th, 2016
37
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<LiquidCrystal.h>
  2. #include <avr/pgmspace.h>
  3.  
  4. LiquidCrystal lcd(8, 9, 4, 5, 6, 7);
  5.  
  6. void leftShift(bool toShift[28], int noOfShifts, bool destination[28]) {
  7.  
  8.   for (int i = 0; i < 28; i++) {
  9.     destination[(i - noOfShifts + 28) % 28] = toShift[i];
  10.   }
  11. }
  12.  
  13.  
  14.  
  15. void divideBinary(bool binary[], size_t sizeOfBinary, bool LB[], bool RB[]) {
  16.   size_t half = sizeOfBinary / 2;
  17.  
  18.   // LB - first half
  19.   size_t i = 0;
  20.   for (; i < half; i++) {
  21.     LB[i] = binary[i];
  22.   }
  23.  
  24.   // RB - second half
  25.   for (; i < half * 2; i++) {
  26.     RB[i - half] = binary[i];
  27.   }
  28. }
  29.  
  30. void createSubkeys(bool binaryKey[8 * 8], bool subkeys[16][48]) {
  31.   Serial.println(F("just entered subkeys")); Serial.flush();
  32.   const uint8_t pc_1[56] PROGMEM = {
  33.     57, 49, 41, 33, 25, 17,  9,
  34.     1, 58, 50, 42, 34, 26, 18,
  35.     10,  2, 59, 51, 43, 35, 27,
  36.     19, 11,  3, 60, 52, 44, 36,
  37.     63, 55, 47, 39, 31, 23, 15,
  38.     7, 62, 54, 46, 38, 30, 22,
  39.     14,  6, 61, 53, 45, 37, 29,
  40.     21, 13,  5, 28, 20, 12,  4
  41.   };
  42.   bool* keyPermutation;
  43.   keyPermutation = new bool[56];
  44.  
  45.  
  46.   // according to pc_1 create from 64-bit key 56-bit keyPermutation
  47.   for (int i = 0; i < 56; i++) {
  48.     keyPermutation[i] = binaryKey[pc_1[i] - 1];
  49.   }
  50.  
  51.   // C and D will be saved here: [C/D] [index] [28 bools]
  52.   bool CD[2][16 + 1][56 / 2];
  53.   Serial.println(F("CD ready")); Serial.flush();
  54.   // divide keyPermutation into halves to C0 a D0 - each consists of 28 bits
  55.  /* divideBinary(keyPermutation, 56, CD[0][0], CD[1][0]);
  56.   delete[] keyPermutation;
  57.  
  58.   // from C0, D0 and shifts make C1, D1 -> C16, D16
  59.   const uint8_t shifts[16] PROGMEM = { 1, 1, 2, 2, 2, 2, 2, 2, 1, 2, 2, 2, 2, 2, 2, 1 };
  60.   for (int i = 1; i < 17; i++) {
  61.     leftShift(CD[0][i - 1], shifts[i - 1], CD[0][i]);
  62.     leftShift(CD[1][i - 1], shifts[i - 1], CD[1][i]);
  63.   }
  64.  
  65.   // each subKey out of 16 is made from one out of 16 CD with the use of pc_2
  66.   const uint8_t pc_2[48] PROGMEM = {
  67.     14,    17,   11,    24,     1,    5,
  68.     3,    28,   15,     6,    21,   10,
  69.     23,    19,   12,     4,    26,    8,
  70.     16,     7,   27,    20,    13,    2,
  71.     41,    52,   31,    37,    47,   55,
  72.     30,    40,   51,    45,    33,   48,
  73.     44,    49,   39,    56,    34,   53,
  74.     46,    42,   50,    36,    29,   32
  75.   };
  76.  
  77.   for (int i = 0; i < 16; i++) {
  78.     for (int j = 0; j < 48; j++) {
  79.  
  80.       // find out which part of CD we should look at - that means C, or D? for C CorD is 0, for D 1
  81.       int where = pc_2[j] - 1;
  82.       bool CorD = 0;
  83.       if (where >= 56 / 2) {
  84.         CorD = 1;
  85.         where -= 56 / 2; // subtract 28, to start indexing from 0 again in case of D
  86.       }
  87.  
  88.       subkeys[i][j] = CD[CorD][i + 1][where];
  89.     }
  90.   }*/
  91.  
  92. //  Serial.println("subkeys ready");
  93. }
  94. void setup() {
  95.   // put your setup code here, to run once:
  96.  
  97.    Serial.begin( 9600 );
  98.  
  99.   lcd.begin(16, 2);
  100.   Serial.println(F("ready")); Serial.flush();
  101.   bool binaryKey[8 * 8];
  102.   bool subkeys[16][48];
  103.   createSubkeys(binaryKey, subkeys);
  104.  
  105. }
  106.  
  107. void loop() {
  108.   // put your main code here, to run repeatedly:
  109.   lcd.setCursor(0,0);
  110.   lcd.print(F("loop"));
  111. }
Add Comment
Please, Sign In to add comment