1

I have googled a lot but enable to get Blowfish ECB algorithm with PKCS5 padding in Objective-C.

I have tried code from here but it is not giving me proper encrypted data. Even I have tried codes from here but it is not with PKSC5 padding.

Unfortunately I have to use Blowfish (no other option) to convert following JAVA code in Objective-C

String objVal=<the json>;
SecretKeySpec lKeySpec = new SecretKeySpec(lKey.getBytes("UTF8"),"Blowfish");
Cipher lCipher = Cipher.getInstance("Blowfish/ECB/PKCS5Padding");
lCipher.init(Cipher.ENCRYPT_MODE, lKeySpec);
byte[] lPassword = objVal.getBytes("UTF8");
byte[] lEncryptPassword = lCipher.doFinal(lPassword);
String lEncryptString = new BASE64Encoder().encode(lEncryptPassword);
StringBuffer nString = new StringBuffer();
for (int i = 0; i < lEncryptString.length(); i++) {
int a = lEncryptString.charAt(i);
if (a != 13 && a != 10 && !lEncryptString.substring(i, i + 1).equals(" ")){
nString.append(lEncryptString.charAt(i));
}
return nString.toString();

Then the encrypted json is encoded:

String returnData=<encrypted json>
byte[] inputBytes = returnData.getBytes();
returnData  = DatatypeConverter.printBase64Binary(inputBytes);

Any one has tried and tested solution for Blowfish ECB algorithm with PKSC5 padding.

Thank you in advance.

I know this question is already (several times) asked but either not answered or not with PKCS5 padding

Martin Thorsen Ranang
  • 2,394
  • 1
  • 28
  • 43
Chetan Koli
  • 178
  • 15
  • Add sample key, input and output in he'd format to the question. Common Crypto supports Blowfish. – zaph Jun 16 '15 at 10:28

1 Answers1

3

Example code:

Add Security.framework

#import <CommonCrypto/CommonCryptor.h>

+ (NSData *)doBlowfish:(NSData *)dataIn
               context:(CCOperation)kCCEncrypt_or_kCCDecrypt
                   key:(NSData *)key
               options:(CCOptions)options
                    iv:(NSData *)iv
                 error:(NSError **)error
{
    CCCryptorStatus ccStatus   = kCCSuccess;
    size_t          cryptBytes = 0;
    NSMutableData  *dataOut    = [NSMutableData dataWithLength:dataIn.length + kCCBlockSizeBlowfish];

    ccStatus = CCCrypt( kCCEncrypt_or_kCCDecrypt,
                       kCCAlgorithmBlowfish,
                       options,
                       key.bytes,
                       key.length,
                       (iv)?nil:iv.bytes,
                       dataIn.bytes,
                       dataIn.length,
                       dataOut.mutableBytes,
                       dataOut.length,
                       &cryptBytes);

    if (ccStatus == kCCSuccess) {
        dataOut.length = cryptBytes;
    }
    else {
        if (error) {
            *error = [NSError errorWithDomain:@"kEncryptionError"
                                         code:ccStatus
                                     userInfo:nil];
        }
        dataOut = nil;
    }

    return dataOut;
}

Test:

NSError *error;
NSData *key = [@"Blowfish" dataUsingEncoding:NSUTF8StringEncoding];
NSString *stringOriginal = @"TestData";
NSData *dataOriginal = [stringOriginal dataUsingEncoding:NSUTF8StringEncoding];;

NSLog(@"key %@", key);
NSLog(@"stringOriginal %@", stringOriginal);
NSLog(@"dataOriginal   %@", dataOriginal);

NSData *dataEncrypted = [Test doBlowfish:dataOriginal
                                 context:kCCEncrypt
                                     key:key
                                 options:kCCOptionPKCS7Padding | kCCOptionECBMode
                                      iv:nil
                                   error:&error];
NSLog(@"dataEncrypted  %@", dataEncrypted);

NSString *encryptedBase64String = [dataEncrypted base64EncodedStringWithOptions:0];
NSLog(@"encryptedBase64String  %@", encryptedBase64String);


NSData *dataToDecrypt = [[NSData alloc] initWithBase64EncodedString:encryptedBase64String options:0];
NSData *dataDecrypted = [Test doBlowfish:dataToDecrypt
                                 context:kCCDecrypt
                                     key:key
                                 options:kCCOptionPKCS7Padding | kCCOptionECBMode
                                      iv:nil
                                   error:&error];
NSLog(@"dataDecrypted  %@", dataDecrypted);

NSString *stringDecrypted = [[NSString alloc] initWithData:dataDecrypted encoding:NSUTF8StringEncoding];
NSLog(@"stringDecrypted %@", stringDecrypted);

Output:

key 426c6f77 66697368
stringOriginal TestData
dataOriginal   54657374 44617461
dataEncrypted  ba5eb956 7e73ae1a b5513ea1 75a14019
encryptedBase64String  ul65Vn5zrhrdmeYV9B5rQA==
dataDecrypted  54657374 44617461
stringDecrypted TestData
zaph
  • 111,848
  • 21
  • 189
  • 228
  • Thanks for your reply but from where I can get CCCryptorStatus and CCOptions. Sorry for such simple question as I have never come across this library . – Chetan Koli Jun 18 '15 at 11:21
  • The options and context are in `CommonCrypto/CommonCryptor.h` which is part of the `Security.framework`. – zaph Jun 18 '15 at 11:35
  • Thanks for your Hint, I was not in position to try this code in these days. To get desired encrypted String, I have to convert 'encryptedBase64String' to NSData and again into String. `NSData *inputBytes = [encryptedBase64String dataUsingEncoding:NSUTF8StringEncoding];` `NSString *encryptedString = [inputBytes base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];` Now to at time of decryption, I am doing reverse way. String to NSData and then String again and that String is passed for decryption. But I am getting null String. Any suggestion. – Chetan Koli Jun 22 '15 at 06:36
  • You can just skip any Base64 code in the test, I made that separate from the encryption/decryption and just added them in the test code. Encrypted data can not be represented directly as a `NSString` or any string, it is data and generally data can not be representable as a string, that is why Base4 is often used. Just for starters, the first 32 ASCII character cods are control characters that were once used to control teletypes and modems and are not printable. – zaph Jun 22 '15 at 10:49
  • Thanks for solution, still I am not able to do decryption. As I don't need decryption solution, I am accepting your solution. But in future I will take your help for the same. – Chetan Koli Jul 01 '15 at 05:36