void DES::setkey(const std::string & KEY){ if (keyset){ throw std::runtime_error("Error: Key has already been set."); } if (KEY.size() != 8){ throw std::runtime_error("Error: Key must be 64 bits long."); } std::string key = ""; for(int x = 0; x < 8; x++){ key += makebin(static_cast <uint8_t> (KEY[x]), 8); } std::string left = "", right = ""; for (uint8_t x = 0; x < 28; x++){ left += key[DES_PC1_l[x] - 1]; right += key[DES_PC1_r[x] - 1]; } for(uint8_t x = 0; x < 16; x++){ left = (left + left).substr(DES_rot[x], 28); right = (right + right).substr(DES_rot[x], 28); std::string k = ""; for(uint8_t y = 0; y < 48; y++) k += (left + right)[DES_PC2[y] - 1]; keys[x] = toint(k, 2); } keyset = true; }
int main() { int j; char sourceIP[20], destIP[20]; scanf("%s%s", sourceIP, destIP); makebin(sourceIP, 0); makebin(destIP, 1); printf(" Source IP binary= "); for(j=0; j<32; j++) printf("%c",sourceIPbin[j]); printf("\n"); printf(" Destin IP binary= "); for(j=0; j<32; j++) printf("%c",destIPbin[j]); printf("\n"); }
void IDEA::setkey(const std::string & KEY){ if (keyset){ throw std::runtime_error("Error: Key has already been set."); } if (KEY.size() != 16){ throw std::runtime_error("Error: Key must be 128 bits in length."); } std::string key = hexlify(KEY); std::vector <std::string> temp; for(uint8_t x = 0; x < 7; x++){ for(int y = 0; y < 8; y++){ temp.push_back(key.substr(y << 2, 4)); } for(uint8_t y = 0; y < 16; y++){ key += makebin(toint(key.substr(y << 1 , 2), 16), 8); } key = key.substr(32, 128); key = key.substr(25, 103) + key.substr(0, 25); for(uint8_t y = 0; y < 16; y++){ key += makehex(toint(key.substr(y << 3, 8), 2), 2); } key = key.substr(128, 32); } temp.erase(temp.begin() + 52, temp.end()); for(uint8_t x = 0; x < temp.size(); x++){ k.push_back(toint(temp[x], 16)); } keyset = true; }
std::string DES::run(const std::string & DATA){ if (!keyset){ throw std::runtime_error("Error: Key has not been set."); } if (DATA.size() != 8){ throw std::runtime_error("Error: Data must be 64 bits in length."); } std::string data = "", temp = ""; for(uint8_t x = 0; x < 8; x++){ data += makebin(static_cast <uint8_t> (DATA[x]), 8); } // IP for(uint8_t x = 0; x < 64; x++){ temp += data[DES_IP[x] - 1]; } data = temp; for(uint8_t x = 0; x < 16; x++){ // split left and right and duplicate right std::string left = data.substr(0, 32); std::string right = data.substr(32, 32); std::string old_right = right; // expand right side uint64_t t = 0; temp = ""; for(uint8_t y = 0; y < 48; y++){ temp += right[DES_EX[y] - 1]; } t = toint(temp, 2); // expanded_right xor key right = makebin(t ^ keys[x], 48); // split right into 8 parts std::string RIGHT[8]; for(uint8_t y = 0; y < 8; y++){ RIGHT[y] = right.substr(6 * y, 6); } // use sboxes temp = ""; for(uint8_t y = 0; y < 8; y++){ std::string s = ""; s += RIGHT[y][0]; s += RIGHT[y][5]; temp += makebin(DES_S_BOX[y][toint(s, 2)][toint(RIGHT[y].substr(1, 4), 2)], 4); } // permutate right = ""; for(uint8_t y = 0; y < 32; y++){ right += temp[DES_P[y]-1]; } // right xor left and combine with old right data = old_right + makebin(toint(left, 2) ^ toint(right, 2), 32); } // reverse last switch data = data.substr(32, 32) + data.substr(0, 32); // IP^-1 uint64_t out = 0; for(uint8_t x = 0; x < 64; x++){ out += static_cast <uint64_t> (data[DES_INVIP[x] - 1] == '1') << (63 - x); } return unhexlify(makehex(out, 16)); }