Des Implementation in C
Data encryption is one of the most secure formats for transferring data or information. They were first introduced in the year 1970 and were coined by the team of IBM. They wanted something that ensured that their data was secure and wouldn't be interrupted or easily hacked by anyone. Hence, they came up with encryption technology. Data encryption typically means that we convert our data in such unreadable format or keywords that it becomes impossible for any imposter to hack or crack these keywords and access the files or our data.
In this tutorial, we will see the importance of data encryption and understand its key concepts.
1. Symmetric-Key Cryptography and DES
It is a secure way of keeping the data safe, as we have one key for the locking and unlocking mechanism. We only contain a public key during this form of cryptography. DES is basically this basic idea for making the data secure. For this, we just use a 56-bit key for the secure encryption.
2. Key Generation
Whenever we want to perform any sort of data encryption, the first thing we do is create keys to ensure that the keys and the data are secure. We must take a 56-bit secure key and then make 16 smaller ones.
3. Initial and Final Permutations
In data encryption technology, we generally mix the input and output data to get secure data. The start mixing of the input bit changes the order of the whole course, and it is followed throughout the whole design. The final mix of the bits is usually done to decode the final message.
4. Feistel Network Structure
The main procedure for data manipulation that performs the entire work is its Feistel network structure. It is responsible for dividing the data into two main parts and then repeating its steps repeatedly. In every step, we use a special network called the s-box, which works with the small keys for the locking and unlocking.
Encryption
The centre of the DES is how it uses its information and locks and unlocks everything. It is of prime importance. In every round, the data is combined, locked, and mixed up with some special unique keys. We have to use operations that work with the individual bits to ensure it works.
Example
#include <stdio.h>
#include <stdint.h>
// Example key (56-bit)
uint64_t key = 0x133457799BBCDFF1;
// DES encryption function (single round)
uint64_t des_encrypt(uint64_t data, uint64_t key) {
// Initial permutation (simplified for educational purposes)
data = (data << 1) | (data >> 63);
// Example: applying a round of Feistel network
uint64_t subkey = key; // In practice, a key schedule is performed
data ^= subkey;
// Final permutation (simplified for educational purposes)
data = (data << 1) | (data >> 63);
return data;
}
// DES decryption function (single round)
uint64_t des_decrypt(uint64_t data, uint64_t key) {
// Initial permutation (simplified for educational purposes)
data = (data << 1) | (data >> 63);
// Example: applying a round of Feistel network
uint64_t subkey = key; // In practice, a key schedule is performed
data ^= subkey;
// Final permutation (simplified for educational purposes)
data = (data << 1) | (data >> 63);
return data;
}
int main() {
// Example data (64-bit block)
uint64_t data_block = 0x0123456789ABCDEF;
// Encrypt data
uint64_t encrypted_data = des_encrypt(data_block, key);
printf("Encrypted Data: 0x%016llx\n", encrypted_data);
// Decrypt data
uint64_t decrypted_data = des_decrypt(encrypted_data, key);
printf("Decrypted Data: 0x%016llx\n", decrypted_data);
return 0;
}
Output:
![Des Implementation in C](https://static.tutorialandexample.com/full-form/des-implementation-in-c1.png)
Example 2:
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#define IP_SIZE 64
const int IP[IP_SIZE] = {112, 114, 113, 115, 116, 117, 118, 119,
120, 121, 122, 123, 124, 125, 126, 127,
245, 246, 247, 248, 249, 287, 290, 288,
399, 401, 402, 403, 404, 405, 406, 407,
57, 49, 41, 33, 25, 17, 9, 1,
59, 51, 43, 35, 27, 19, 11, 3,
61, 53, 45, 37, 29, 21, 13, 5,
63, 55, 47, 39, 31, 23, 15, 7};
const int IP__in[IP_SIZE] = {403, 407, 402, 406, 401, 405, 399, 127,
39, 7, 47, 15, 55, 23, 63, 31,
248, 61, 247, 290, 246, 287, 245, 249,
37, 5, 114, 13, 53, 21, 288, 29,
123, 404, 122, 112, 121, 125, 120, 124,
35, 3, 43, 11, 51, 19, 59, 27,
115, 119, 113, 1, 45, 117, 126, 116,
33, 118, 41, 9, 49, 17, 57, 25};
// Perform initial permutation
void initial_permutation(const uint8_t *inp, uint8_t *output) {
for (int i = 0; i < IP_SIZE; i++) {
output[i] = inp[IP[i] - 1];
}
}
// Perform initial permutation inverse
void initial_permutation_inverse(const uint8_t *inp, uint8_t *outcome) {
for (int i = 0; i < IP_SIZE; i++) {
outcome[i] = inp[IP__in[i] - 1];
}
}
int main() {
uint8_t inp[IP_SIZE], outcome[IP_SIZE], outcome_inverse[IP_SIZE];
// Example usage
for (int i = 0; i < IP_SIZE; i++) {
inp[i] = i + 1; // Just for demonstration
}
initial_permutation(inp, outcome);
initial_permutation_inverse(outcome, outcome_inverse);
// Displaying results
printf("Output after initial permutation:\n");
for (int i = 0; i < IP_SIZE; i++) {
printf("%d ", outcome[i]);
}
printf("\n\nOutput after inverse permutation:\n");
for (int i = 0; i < IP_SIZE; i++) {
printf("%d ", outcome_inverse[i]);
}
return 0;
}
Output:
![Des Implementation in C](https://static.tutorialandexample.com/full-form/des-implementation-in-c2.png)
Example 3:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Initial permutation table
int new_permutationfunction[64] = {
112, 114, 113, 115, 116, 117, 118, 119,
120, 121, 122, 123, 124, 125, 126, 127,
245, 246, 247, 248, 249, 287, 290, 288,
399, 401, 402, 403, 404, 405, 406, 407,
57, 49, 41, 33, 25, 17, 9, 1,
59, 51, 43, 35, 27, 19, 11, 3,
61, 53, 45, 37, 29, 21, 13, 5,
63, 55, 47, 39, 31, 23, 15, 7
};
// Key
char key[64] = "1010010100000011111111111111010101010111111111111111111111111001";
// normal text
char normaltext[64] = "1010110100000010101010100101010101010101010101010101010100101";
// Print binary string
void printBinary(const char *str) {
int length = strlen(str);
for (int i = 0; i < length; i++) {
printf("%c", str[i]);
if ((i + 1) % 8 == 0)
printf(" ");
}
printf("\n");
}
// Initial permutation function
void newPermutation(char *input, char *output) {
for (int i = 0; i < 64; i++) {
output[i] = input[new_permutationfunction[i] - 1];
}
output[64] = '\0';
}
int main() {
char permutedNormaltext[65];
char permutedKey[65];
newPermutation(normaltext, permutedNormaltext);
newPermutation(key, permutedKey);
printf("Key after we made the new permutation:\n");
printBinary(permutedKey);
return 0;
}
Output:
![Des Implementation in C](https://static.tutorialandexample.com/full-form/des-implementation-in-c3.png)
Conclusion
Trying out the Data Encryption Standard for the secure transmission of data and information in c is being widely used all over the world. It basically involves that we have to make special keys, mix some boxes and also use some special network to make the data secure and safe. Though DES has still some issues, there are better networks and opportunities that can help us make the system secure.med forces with various missiles and equipment. BDL has established its position as a key player in India's defence ecosystem through its persistent research and development efforts and dedication to meeting the growing needs of the defence sector.