• // XOR left and op x = xor_(op, left); left = x; // Swapper if (i != 15) { swap(left, right);
  • 34, 2, 42, 10, 50, 18, 58, 26, 33, 1, 41, 9, 49, 17, 57, 25 }; // Final Permutation string cipher = bin2hex(permute(combine, final_perm, 64));
  • // getting 56 bit key from 64 bit using the parity bits key = permute(key, keyp, 56); // key without parity // Number of bit shifts
  • // Combining string combine = left + right; // Key Compression string RoundKey = permute(combine, key_comp, 48);
  • 6 Simmetrik va assimetrik shifrlashlar




    Download 0,53 Mb.
    bet10/12
    Sana18.05.2024
    Hajmi0,53 Mb.
    #242061
    1   ...   4   5   6   7   8   9   10   11   12
    Bog'liq
    6 Simmetrik va assimetrik shifrlashlar

    int val = s[i][row][col];
    op += char(val / 8 + '0');
    val = val % 8;
    op += char(val / 4 + '0');
    val = val % 4;
    op += char(val / 2 + '0');
    val = val % 2;
    op += char(val + '0');
    }
    // Straight D-box
    op = permute(op, per, 32);


    // XOR left and op
    x = xor_(op, left);


    left = x;


    // Swapper
    if (i != 15) {
    swap(left, right);
    }
    cout << "Round " << i + 1 << " " << bin2hex(left) << " "
    << bin2hex(right) << " " << rk[i] << endl;
    }


    // Combination
    string combine = left + right;


    // Final Permutation Table
    int final_perm[64] = { 40, 8, 48, 16, 56, 24, 64, 32,
    39, 7, 47, 15, 55, 23, 63, 31,
    38, 6, 46, 14, 54, 22, 62, 30,
    37, 5, 45, 13, 53, 21, 61, 29,
    36, 4, 44, 12, 52, 20, 60, 28,
    35, 3, 43, 11, 51, 19, 59, 27,
    34, 2, 42, 10, 50, 18, 58, 26,
    33, 1, 41, 9, 49, 17, 57, 25 };


    // Final Permutation
    string cipher = bin2hex(permute(combine, final_perm, 64));
    return cipher;
    }
    int main()
    {
    // pt is plain text
    string pt, key;
    cout<<"Enter plain text(in hexadecimal): ";
    cin>>pt;
    cout<<"Enter key(in hexadecimal): ";
    cin>>key;


    // pt = "123456ABCD132536";
    // key = "AABB09182736CCDD";
    // Key Generation


    // Hex to binary
    key = hex2bin(key);


    // Parity bit drop table
    int keyp[56] = { 57, 49, 41, 33, 25, 17, 9,
    1, 58, 50, 42, 34, 26, 18,
    10, 2, 59, 51, 43, 35, 27,
    19, 11, 3, 60, 52, 44, 36,
    63, 55, 47, 39, 31, 23, 15,
    7, 62, 54, 46, 38, 30, 22,
    14, 6, 61, 53, 45, 37, 29,
    21, 13, 5, 28, 20, 12, 4 };


    // getting 56 bit key from 64 bit using the parity bits
    key = permute(key, keyp, 56); // key without parity


    // Number of bit shifts
    int shift_table[16] = { 1, 1, 2, 2,
    2, 2, 2, 2,
    1, 2, 2, 2,
    2, 2, 2, 1 };


    // Key- Compression Table
    int key_comp[48] = { 14, 17, 11, 24, 1, 5,
    3, 28, 15, 6, 21, 10,
    23, 19, 12, 4, 26, 8,
    16, 7, 27, 20, 13, 2,
    41, 52, 31, 37, 47, 55,
    30, 40, 51, 45, 33, 48,
    44, 49, 39, 56, 34, 53,
    46, 42, 50, 36, 29, 32 };


    // Splitting
    string left = key.substr(0, 28);
    string right = key.substr(28, 28);


    vector rkb; // rkb for RoundKeys in binary
    vector rk; // rk for RoundKeys in hexadecimal
    for (int i = 0; i < 16; i++) {
    // Shifting
    left = shift_left(left, shift_table[i]);
    right = shift_left(right, shift_table[i]);


    // Combining
    string combine = left + right;


    // Key Compression
    string RoundKey = permute(combine, key_comp, 48);


    rkb.push_back(RoundKey);
    rk.push_back(bin2hex(RoundKey));
    }


    cout << "\nEncryption:\n\n";
    string cipher = encrypt(pt, rkb, rk);
    cout << "\nCipher Text: " << cipher << endl;


    cout << "\nDecryption\n\n";
    reverse(rkb.begin(), rkb.end());

    Download 0,53 Mb.
    1   ...   4   5   6   7   8   9   10   11   12




    Download 0,53 Mb.

    Bosh sahifa
    Aloqalar

        Bosh sahifa



    6 Simmetrik va assimetrik shifrlashlar

    Download 0,53 Mb.