File indexing completed on 2024-06-23 03:58:16

0001 #include "blowfish.h"
0002 #include "cbc.h"
0003 #include <stdio.h>
0004 #include <stdlib.h>
0005 #include <string.h>
0006 
0007 int main()
0008 {
0009     BlockCipher *bf;
0010     char data[] = "This is a test.";
0011     const char expect[] = "\x3f\x3c\x2d\xae\x8c\x7\x84\xf2\xa7\x6d\x28\xbd\xd\xb\xb8\x79";
0012     char key[] = "testkey";
0013     unsigned long et[] = {0x11223344};
0014 
0015     printf("%d:  0x11 == %d and 0x44 == %d\n", ((unsigned char *)et)[0], 0x11, 0x44);
0016     bf = new BlowFish();
0017     //  bf = new CipherBlockChain(new BlowFish());
0018 
0019     bf->setKey((void *)key, 7 * 8);
0020 
0021     if (!bf->readyToGo()) {
0022         printf("Error: not ready to go!\n");
0023         return -1;
0024     }
0025 
0026     printf("About to encrypt...\n");
0027     fflush(stdout);
0028     if (-1 == bf->encrypt((void *)data, 16)) {
0029         printf("Error: encrypt failed!\n");
0030         return -1;
0031     }
0032 
0033     printf("Encryption done.  data[] is now: ");
0034     for (int i = 0; i < 16; i++) {
0035         printf("0x%x ", data[i] & 0xff);
0036         if ((data[i] & 0xff) != (expect[i] & 0xff)) {
0037             printf("Error.  This byte failed the comparison.  It should have been 0x%x.\n", expect[i] & 0xff);
0038             break;
0039         }
0040     }
0041     printf("\n");
0042 
0043     delete bf;
0044     bf = new BlowFish();
0045     //  bf = new CipherBlockChain(new BlowFish());
0046     bf->setKey((void *)key, 7 * 8);
0047 
0048     printf("About to decrypt...\n");
0049     fflush(stdout);
0050     if (-1 == bf->decrypt((void *)data, 16)) {
0051         printf("Error: decrypt failed!\n");
0052         return -1;
0053     }
0054     // bf->decrypt((void *)(data+8), 8);
0055 
0056     printf("All done!  Result...  data[] = \"%s\"\n", data);
0057     if (strcmp(data, "This is a test.")) {
0058         printf("ERROR. Decryption failed.\n");
0059         return -1;
0060     }
0061 
0062     delete bf;
0063 }