ecb mode working, still bug in cbc mode.
[my-code/crypto.git] / test.c
diff --git a/test.c b/test.c
index a2e7067..98d1d6b 100644 (file)
--- a/test.c
+++ b/test.c
@@ -9,13 +9,16 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <fcntl.h>
-
+#include <unistd.h>
+#include <string.h>
 #include "des.h"
 
+#define LEN 32
 
 int main() {
- u8 plain[64];
- u8 crypted[64];
+ u8 plain[LEN];
+ u8 crypted[LEN];
+ u8 plain_new[LEN];
  u8 key[8];
  int fd,i;
 
@@ -25,34 +28,55 @@ int main() {
  }
 
  printf("des crypt/decrypt test:\n");
- printf("- geberating random key ...\n");
+ printf("- generating simple/random key ...\n");
  read(fd,key,8);
  printf("key: ");
  for(i=0;i<8;i++) printf("%02x ",key[i]);
  puts("");
 
- memset(plain,0,64);
- strcpy(plain,"allyouratmels ... :)");
+ memset(plain,0,LEN);
+ strcpy(plain,"allyourbase");
+ printf("encrypting '%s' \n",plain);
  
- printf("encrypting '%s' (ecb mode) ...\n",plain);
- memset(crypted,0,64);
- des_encrypt(plain,crypted,key,64,MODE_ECB);
- printf("crypted: ");
- for(i=0;i<64;i++) printf("%c",crypted[i]);
+ printf("ecb mode:\n");
+ memset(crypted,0,LEN);
+ des_encrypt(plain,crypted,key,LEN,MODE_ECB);
+ printf("plain: ");
+ for(i=0;i<LEN;i++) printf("%02x ",plain[i]);
  puts("");
- printf("encrypting '%s' (cbc mode) ...\n",plain);
- memset(crypted,0,64);
- des_encrypt(plain,crypted,key,64,MODE_CBC);
  printf("crypted: ");
- for(i=0;i<64;i++) printf("%c",crypted[i]);
+ for(i=0;i<LEN;i++) printf("%02x ",crypted[i]);
+ puts("");
+ 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);
 
- printf("--\n");
- printf("verify:\n");
- des_decrypt(crypted,plain,key,64,MODE_CBC);
- printf("plain text: ");
- for(i=0;i<64;i++) printf("%c",plain[i]);
+ /* test */
+ printf("plain: ");
+ 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);
 
  puts("done :)");