SHARE
TWEET

Untitled

a guest Feb 16th, 2019 109 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import std.stdio;
  2. import std.math;
  3. import std.random;
  4. import std.string;
  5.  
  6. // S-Box
  7. immutable ubyte[16][16] sbox = cast(ubyte[][]) [
  8.     [0x63,0x7C,0x77,0x7B,0xF2,0x6B,0x6F,0xC5,0x30,0x01,0x67,0x2B,0xFE,0xD7,0xAB,0x76],
  9.     [0xCA,0x82,0xC9,0x7D,0xFA,0x59,0x47,0xF0,0xAD,0xD4,0xA2,0xAF,0x9C,0xA4,0x72,0xC0],
  10.     [0xB7,0xFD,0x93,0x26,0x36,0x3F,0xF7,0xCC,0x34,0xA5,0xE5,0xF1,0x71,0xD8,0x31,0x15],
  11.     [0x04,0xC7,0x23,0xC3,0x18,0x96,0x05,0x9A,0x07,0x12,0x80,0xE2,0xEB,0x27,0xB2,0x75],
  12.     [0x09,0x83,0x2C,0x1A,0x1B,0x6E,0x5A,0xA0,0x52,0x3B,0xD6,0xB3,0x29,0xE3,0x2F,0x84],
  13.     [0x53,0xD1,0x00,0xED,0x20,0xFC,0xB1,0x5B,0x6A,0xCB,0xBE,0x39,0x4A,0x4C,0x58,0xCF],
  14.     [0xD0,0xEF,0xAA,0xFB,0x43,0x4D,0x33,0x85,0x45,0xF9,0x02,0x7F,0x50,0x3C,0x9F,0xA8],
  15.     [0x51,0xA3,0x40,0x8F,0x92,0x9D,0x38,0xF5,0xBC,0xB6,0xDA,0x21,0x10,0xFF,0xF3,0xD2],
  16.     [0xCD,0x0C,0x13,0xEC,0x5F,0x97,0x44,0x17,0xC4,0xA7,0x7E,0x3D,0x64,0x5D,0x19,0x73],
  17.     [0x60,0x81,0x4F,0xDC,0x22,0x2A,0x90,0x88,0x46,0xEE,0xB8,0x14,0xDE,0x5E,0x0B,0xDB],
  18.     [0xE0,0x32,0x3A,0x0A,0x49,0x06,0x24,0x5C,0xC2,0xD3,0xAC,0x62,0x91,0x95,0xE4,0x79],
  19.     [0xE7,0xC8,0x37,0x6D,0x8D,0xD5,0x4E,0xA9,0x6C,0x56,0xF4,0xEA,0x65,0x7A,0xAE,0x08],
  20.     [0xBA,0x78,0x25,0x2E,0x1C,0xA6,0xB4,0xC6,0xE8,0xDD,0x74,0x1F,0x4B,0xBD,0x8B,0x8A],
  21.     [0x70,0x3E,0xB5,0x66,0x48,0x03,0xF6,0x0E,0x61,0x35,0x57,0xB9,0x86,0xC1,0x1D,0x9E],
  22.     [0xE1,0xF8,0x98,0x11,0x69,0xD9,0x8E,0x94,0x9B,0x1E,0x87,0xE9,0xCE,0x55,0x28,0xDF],
  23.     [0x8C,0xA1,0x89,0x0D,0xBF,0xE6,0x42,0x68,0x41,0x99,0x2D,0x0F,0xB0,0x54,0xBB,0x16]
  24. ];
  25. // Inverse der S-Box
  26. immutable ubyte[16][16] rbox = cast(ubyte[][]) [
  27.     [0x52,0x09,0x6A,0xD5,0x30,0x36,0xA5,0x38,0xBF,0x40,0xA3,0x9E,0x81,0xF3,0xD7,0xFB],
  28.     [0x7C,0xE3,0x39,0x82,0x9B,0x2F,0xFF,0x87,0x34,0x8E,0x43,0x44,0xC4,0xDE,0xE9,0xCB],
  29.     [0x54,0x7B,0x94,0x32,0xA6,0xC2,0x23,0x3D,0xEE,0x4C,0x95,0x0B,0x42,0xFA,0xC3,0x4E],
  30.     [0x08,0x2E,0xA1,0x66,0x28,0xD9,0x24,0xB2,0x76,0x5B,0xA2,0x49,0x6D,0x8B,0xD1,0x25],
  31.     [0x72,0xF8,0xF6,0x64,0x86,0x68,0x98,0x16,0xD4,0xA4,0x5C,0xCC,0x5D,0x65,0xB6,0x92],
  32.     [0x6C,0x70,0x48,0x50,0xFD,0xED,0xB9,0xDA,0x5E,0x15,0x46,0x57,0xA7,0x8D,0x9D,0x84],
  33.     [0x90,0xD8,0xAB,0x00,0x8C,0xBC,0xD3,0x0A,0xF7,0xE4,0x58,0x05,0xB8,0xB3,0x45,0x06],
  34.     [0xD0,0x2C,0x1E,0x8F,0xCA,0x3F,0x0F,0x02,0xC1,0xAF,0xBD,0x03,0x01,0x13,0x8A,0x6B],
  35.     [0x3A,0x91,0x11,0x41,0x4F,0x67,0xDC,0xEA,0x97,0xF2,0xCF,0xCE,0xF0,0xB4,0xE6,0x73],
  36.     [0x96,0xAC,0x74,0x22,0xE7,0xAD,0x35,0x85,0xE2,0xF9,0x37,0xE8,0x1C,0x75,0xDF,0x6E],
  37.     [0x47,0xF1,0x1A,0x71,0x1D,0x29,0xC5,0x89,0x6F,0xB7,0x62,0x0E,0xAA,0x18,0xBE,0x1B],
  38.     [0xFC,0x56,0x3E,0x4B,0xC6,0xD2,0x79,0x20,0x9A,0xDB,0xC0,0xFE,0x78,0xCD,0x5A,0xF4],
  39.     [0x1F,0xDD,0xA8,0x33,0x88,0x07,0xC7,0x31,0xB1,0x12,0x10,0x59,0x27,0x80,0xEC,0x5F],
  40.     [0x60,0x51,0x7F,0xA9,0x19,0xB5,0x4A,0x0D,0x2D,0xE5,0x7A,0x9F,0x93,0xC9,0x9C,0xEF],
  41.     [0xA0,0xE0,0x3B,0x4D,0xAE,0x2A,0xF5,0xB0,0xC8,0xEB,0xBB,0x3C,0x83,0x53,0x99,0x61],
  42.     [0x17,0x2B,0x04,0x7E,0xBA,0x77,0xD6,0x26,0xE1,0x69,0x14,0x63,0x55,0x21,0x0C,0x7D]
  43. ];
  44. // Rundenkonstanten für Key-Schedule
  45. immutable ubyte[4][10] rcon = [
  46.     [0x01, 0, 0, 0], [0x02, 0, 0, 0], [0x04, 0, 0, 0], [0x08, 0, 0, 0], [0x10, 0, 0, 0],
  47.     [0x20, 0, 0, 0], [0x40, 0, 0, 0], [0x80, 0, 0, 0], [0x1B, 0, 0, 0], [0x36, 0, 0, 0]
  48. ];
  49.  
  50. immutable ubyte[4][4] gField = [    // Rijndael's Galois Field
  51.     [ 2,  3,  1,  1],
  52.     [ 1,  2,  3,  1],
  53.     [ 1,  1,  2,  3],
  54.     [ 3,  1,  1,  2]
  55. ];
  56.  
  57. immutable ubyte[4][4] iField = [    // Inverse Rijndael's Galois Field
  58.     [14, 11, 13,  9],
  59.     [ 9, 14, 11, 13],
  60.     [13,  9, 14, 11],
  61.     [11, 13,  9, 14]
  62. ];
  63.  
  64. uint symKey;
  65. char[16] msg = "Goeifm-Nagjdhcht";
  66.  
  67. void main()
  68. {
  69.     //assert(msg == msg.encrypt(symKey).decrypt(symKey));
  70.     char[16] geheimtext;
  71.     char[4][4] block = msg.toBlock();
  72.                                 block.printBlock();
  73.     block.subBytes();           block.printBlock();
  74.     block.shiftRows();          block.printBlock();
  75.     block.mixColumns();         block.printBlock();
  76.     block.invMixColumns();      block.printBlock();
  77.     block.invShiftRows();       block.printBlock();
  78.     block.invSubBytes();        block.printBlock();
  79.  
  80.     /*writeln("Führe Diffie-Hellman durch...\n");
  81.     diffie_hellman();
  82.     writefln("\nVerschlüssele \"%s\" mit AES...\n", msg);
  83.     geheimtext = encrypt(msg, symKey);
  84.     writefln("\nVerschlüsselter Text: %s", geheimtext);
  85.     writefln("\nEntschlüsselter Text: %s", decrypt(geheimtext, symKey));
  86.     */
  87.  
  88. }
  89.  
  90. void diffie_hellman()
  91. {
  92.    
  93.     uint n = 4_294_967_291; // größte Primzahl unter dem 32 bit uint Maximalwert
  94.     uint g = 3;
  95.  
  96.     uint private_a;
  97.     uint private_b;
  98.     uint public_a;
  99.     uint public_b;
  100.  
  101.     writefln("n:%s\ng:%s", n, g);
  102.  
  103.     private_a = uniform!"[]"(1, n-1);
  104.     private_b = uniform!"[]"(1, n-1);
  105.     writefln("Private Key A: %X\nPrivate Key B: %X", private_a, private_b);
  106.  
  107.     public_a = powmod(g, private_a, n);
  108.     public_b = powmod(g, private_b, n);
  109.     writefln("Public Key A: %X\nPublic Key B: %X", public_a, public_b);
  110.  
  111.     symKey = powmod(public_a, private_b, n);
  112.     writefln("Key: %X   pub_a^priv_b mod n = %s  |  pub_b^priv_a mod n = %s",
  113.              symKey, powmod(public_a, private_b, n), powmod(public_b, private_a, n));
  114.    
  115. }
  116.  
  117. char[4][4] toBlock(char[16] a)
  118. {
  119.     char[4][4] block;
  120.     foreach(i, c; a)
  121.         block[i / 4][i % 4] = c;
  122.     return block;
  123. }
  124.  
  125. char[16] toArray(char[4][4] b)
  126. {
  127.     char[16] array;
  128.     foreach(c; 0..4)
  129.     {
  130.         foreach(r; 0..4)
  131.             array[c * 4 + r] = b[c][r];
  132.     }
  133.     return array;
  134. }
  135.  
  136.  
  137. char[16] encrypt(char[16] text, uint key)
  138. {
  139.     char[4][4][11] keys;
  140.     char[4][4] block;
  141.     block = toBlock(text);  // Block wird mit dem String gefüllt
  142.  
  143.     writeln("Klarext:");
  144.     block.printBlock(true);
  145.  
  146.     writeln("\nGeneriere Rundenschlüssel...");
  147.     keySchedule(key, keys);
  148.     /*foreach(i, e; keys)
  149.     {
  150.         if(i == 0)
  151.             writeln("\nStartschlüssel (für Runde 0):");
  152.         else
  153.             writefln("\nRundenschlüssel für %s. Runde:", i);
  154.         e.printBlock();
  155.     }*/
  156.     writeln();
  157.  
  158.     // Verschlüsselung
  159.     writeln("Starte Verschlüsselung...");
  160.     writeln("\nText:");
  161.     block.printBlock(true);
  162.     // Vorrunde
  163.     block.addRoundKey(keys[0]);
  164.     writeln("\nBlock nach Runde 0 (Vorrunde):");
  165.     block.printBlock();
  166.     // Runden
  167.     for(auto i = 1; i < 11; i++)
  168.     {
  169.         block.subBytes();
  170.         block.shiftRows();
  171.         //if(i != 10) block.mixColumns();
  172.         block.addRoundKey(keys[i]);
  173.         writefln("\nBlock nach Runde %s:", i);
  174.         block.printBlock();
  175.     }
  176.  
  177.     return block.toArray();
  178. }
  179.  
  180. char[16] decrypt(char[16] text, uint key)
  181. {
  182.     char[4][4][11] keys;
  183.     char[4][4] block;
  184.     block = text.toBlock();
  185.  
  186.     writeln("Geheimtext:");
  187.     block.printBlock(true);
  188.  
  189.     writeln("\nGeneriere Rundenschlüssel...");
  190.     keySchedule(key, keys);
  191.     /*foreach(i, e; keys)
  192.     {
  193.         if(i == 0)
  194.             writeln("\nStartschlüssel (für Runde 0):");
  195.         else
  196.             writefln("\nRundenschlüssel für %s. Runde:", i);
  197.         e.printBlock();
  198.     }*/
  199.     writeln();
  200.  
  201.     // Entschlüsselung
  202.     writeln("Starte Entschlüsselung...");
  203.     writeln("\nText:");
  204.     block.printBlock(true);
  205.     // Runden
  206.     for(auto i = 10; i > 0; i--)
  207.     {
  208.         block.addRoundKey(keys[i]);
  209.         //if(i != 10) block.invMixColumns();
  210.         block.invShiftRows();
  211.         block.invSubBytes();
  212.         writefln("\nBlock nach Runde %s:", i);
  213.         block.printBlock();
  214.     }
  215.     //Vorrunde
  216.     block.addRoundKey(keys[0]);
  217.     writeln("\nBlock nach Runde 0 (Vorrunde):");
  218.     block.printBlock(true);
  219.  
  220.     return block.toArray();
  221. }
  222.  
  223. void keySchedule(uint key, ref char[4][4][11] keys)
  224. {
  225.     //char[4][4][11] keys;  // 4x4 Schlüsselblöcke für 11 Runden (inklusive Vorrunde)
  226.     foreach(c; 0..4)    // c: Spalte
  227.     {
  228.         foreach(r; 0..4)    // r: Zeile
  229.         {
  230.             /* Der 32 bit int wird in Bytes unterteilt und
  231.                in alle 4 Spalten des ersten Schlüsselblocks
  232.                geschrieben. Damit wird der 32 bit Schlüssel
  233.                qusai zur nötigen 128 bit größe erweitert.  */
  234.             keys[0][c][r] = key >>> (32 - 8 * r) & 0xFF;
  235.         }
  236.     }
  237.  
  238.     foreach(i; 1..11)   // i: Runde
  239.     {
  240.         /* Erste Spalte eines Rundenschlüssels:
  241.            RotWord,SubBytes mit linker Spalte XOR Spalte des letzten Blocks XOR Rundenkonstante */
  242.         char[4] prev = keys[i-1][3].dup; // kopie der links angrenzenden Spalte
  243.         prev.rotWord();
  244.         prev.keySubBytes();
  245.         foreach(r, ref e; keys[i][0])
  246.             e = keys[i - 1][0][r] ^ prev[r] ^ rcon[i - 1][r];
  247.         /* Die anderen 3 Spalten:
  248.            Spalte des letzten Blocks XOR linke Spalte */
  249.         foreach(c; 1..4)
  250.         {
  251.             foreach(r; 0..4)
  252.                 keys[i][c][r] = keys[i - 1][c][r] ^ keys[i][c - 1][r];
  253.         }
  254.     }
  255.    
  256. }
  257.  
  258. void printBlock(char[4][4] b, bool asChar = false)
  259. {
  260.     writeln();
  261.     foreach(r; 0..4)
  262.         writefln(asChar ? "%s %s %s %s" : "%02X %02X %02X %02X", b[0][r], b[1][r], b[2][r], b[3][r]);
  263. }
  264.  
  265. void rotWord(char[] b, int places = 1)
  266. {
  267.     auto copy = b.dup;
  268.     for(auto i = 0; i < b.length; i++)
  269.         b[i] = copy[(i + places) % $];
  270. }
  271.  
  272. void keySubBytes(char[] b)
  273. {
  274.     foreach(ref i; b)
  275.         i = sbox[(i & 0xF0) >> 4][i & 0x0F];
  276. }
  277.  
  278. void subBytes(ref char[4][4] b)
  279. {
  280.     foreach(ref c; b)
  281.     {
  282.         foreach(ref r; c)
  283.             r = sbox[(r & 0xF0) >> 4][r & 0x0F];
  284.     }
  285. }
  286.  
  287. void invSubBytes(ref char[4][4] b)
  288. {
  289.     foreach(ref c; b)
  290.     {
  291.         foreach(ref r; c)
  292.             r = rbox[(r & 0xF0) >> 4][r & 0x0F];
  293.     }
  294. }
  295.  
  296. void shiftRows(ref char[4][4] b)
  297. {
  298.     foreach(i; 0..4)
  299.     {
  300.         auto r = [ b[0][i], b[1][i], b[2][i], b[3][i] ];
  301.         r.rotWord(i);
  302.         b[0][i] = r[0];
  303.         b[1][i] = r[1];
  304.         b[2][i] = r[2];
  305.         b[3][i] = r[3];
  306.     }
  307. }
  308.  
  309. void invShiftRows(ref char[4][4] b)
  310. {
  311.     foreach(i; 0..4)
  312.     {
  313.         auto r = [ b[0][i], b[1][i], b[2][i], b[3][i] ];
  314.         r.rotWord(-i);
  315.         b[0][i] = r[0];
  316.         b[1][i] = r[1];
  317.         b[2][i] = r[2];
  318.         b[3][i] = r[3];
  319.     }
  320. }
  321.  
  322. ubyte galoisMult(ubyte a, ubyte b)
  323. {
  324.     int res;
  325.     foreach(i; 0..8)
  326.     {
  327.         if((a & 1 << i) != 0)
  328.             res ^= b << i;
  329.     }
  330.     if((res & 0b1_0000_0000) != 0)
  331.         res ^= 0b1_0001_1011;   //x⁸+x⁴+x³+x+1
  332.     return cast(ubyte) res;
  333. }
  334.  
  335. void mixColumns(ref char[4][4] b)
  336. {
  337.     foreach(c; 0..4)
  338.     {
  339.         auto tempC = b[c].dup;
  340.         foreach(r; 0..4)
  341.         {
  342.             byte t;
  343.             foreach(i; 0..4)
  344.                 t ^= galoisMult(gField[r][i], b[c][i]);
  345.             tempC[r] = t;
  346.         }
  347.         b[c] = tempC.dup;
  348.     }
  349. }
  350.  
  351. void invMixColumns(ref char[4][4] b)
  352. {
  353.     foreach(c; 0..4)
  354.     {
  355.         auto tempC = b[c].dup;
  356.         foreach(r; 0..4)
  357.         {
  358.             byte t;
  359.             foreach(i; 0..4)
  360.                 t ^= galoisMult(iField[r][i], b[c][i]);
  361.             tempC[r] = t;
  362.         }
  363.         b[c] = tempC.dup;
  364.     }
  365. }
  366.  
  367. void addRoundKey(ref char[4][4] b, char[4][4] key)
  368. {
  369.     foreach(c; 0..4)
  370.     {
  371.         foreach(r; 0..4)
  372.             b[c][r] ^= key[c][r];
  373.     }
  374. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top