83e0694e96ee398aea29774fb7feb92de6803a2a
[my-code/crypto.git] / test.c
1 /*
2  * test.c - test ciphers ...
3  *
4  * hackbard@hackdaworld.dyndns.org
5  *
6  */
7
8 #include <stdio.h>
9 #include <sys/types.h>
10 #include <sys/stat.h>
11 #include <fcntl.h>
12 #include <unistd.h>
13 #include <string.h>
14 #include "des.h"
15
16
17 int main() {
18  u8 plain[64];
19  u8 crypted[64];
20  u8 key[8];
21  int fd,i;
22
23  if((fd=open("/dev/urandom",O_RDONLY))<0) {
24   puts("open urandom device failed!");
25   return fd;
26  }
27
28  printf("des crypt/decrypt test:\n");
29  printf("- geberating random key ...\n");
30  read(fd,key,8);
31  printf("key: ");
32  for(i=0;i<8;i++) printf("%02x ",key[i]);
33  puts("");
34
35  memset(plain,0,64);
36  strcpy(plain,"allyouratmels ... :)");
37  
38  printf("encrypting '%s' (ecb mode) ...\n",plain);
39  memset(crypted,0,64);
40  des_encrypt(plain,crypted,key,64,MODE_ECB);
41  printf("crypted: ");
42  for(i=0;i<64;i++) printf("%c",crypted[i]);
43  puts("");
44  printf("encrypting '%s' (cbc mode) ...\n",plain);
45  memset(crypted,0,64);
46  des_encrypt(plain,crypted,key,64,MODE_CBC);
47  printf("crypted: ");
48  for(i=0;i<64;i++) printf("%c",crypted[i]);
49  puts("");
50
51  printf("--\n");
52  printf("verify:\n");
53  des_decrypt(crypted,plain,key,64,MODE_CBC);
54  printf("plain text: ");
55  for(i=0;i<64;i++) printf("%c",plain[i]);
56  puts("");
57
58  puts("done :)");
59
60  return 1;
61 }