1 #include <stdint.h> 2 #include <stdlib.h> 3 #include <unistd.h> 4 5 #include "crypt_des.h" 6 7 static struct expanded_key __encrypt_key; 8 9 void setkey(const char *key) 10 { 11 unsigned char bkey[8]; 12 int i, j; 13 14 for (i = 0; i < 8; i++) { 15 bkey[i] = 0; 16 for (j = 7; j >= 0; j--, key++) 17 bkey[i] |= (uint32_t)(*key & 1) << j; 18 } 19 20 __des_setkey(bkey, &__encrypt_key); 21 } 22 23 void encrypt(char *block, int edflag) 24 { 25 struct expanded_key decrypt_key, *key; 26 uint32_t b[2]; 27 int i, j; 28 char *p; 29 30 p = block; 31 for (i = 0; i < 2; i++) { 32 b[i] = 0; 33 for (j = 31; j >= 0; j--, p++) 34 b[i] |= (uint32_t)(*p & 1) << j; 35 } 36 37 key = &__encrypt_key; 38 if (edflag) { 39 key = &decrypt_key; 40 for (i = 0; i < 16; i++) { 41 decrypt_key.l[i] = __encrypt_key.l[15-i]; 42 decrypt_key.r[i] = __encrypt_key.r[15-i]; 43 } 44 } 45 46 __do_des(b[0], b[1], b, b + 1, 1, 0, key); 47 48 p = block; 49 for (i = 0; i < 2; i++) 50 for (j = 31; j >= 0; j--) 51 *p++ = b[i]>>j & 1; 52 } 53