String hex2bin(String hex) {
String bin = "";
for (char c : hex) {
int value;
if (c >= '0' && c <= '9') {
value = c - '0';
} else if (c >= 'A' && c <= 'F') {
value = c - 'A' + 10;
} else {
Serial.println("Not a valid hexadecimal string");
return "";
}
for (int i = 3; i >= 0; i--) {
bin += ((value >> i) & 1) ? '1' : '0';
}
}
return bin;
}
void generateKeys(String keyText, int key[16][48]) {
int key56[56];
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};
for (int i = 0; i < 64; i++) {
key56[i] = (keyText.charAt(i / 4) - '0') & 1;
}
for (int i = 0; i < 56; i++) {
key56[i] = key56[keyp[i] - 1];
}
int keyl[28], keyr[28];
for (int i = 0; i < 28; i++) {
keyl[i] = key56[i];
keyr[i] = key56[i + 28];
}
int key2[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};
for (int round = 0; round < 16; round++) {
int nshift = (round == 0 || round == 1 || round == 8 || round == 15) ? 1 : 2;
while (nshift--) {
int temp1 = keyl[0], temp2 = keyr[0];
for (int j = 0; j < 27; j++) {
keyl[j] = keyl[j + 1];
keyr[j] = keyr[j + 1];
}
keyl[27] = temp1;
keyr[27] = temp2;
}
for (int j = 0; j < 24; j++) {
key[round][j] = keyl[key2[j] - 1];
}
for (int j = 24; j < 48; j++) {
key[round][j] = keyr[key2[j] - 1 - 28];
}
}
}
String des(String text, String keyText, bool decrypt = false) {
String p = hex2bin(text);
String kp = hex2bin(keyText);
if (p.length() != 64 || kp.length() != 64) {
Serial.println("Invalid input length");
return "";
}
int key[16][48];
generateKeys(kp, key);
// Initial permutation and splitting into left (L) and right (R)
String l = p.substring(0, 32);
String r = p.substring(32, 64);
// DES process
for (int round = 0; round < 16; round++) {
String temp = l;
String ep = ""; // Expansion permutation
int t = 1;
// Expansion P box
ep += l[31];
for (int i = 0; i < 32; i++) {
if ((t + 1) % 6 == 0) {
ep += l[4 * ((t + 1) / 6)];
t++;
}
if (t % 6 == 0 && i != 0) {
ep += l[4 * (t / 6) - 1];
t++;
}
ep += l[i];
t++;
}
ep += l[0];
// Key XOR with output of expansion P box
String xorout = "";
for (int i = 0; i < 48; i++) {
xorout += char(((ep.charAt(i) - '0') ^ key[decrypt ? (15 - round) : round][i]) + '0');
}
// S-box compression
String sout = "";
int s[8][4][16] = {
{14, 4, 13, 1, 2, 15, 11, 8, 3, 10, 6, 12, 5, 9, 0, 7},
{15, 1, 8, 14, 6, 11, 3, 4, 9, 7, 2, 13, 12, 0, 5, 10},
{10, 0, 9, 14, 6, 3, 15, 5, 1, 13, 12, 7, 11, 4, 2, 8},
{7, 13, 14, 3, 0, 6, 9, 10, 1, 2, 8, 5, 11, 12, 4, 15},
{2, 12, 4, 1, 7, 10, 11, 6, 8, 5, 3, 15, 13, 0, 14, 9},
{12, 1, 10, 15, 9, 2, 6, 8, 0, 13, 3, 4, 14, 7, 5, 11},
{4, 11, 2, 14, 15, 0, 8, 13, 3, 12, 9, 7, 5, 10, 6, 1},
{13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7}
};
for (int i = 0; i < 48; i += 6) {
int row = (xorout.charAt(i + 5) - '0') + 2 * (xorout.charAt(i) - '0');
int col = (xorout.charAt(i + 1) - '0') * 8 + (xorout.charAt(i + 2) - '0') * 4 +
(xorout.charAt(i + 3) - '0') * 2 + (xorout.charAt(i + 4) - '0');
int temp = s[i / 6][row][col];
String sbin = "";
for (int j = 3; j >= 0; j--) {
sbin += ((temp >> j) & 1) ? '1' : '0';
}
sout += sbin;
}
// Straight P-box permutation
int per[32] = {16, 7, 20, 21, 29, 12, 28, 17,
1, 15, 23, 26, 5, 18, 31, 10,
2, 8, 24, 14, 32, 27, 3, 9,
19, 13, 30, 6, 22, 11, 4, 25};
String pc = "";
for (int i = 0; i < 32; i++) {
pc += sout[per[i] - 1];
}
// XOR with the left half
l = r;
r = "";
for (int i = 0; i < 32; i++) {
r += char(((pc.charAt(i) - '0') ^ (temp.charAt(i) - '0')) + '0');
}
}
// Final permutation
String finalPerm = l + r;
int fperm[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};
String result = "";
for (int i = 0; i < 64; i++) {
result += finalPerm[fperm[i] - 1];
}
return result;
}
void setup() {
Serial.begin(115200);
String cipher = "0123456789ABCDEF"; // Example ciphertext
String keyText = "133457799BBCDFF1"; // Example key
String decrypted = des(cipher, keyText, true);
Serial.println("Decrypted: " + decrypted);
}
void loop() {
// Do nothing
Serial.println("ooy");
}