0

I have scanned a QR Code image using ZXingWidget lib, received a string as response. Now I want to encrypt that string using lib-sodium in iOS.

Any suggestion how to do string encryption in iOS using lib-sodium library.... Thanks in advance...

Shaik Riyaz
  • 11,204
  • 7
  • 53
  • 70
  • Why lib-sodium? What aspect of it is of particular interest? If all you want t do is "string encryption" just use CommonCrypto, there are several good wrappers including Rob Napier's [RNCryptor](https://github.com/RNCryptor/RNCryptor). – zaph Jan 08 '14 at 12:34
  • RNCryptor could you share simple string encryption using ASE ... – user3168665 Jan 09 '14 at 12:53
  • Could you please provide sample code if possible. on implementing PBKDF for encrypting a string. Thanks. – user3168665 Jan 10 '14 at 06:05

1 Answers1

0

As requested: Sample code on implementing PBKDF

Note, this is an example, not production code.

#import <CommonCrypto/CommonKeyDerivation.h>
+ (NSData *)doKeyForPassword:(NSString *)password
                        salt:(NSData *)salt
                     keySize:(NSUInteger)keySize
                      rounds:(NSUInteger)rounds {
    NSMutableData *derivedKey = [NSMutableData dataWithLength:keySize];

    NSData *passwordData = [password dataUsingEncoding: NSUTF8StringEncoding];

    CCKeyDerivationPBKDF(kCCPBKDF2, // algorithm
        passwordData,               // password
         passwordData,              // passwordLength
         salt.bytes,                // salt
         salt.length,               // saltLen
         kCCPRFHmacAlgSHA1,         // PRF
         rounds,                    // rounds
         derivedKey.mutableBytes,   // derivedKey
         derivedKey.length);        // derivedKeyLen

    return derivedKey;
}

Very simple test, use a better salt and a better rounds count, possible using CCCalibratePBKDF.

- (void)test_doKeyForPassword {
    NSData *key = [Crypto doKeyForPassword:@"password"
                                      salt:[@"salt" dataUsingEncoding:NSUTF8StringEncoding]
                                   keySize:kCCKeySizeAES128
                                    rounds:1000];
    NSLog(@"doKeyForPassword: %@",key);
}

If you are copying this code to use in a production app: Don't. This is just example code. Basically if one needs this code they should not be doing cryptography. Hire a domain expert, at a minimum have the code vetted by a domain expert.

zaph
  • 111,848
  • 21
  • 189
  • 228