Friday 15 March 2013

c++ - Serpent Implementation in Crypto++ -



c++ - Serpent Implementation in Crypto++ -

i seek implement serpent algorithm using crypto++. found little document related cryptopp::serpent (including http://www.cryptopp.com/) can give me worked simple illustration of serpent?

from crypto++ wiki page on serpent:

autoseededrandompool prng; secbyteblock key(serpent::default_keylength); prng.generateblock(key, key.size()); byte iv[serpent::blocksize]; prng.generateblock(iv, sizeof(iv)); string plain = "cbc mode test"; string cipher, encoded, recovered; /*********************************\ \*********************************/ seek { cout << "plain text: " << plain << endl; cbc_mode< serpent >::encryption e; e.setkeywithiv(key, key.size(), iv); // streamtransformationfilter adds padding // required. ecb , cbc mode must padded // block size of cipher. stringsource ss1(plain, true, new streamtransformationfilter(e, new stringsink(cipher) ) // streamtransformationfilter ); // stringsource } catch(const cryptopp::exception& e) { cerr << e.what() << endl; exit(1); } /*********************************\ \*********************************/ // pretty print stringsource ss2(cipher, true, new hexencoder( new stringsink(encoded) ) // hexencoder ); // stringsource cout << "cipher text: " << encoded << endl; /*********************************\ \*********************************/ seek { cbc_mode< serpent >::decryption d; d.setkeywithiv(key, key.size(), iv); // streamtransformationfilter removes // padding required. stringsource ss3(cipher, true, new streamtransformationfilter(d, new stringsink(recovered) ) // streamtransformationfilter ); // stringsource cout << "recovered text: " << recovered << endl; } catch(const cryptopp::exception& e) { cerr << e.what() << endl; exit(1); }

a typical output shown below. note each run produce different results because key , initialization vector randomly generated.

$ ./driver.exe key: be4295539f6bd1752fd0a80229ef8847 iv: 00963f59224794d5ad4252094358fbc3 plain text: cbc mode test cipher text: cf2cf2547e02f6d34d97246e8042ed89 recovered text: cbc mode test

there's not much it. can swap block cipher in or out. same code work aes, camellia, 3des, etc.

c++ cryptography crypto++

No comments:

Post a Comment