*
*/
-#include <stdio.h>
#include <string.h>
#include "des.h"
for(i=4;i<6;i++)
obj[i]=(obj[i]<<1)|(obj[i+1]>>7);
obj[6]=obj[6]<<1|((mid&8)>>3);
- printf("debug: ");
- for(i=0;i<7;i++) printf("%02x ",obj[i]);
- printf("\n");
return 1;
}
u8 key0_56[DES_INITIAL_KEY_LEN/8];
/* initial permutation of key */
table_trans(key0,key0_56,key_perm_table,DES_INITIAL_KEY_LEN);
- for(i=0;i<7;i++) printf("%02x ",key0_56[i]);
- printf("\n");
for(i=0;i<DES_SUBKEYS;i++) {
/* split and rotate key_056 */
subkey_trans(key0_56,i);
/* sbox substitutions and pbox permutations */
data_s_and_p(permuted_data);
/* xor with left data */
- memcpy(tmp,right,DES_DATA_BLOCK_LEN/(2*8));
- for(j=0;j<DES_DATA_BLOCK_LEN/(2*8);j++)
- right[j]=permuted_data[j]^left[j];
- /* switch left and right data - not for last round */
- if(i!=15) memcpy(left,tmp,DES_DATA_BLOCK_LEN/(2*8));
+ if(i!=15) {
+ memcpy(tmp,right,DES_DATA_BLOCK_LEN/(2*8));
+ for(j=0;j<DES_DATA_BLOCK_LEN/(2*8);j++)
+ right[j]=permuted_data[j]^left[j];
+ /* switch left and right data - not for last round */
+ memcpy(left,tmp,DES_DATA_BLOCK_LEN/(2*8));
+ }
+ else
+ for(j=0;j<DES_DATA_BLOCK_LEN/(2*8);j++) left[j]^=permuted_data[j];
}
/* merge and do final permutation */
memcpy(permuted_data,left,DES_DATA_BLOCK_LEN/(2*8));
- loop 16 times
*/
compute_subkeys(key,subkey);
- printf("debug:\n");
- for(i=0;i<16;i++) {
- printf("subkey %d: ",i);
- for(j=0;j<6;j++) printf("%02x ",subkey[i][j]);
- printf("\n");
- }
/* split data to 64bit blocks, and crypt/decrypt each block
depending on mode (CBC/ECB):
- initial data transformation
#include <string.h>
#include "des.h"
+#define LEN 32
int main() {
- u8 plain[32];
- u8 crypted[32];
+ u8 plain[LEN];
+ u8 crypted[LEN];
+ u8 plain_new[LEN];
u8 key[8];
int fd,i;
printf("des crypt/decrypt test:\n");
printf("- generating simple/random key ...\n");
- // read(fd,key,8);
- memset(key,0x90,8);
+ read(fd,key,8);
printf("key: ");
for(i=0;i<8;i++) printf("%02x ",key[i]);
puts("");
- memset(plain,0,32);
- // strcpy(plain,"allyouratmels ... :)");
- printf("encrypting '");
- for(i=0;i<32;i++) printf("%02x%c",plain[i],i==31?'\n':' ');
+ memset(plain,0,LEN);
+ strcpy(plain,"allyourbase");
+ printf("encrypting '%s' \n",plain);
- printf("encrypting (ecb mode) ...\n");
- memset(crypted,0,32);
- des_encrypt(plain,crypted,key,32,MODE_ECB);
+ printf("ecb mode:\n");
+ memset(crypted,0,LEN);
+ des_encrypt(plain,crypted,key,LEN,MODE_ECB);
printf("plain: ");
- for(i=0;i<32;i++) printf("%02x ",plain[i]);
+ for(i=0;i<LEN;i++) printf("%02x ",plain[i]);
puts("");
printf("crypted: ");
- for(i=0;i<32;i++) printf("%02x ",crypted[i]);
+ for(i=0;i<LEN;i++) printf("%02x ",crypted[i]);
puts("");
- printf("decrypting (ecb mode) ...\n");
- memset(plain,0,32);
- des_decrypt(crypted,plain,key,32,MODE_ECB);
- printf("crypted: ");
- for(i=0;i<32;i++) printf("%02x ",crypted[i]);
+ des_decrypt(crypted,plain_new,key,LEN,MODE_ECB);
+ printf("new plain: ");
+ for(i=0;i<LEN;i++) printf("%02x ",plain[i]);
puts("");
+ if(!strcmp(plain,plain_new)) printf("looks good!\n");
+
+ printf("cbc mode:\n");
+ memset(crypted,0,LEN);
+ memset(plain_new,0,LEN);
+ printf("plain: ");
+ for(i=0;i<LEN;i++) printf("%02x ",plain[i]);
+ puts("");
+ des_encrypt(plain,crypted,key,LEN,MODE_CBC);
+
+ /* test */
printf("plain: ");
- for(i=0;i<32;i++) printf("%02x ",plain[i]);
+ for(i=0;i<LEN;i++) printf("%02x ",plain[i]);
+ puts("");
+ /* test end */
+
+ printf("crypted: ");
+ for(i=0;i<LEN;i++) printf("%02x ",crypted[i]);
+ puts("");
+ des_decrypt(crypted,plain_new,key,LEN,MODE_CBC);
+ printf("new plain: ");
+ for(i=0;i<LEN;i++) printf("%02x ",plain[i]);
puts("");
+ if(!strcmp(plain,plain_new)) printf("looks good!\n");
close(fd);