0% found this document useful (0 votes)
11 views4 pages

DES_compressed

The document contains a C++ implementation of the Simplified Data Encryption Standard (S-DES) algorithm, which includes key generation, encryption, and decryption functions. It utilizes permutation and substitution techniques with defined S-boxes and allows input in both binary and hexadecimal formats. The main function provides a user interface for encrypting and decrypting messages using a 10-bit key and 8-bit plaintext or ciphertext.

Uploaded by

atharvabhangale3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views4 pages

DES_compressed

The document contains a C++ implementation of the Simplified Data Encryption Standard (S-DES) algorithm, which includes key generation, encryption, and decryption functions. It utilizes permutation and substitution techniques with defined S-boxes and allows input in both binary and hexadecimal formats. The main function provides a user interface for encrypting and decrypting messages using a 10-bit key and 8-bit plaintext or ciphertext.

Uploaded by

atharvabhangale3
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

DES

#include <iostream>
#include <bitset>
#include <string>
#include <cstdlib>
using namespace std;
// Permutation functions
const int P10[10] = {3, 5, 2, 7, 4, 10, 1, 9, 8, 6};
const int P8[8] = {6, 3, 7, 4, 8, 5, 10, 9};
const int IP[8] = {2, 6, 3, 1, 4, 8, 5, 7};
const int IP_INV[8] = {4, 1, 3, 5, 7, 2, 8, 6};
const int EP[8] = {4, 1, 2, 3, 2, 3, 4, 1};
const int P4[4] = {2, 4, 3, 1};
// S-Boxes
int S0[4][4] = {
{1, 0, 3, 2},
{3, 2, 1, 0},
{0, 2, 1, 3},
{3, 1, 3, 2}};
int S1[4][4] = {
{0, 1, 2, 3},
{2, 0, 1, 3},
{3, 0, 1, 0},
{2, 1, 0, 3}};
// Function to perform permutation
string permute(string key, const int* table, int n) {
string permuted_key = "";
for (int i = 0; i < n; i++) {
permuted_key += key[table[i] - 1];
}
return permuted_key;
}
// Left shift operation
string leftShift(string key, int shifts) {
return key.substr(shifts) + key.substr(0, shifts);
}
// S-Box substitution
string sBoxSubstitution(string input, int sBox[4][4]) {
int row = (input[0] - '0') * 2 + (input[3] - '0');
int col = (input[1] - '0') * 2 + (input[2] - '0');
int val = sBox[row][col];
return bitset<2>(val).to_string();
}
// Function to generate subkeys
void generateKeys(string key, string &k1, string &k2) {
key = permute(key, P10, 10);
string left = key.substr(0, 5), right = key.substr(5, 5);
left = leftShift(left, 1);
right = leftShift(right, 1);
k1 = permute(left + right, P8, 8);
left = leftShift(left, 2);
right = leftShift(right, 2);
k2 = permute(left + right, P8, 8);
}
// Function for F function
string fFunction(string right, string key) {
string expanded = permute(right, EP, 8);
unsigned long xorResult = bitset<8>(expanded).to_ulong() ^
bitset<8>(key).to_ulong();
string xorBinary = bitset<8>(xorResult).to_string(); // ✅ Corrected
string s0Sub = sBoxSubstitution(xorBinary.substr(0, 4), S0);
string s1Sub = sBoxSubstitution(xorBinary.substr(4, 4), S1);
return permute(s0Sub + s1Sub, P4, 4);
}
// Function to perform S-DES encryption or decryption
string sdes(string text, string key, bool encrypt) {
string k1, k2;
generateKeys(key, k1, k2);
if (!encrypt) swap(k1, k2);
text = permute(text, IP, 8);
string left = text.substr(0, 4), right = text.substr(4, 4);
string fOut = fFunction(right, k1);
left = bitset<4>(bitset<4>(left).to_ulong() ^
bitset<4>(fOut).to_ulong()).to_string();
swap(left, right);
fOut = fFunction(right, k2);
left = bitset<4>(bitset<4>(left).to_ulong() ^
bitset<4>(fOut).to_ulong()).to_string();
return permute(left + right, IP_INV, 8);
}
// Convert hex to binary
string hexToBinary(string hex) {
string binary = "";
for (char c : hex) {
int num = (c >= '0' && c <= '9') ? c - '0' : (c >= 'A' && c <= 'F') ? c -
'A' + 10 : -1;
if (num == -1) return "";
binary += bitset<4>(num).to_string();
}
return binary;
}
// Convert binary to hex
string binaryToHex(string binary) {
string hex = "";
for (size_t i = 0; i < binary.length(); i += 4) {
int num = bitset<4>(binary.substr(i, 4)).to_ulong();
hex += (num < 10) ? char(num + '0') : char(num - 10 + 'A');
}
return hex;
}
// Validate binary input
bool isValidBinary(string bin) {
return bin.length() == 8 && bin.find_first_not_of("01") ==
string::npos;
}
// Validate hexadecimal input
bool isValidHex(string hex) {
return hex.length() == 2 &&
hex.find_first_not_of("0123456789ABCDEF") == string::npos;
}
// Main function
int main() {
string plaintext, key, ciphertext;
int choice;
while (true) {
cout << "\n--- S-DES Simulation ---\n";
cout << "1. Encrypt\n2. Decrypt\n3. Exit\n";
cout << "Enter your choice: ";
cin >> choice;
if (choice == 3) break;
cout << "Enter 10-bit key in binary: ";
cin >> key;
if (key.length() != 10 || key.find_first_not_of("01") !=
string::npos) {
cout << "Invalid key! Must be 10-bit binary.\n";
continue;
}
cout << "Enter 8-bit plaintext/ciphertext (binary) or 2-digit hex:
";
cin >> plaintext;
string inputBinary;
if (isValidBinary(plaintext)) {
inputBinary = plaintext;
} else if (isValidHex(plaintext)) {
inputBinary = hexToBinary(plaintext);
} else {
cout << "Invalid input! Must be 8-bit binary or 2-digit hex.\n";
continue;
}
string outputBinary = sdes(inputBinary, key, (choice == 1));
string outputHex = binaryToHex(outputBinary);
cout << "Result (Binary): " << outputBinary << endl;
cout << "Result (Hex): " << outputHex << endl;
}
return 0;
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy