objective-cswiftfirebaserncryptor

How to use RNCryptor and Firebase togeather - Swift & Objective C


is it possible to use RNCryptor and firebase together? You cant store NSData into firebase and thats what RNCryptor uses?

What other ways can i encrypt users data for the backend?


Solution

  • The answer is yes! you can use it with Firebase.

    We have RNCryptor integrated into a project and are using it to encrypt and decrypt private data stored in Firebase.

    Encrypt in ObjC

    NSString *plainText = @"Hello!"
    NSData *data = [plainText dataUsingEncoding:NSUTF8StringEncoding];
    NSError *error;
    NSData *encryptedData = [RNEncryptor encryptData:data
                                        withSettings:kRNCryptorAES256Settings
                                            password:aKey
                                               error:&error];
    
    NSString *stringFromEncryptedData = [encryptedData 
             base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
    

    Here's a decrypt pattern in ObjC

    NSData *dataFromEncryptedString = [[NSData alloc] 
              initWithBase64EncodedString:encryptedString 
                                  options:NSDataBase64DecodingIgnoreUnknownCharacters];
    NSError *error;
    NSData *decryptedData = [RNDecryptor decryptData:dataFromEncryptedString
                                        withPassword:aKey
                                               error:&error];
    
    NSString *plainText = [[NSString alloc] initWithData:decryptedData
                                                encoding:NSUTF8StringEncoding];
    

    Note that aKey is the secret string pattern you want to use to encrypt/decrypt your plain text string.