This article presents the Blowfish algorithm implementation in Objective-C for use in iPhone/iPad projects. The source code is available for download below and you can readily include the header and objective-C files in your iOS project. Two cipher modes are implemented in the code, namely ECB (electronic codebook) and CBC (cypher-block chaining).
What is Blowfish Algorithm?
Blowfish is a keyed symmetric block encryption algorithm, designed by Bruce Schneier. The word 'symmetric' means that it uses the same secret 'key' to encrypt as well as decrypt the messages. The word 'block cipher' means that it divides the input message into fixed-length blocks during the encryption/decryption process. The block-length for Blowfish is 8 bytes i.e. 64 bits, and the key length can be upto a maximum of 56 bytes i.e. 448 bits.
Blowfish algorithm has been claimed as reasonably strong and looks like no effective cryptanalysis has been found so far. It is an ideal algorithm for use in resource-constrained environments like embedded systems. The author of Blowfish has stated that the algorithm will remain unpatented and will be available in public domain and can be freely used by anyone. More information on Blowfish is available at http://en.wikipedia.org/wiki/Blowfish_(cipher)
The following image shows the iPhone test application for Blowfish encryption/decryption.
The following image shows the Xcode screenshot of the iPhone test application. The image shows the header and objective-c files available for using in your iPhone application.
Using the code
The following code shows the event-handler of the 'Encrypt' button . The code shows how to use the BlowfishAlgorithm Objective-C class for encrypting a user-specified plain text. You can provide a random key and initialization-vector. The key can be 1 to 56 bytes in length and the init-vector should be 8 bytes in length.
Downloads for this article
|Blowfish-ObjectiveC-Source 47.39 kb (289 downloads)
||iOS SDK 4.3, Xcode 4.6