Search code examples
cencryptionblowfish

Blowfish C example for char*


I need help in Kocher's blowfish algorythm to implement in C. Its url is(http://www.schneier.com/code/bfsh-koc.zip). I could do the basics (initialize), but decryption isn't well. I know I should work with longs, but please help me to write a char * function what needs (char *encrypted_text) and returns the decrypted text. THANKS


Solution

  • here are the basics of encryption, how it works in code depends on the implementation:

    do some sort of initialization of state, include the encryption key up to 56 bytes, set up the mode (cbc, ecb, etc)

    feed into your machine (Block Size) byte chunks of data until you are out of data... be sure to pad the end of the data stream somehow to get to 8 bytes...

    now that your have completed you can extract the hash from the state...

    see doesn't that sound easy...

    now an openSSL example:

    void *source = "12345678";
    size_t len = strlen(source);
    assert(len % BF_BLOCK == 0);
    void *dest = malloc(len);
    BF_KEY key;
    BF_set_key(&key, 5, "12345"); // make a key
    
    while(len > 0) {
        BF_ecb_encrypt(source, dest, key, 1);// or other BF function see docs.
        source += BF_BLOCK;
        dest += BF_BLOCK;
        len -= BF_BLOCK;
    }