Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8980905
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T20:12:08+00:00 2026-06-15T20:12:08+00:00

What is the best way to implement BlowFish ECB encryption in iOS ??? I

  • 0

What is the best way to implement BlowFish ECB encryption in iOS???
I have been googling a lot and found the library here. But there are no documentation of this library. Not sure how to use it.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T20:12:09+00:00Added an answer on June 15, 2026 at 8:12 pm

    I got Paul Kocher implementation from Bruce Schneier’s website. And here is how an encryption method may look like:

    #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 (&ctx, (unsigned char*)[secretKey bytes], [secretKey length]);
        
        NSRange aLeftRange, aRightRange;
        NSData *aLeftBox, *aRightBox;
        unsigned long dl = 0, dr = 0;
        
        for (int i = 0; i < [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:&dl length:sizeof(unsigned long)];
            [aRightBox getBytes:&dr length:sizeof(unsigned long)];
            
            // Encipher
            Blowfish_Encrypt(&ctx, &dl, &dr);
            
            // Put bytes back
            [dataToEncrypt replaceBytesInRange:aLeftRange withBytes:&dl];
            [dataToEncrypt replaceBytesInRange:aRightRange withBytes:&dr];
        }
        
        return [dataToEncrypt getBase64String];
    }
    

    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 Blowfish_Encrypt you need to call Blowfish_Decrypt.
    Here is a source code for that (I assume that you just decrypt the cipher text, but don’t deal with padding here):

    + (NSData *)blowfishDecrypt:(NSData *)messageData usingKey:(NSData *)secretKeyData
    {
        NSMutableData *decryptedData = [messageData mutableCopy];
        
        BLOWFISH_CTX ctx;
        Blowfish_Init (&ctx, (unsigned char*)[secretKeyData bytes], [secretKeyData length]);
        
        NSRange aLeftRange, aRightRange;
        NSData *aLeftBox, *aRightBox;
        unsigned long dl = 0, dr = 0;
        
        for (int i = 0; i< [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:&dl length:sizeof(unsigned long)];
            [aRightBox getBytes:&dr length:sizeof(unsigned long)];
            
            // Decipher
            Blowfish_Decrypt(&ctx, &dl, &dr);
            
            // Put bytes back
            [decryptedData replaceBytesInRange:aLeftRange withBytes:&dl];
            [decryptedData replaceBytesInRange:aRightRange withBytes:&dr];
        }
        
        return decryptedData;
    }
    

    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.

    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.

    Update: Actually, you should not use PADDING_PRASE in your process. Instead, you should use one of the standard algorithms for block ciphers described on Wikipedia page

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

What is the best way to implement simple pagination? Here is the code im
What is the best way to implement a rank system: here is the code
What is the best way to implement web services in PHP? I have heard
What's the best way to implement functionality in Android where I have a large
What is the best way to implement a StackOverflow-style Recent Activities page? I have
What's the best way to implement a you must have JS enabled message? My
What is the best way to implement a horizontal navbar in GWT? Using a
What is the best way to implement some kind of progress indicator in Play?
What is the best way to implement a three item hashMap? For example, I
What's the best way to implement a Hash that can be modified across multiple

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.