I want to implement Electronic codebook (ECB) Cipher-block chaining (CBC) Cipher feedback (CFB) Output feedback (OFB) Counter (CTR) modes either in c++ or in java. But I don’t know how and where to start. Can any one please suggest me the steps to do?
I want to implement Electronic codebook (ECB) Cipher-block chaining (CBC) Cipher feedback (CFB) Output
Share
It is actually pretty simple, let’s assume you have a function called
block_cipher_encrypt(plaintext, key)that takes a single block of plaintext and a key as input and returns a single block of ciphertext.Now, say you have an array of blocks of plaintext (say
pt[i]is the ith block of plaintext) and an arrayctfor ciphertext blocks. To do ECB, it would be:For CBC mode, you also need an IV, but it is still pretty simple:
For the other modes, just look at Wikipedia’s article on modes of operation. They provide nice block diagrams for both encryption and decryption. That is what I did for the pseudocode above.