Last active
September 17, 2018 20:03
-
-
Save menangen/032b802bd96c214c6471879a63f8e8e2 to your computer and use it in GitHub Desktop.
newDES C implementation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// main.c | |
// newDES | |
// | |
// Created by menangen on 12.09.2018. | |
// Copyright © 2018 menangen. All rights reserved. | |
// | |
#include <stdio.h> | |
#import "newDES.h" | |
static | |
inline void | |
print_array (uint8_t * unsigned_array) { | |
for (int i = 0; i < 8; i++) { | |
printf(" %02x ", unsigned_array[i]); | |
} | |
printf("\n"); | |
} | |
int main(int argc, const char * argv[]) { | |
// insert code here... | |
printf("Hello, newDES!\n"); | |
uint8_t key [SIZE_USER_KEY] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, 0x0B, 0x0C, 0x0D, 0x0E }; | |
uint8_t encrypt_block [8] = { 0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07 }; | |
print_array(encrypt_block); | |
encrypt_data(encrypt_block, key); | |
print_array(encrypt_block); | |
decrypt_data(encrypt_block, key); | |
print_array(encrypt_block); | |
return 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// newDES.c | |
// newDES | |
// | |
// Created by menangen on 13.09.2018. | |
// Copyright © 2018 menangen. All rights reserved. | |
// | |
#include "newDES.h" | |
u8 hash(u8 firstByte, u8 data, u8 keyByte, u8 exKeyByte) { | |
// printf("[0] data: %02x \n", data); | |
data -= 199; // printf("[1] data: %02x \n", data); | |
data ^= keyByte; // printf("[2] data: %02x \n", data); | |
data -= 71; // printf("[3] data: %02x \n", data); | |
data -= (data << 5); // printf("[4] data: %02x \n", data); | |
data ^= exKeyByte; // printf("[5] data: %02x \n", data); | |
data -= 151; // printf("[6] data: %02x \n", data); | |
data -= (data << 1); // printf("[7] data: %02x \n\n", data); | |
return data ^ firstByte; | |
}; | |
void | |
encrypt_data (u8 * data, u8 *key) | |
{ | |
u8 B0 = data[0], B1 = data[1], B2 = data[2], B3 = data[3], B4 = | |
data[4], B5 = data[5], B6 = data[6], B7 = data[7]; | |
u8 i = 0, ex = 0; | |
while (1) | |
{ | |
B4 = hash(B4, B0, key[i], ex); | |
if (++i == SIZE_USER_KEY) | |
{ | |
i = 0; | |
ex = key[7]; | |
} | |
B5 = hash(B5, B1, key[i], ex); | |
if (++i == SIZE_USER_KEY) | |
{ | |
i = 0; | |
ex = key[8]; | |
} | |
B6 = hash(B6, B2, key[i], ex); | |
if (++i == SIZE_USER_KEY) | |
{ | |
i = 0; | |
ex = key[9]; | |
} | |
B7 = hash(B7, B3, key[i], ex); | |
if (++i == SIZE_USER_KEY) | |
break; | |
B1 = hash(B1, B4, key[i++], ex); | |
B2 = hash(B2, B5, B4, 0); | |
B3 = hash(B3, B6, key[i++], ex); | |
B0 = hash(B0, B7, key[i++], ex); | |
} | |
data[0] = B0; | |
data[1] = B1; | |
data[2] = B2; | |
data[3] = B3; | |
data[4] = B4; | |
data[5] = B5; | |
data[6] = B6; | |
data[7] = B7; | |
} | |
void | |
decrypt_data (u8 * data, u8 *key) | |
{ | |
u8 | |
B0 = data[0], B1 = data[1], | |
B2 = data[2], B3 = data[3], | |
B4 = data[4], B5 = data[5], | |
B6 = data[6], B7 = data[7]; | |
#define LAST_KEY_POSITION SIZE_USER_KEY - 1; | |
int8_t i = LAST_KEY_POSITION; | |
u8 ex = key[9]; | |
while (1) | |
{ | |
B7 = hash(B7, B3, key[i], ex); | |
if (--i < 0) | |
{ | |
i = LAST_KEY_POSITION; | |
ex = key[8]; | |
} | |
B6 = hash(B6, B2, key[i], ex); | |
if (--i < 0) | |
{ | |
i = LAST_KEY_POSITION; | |
ex = key[7]; | |
} | |
B5 = hash(B5, B1, key[i], ex); | |
if (--i < 0) | |
{ | |
i = LAST_KEY_POSITION; | |
ex = 0; | |
} | |
B4 = hash(B4, B0, key[i], ex); | |
if (--i < 0) | |
break; | |
B0 = hash(B0, B7, key[i--], ex); | |
B3 = hash(B3, B6, key[i--], ex); | |
B2 = hash(B2, B5, B4, 0); | |
B1 = hash(B1, B4, key[i--], ex); | |
} | |
data[0] = B0; | |
data[1] = B1; | |
data[2] = B2; | |
data[3] = B3; | |
data[4] = B4; | |
data[5] = B5; | |
data[6] = B6; | |
data[7] = B7; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// newDES.h | |
// newDES | |
// | |
// Created by menangen on 13.09.2018. | |
// Copyright © 2018 menangen. All rights reserved. | |
// | |
#ifndef newDES_h | |
#define newDES_h | |
#import <string.h> | |
#endif /* newDES_h */ | |
#define SIZE_USER_KEY 15 | |
typedef uint8_t u8; | |
void encrypt_data (u8 *data, u8 *key); | |
void decrypt_data (u8 *data, u8 *key); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment