Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I got Paul Kocher implementation from <a href="http://www.schneier.com/blowfish-download.html" rel="nofollow">Bruce Schneier's website</a>. And here is how an encryption method may look like: </p> <pre><code>#define PADDING_PHRASE @" " #import "CryptoUtilities.h" #import "blowfish.h" #import "NSData+Base64Utilities.h" @implementation CryptoUtilities + (NSString *)blowfishEncrypt:(NSData *)messageData usingKey:(NSData *)secretKey { NSMutableData *dataToEncrypt = [messageData mutableCopy]; if ([dataToEncrypt length] % 8) { NSMutableData *emptyData = [[PADDING_PHRASE dataUsingEncoding:NSUTF8StringEncoding] mutableCopy]; emptyData.length = 8 - [dataToEncrypt length] % 8; [dataToEncrypt appendData:emptyData]; } // Here we have data ready to encipher BLOWFISH_CTX ctx; Blowfish_Init (&amp;ctx, (unsigned char*)[secretKey bytes], [secretKey length]); NSRange aLeftRange, aRightRange; NSData *aLeftBox, *aRightBox; unsigned long dl = 0, dr = 0; for (int i = 0; i &lt; [dataToEncrypt length]; i += 8) { // Divide data into octets... // …and then into quartets aLeftRange = NSMakeRange(i, 4); aRightRange = NSMakeRange(i + 4, 4); aLeftBox = [dataToEncrypt subdataWithRange:aLeftRange]; aRightBox = [dataToEncrypt subdataWithRange:aRightRange]; // Convert bytes into unsigned long [aLeftBox getBytes:&amp;dl length:sizeof(unsigned long)]; [aRightBox getBytes:&amp;dr length:sizeof(unsigned long)]; // Encipher Blowfish_Encrypt(&amp;ctx, &amp;dl, &amp;dr); // Put bytes back [dataToEncrypt replaceBytesInRange:aLeftRange withBytes:&amp;dl]; [dataToEncrypt replaceBytesInRange:aRightRange withBytes:&amp;dr]; } return [dataToEncrypt getBase64String]; } </code></pre> <p>I am not really good in C, but it seems that my implementation works correctly. To decrypt you need just repeat same steps, but instead of <i>Blowfish_Encrypt</i> you need to call <i>Blowfish_Decrypt</i>.<br> Here is a source code for that (I assume that you just decrypt the cipher text, but don't deal with padding here):</p> <pre><code>+ (NSData *)blowfishDecrypt:(NSData *)messageData usingKey:(NSData *)secretKeyData { NSMutableData *decryptedData = [messageData mutableCopy]; BLOWFISH_CTX ctx; Blowfish_Init (&amp;ctx, (unsigned char*)[secretKeyData bytes], [secretKeyData length]); NSRange aLeftRange, aRightRange; NSData *aLeftBox, *aRightBox; unsigned long dl = 0, dr = 0; for (int i = 0; i&lt; [decryptedData length]; i += 8) { // Divide data into octets... // …and then into quartets aLeftRange = NSMakeRange(i, 4); aRightRange = NSMakeRange(i + 4, 4); aLeftBox = [decryptedData subdataWithRange:aLeftRange]; aRightBox = [decryptedData subdataWithRange:aRightRange]; // Convert bytes into unsigned long [aLeftBox getBytes:&amp;dl length:sizeof(unsigned long)]; [aRightBox getBytes:&amp;dr length:sizeof(unsigned long)]; // Decipher Blowfish_Decrypt(&amp;ctx, &amp;dl, &amp;dr); // Put bytes back [decryptedData replaceBytesInRange:aLeftRange withBytes:&amp;dl]; [decryptedData replaceBytesInRange:aRightRange withBytes:&amp;dr]; } return decryptedData; } </code></pre> <p>You might want to return pure bytes or Base64 string. For the last case I have a category, which adds an initialiser, which initialises NSData object with Base64 string and a method, which allows to get Base64 string from NSData.</p> <p>You should also think about playing with PADDING_PHRASE, for example, what if you want to add not just several spaces, but some random bytes? In this case you should send a padding length somehow.</p> <p><strong>Update:</strong> Actually, you <strong>should not</strong> use PADDING_PRASE in your process. Instead, you should use one of the standard algorithms for block ciphers described on <a href="http://en.wikipedia.org/wiki/Padding_%28cryptography%29#Block_cipher_mode_of_operation" rel="nofollow">Wikipedia page</a> </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload