GO FURTHER, FASTER: Try the Swift Career Accelerator today! >>

Obfuscating JSON data

Forums > Swift

Hi All,

I'm saving my JSON data (via .fileExporter to iDrive) and, hurrah, it finally works saving all my classes and sub-classes. However, as the files are are on the iDrive they are easily accessible and their contents easily viewable, which exposes the underlying data - I don't really want that. The data is not going to be shared with any other app.

I appreciate that whatever I do is unlikely to deter any but the most casual user but is there a way I can simply obfuscate the data and still save as a text file?

Thanks in advance,

Jes

3      

There are a couple of things you could do:

Not Really Secure

Base64 encode the values before writing them to the file, when reading the file back in base64 decode them, this is not secure at all and anyone with technical knowledge is likely going to understand how to base64 decode.

Secure

Other 2 options would be to enxrypt the values prior to writing the file (and possibly best base64 encoding the encrypted value). This would leave the structure of the json file (or whatever file you are writing to disc) to remain as a file in that format, just the values would be unreadable

A better option, encrypt the whole file while writing it to disk. This would mean that a user opoening the fle wouldnt even know that it is a json file at all.

https://developer.apple.com/documentation/applearchive/encrypting_and_decrypting_a_string

Either of these secure options would require storing an encrpytion key. You could probably look at storing this in the keychain. Of course if this is lost it does mean that you would lose the data in the file.

3      

Hacking with Swift is sponsored by Alex.

SPONSORED Alex is the iOS & Mac developer’s ultimate AI assistant. It integrates with Xcode, offering a best-in-class Swift coding agent. Generate modern SwiftUI from images. Fast-apply suggestions from Claude 3.5 Sonnet, o3-mini, and DeepSeek R1. Autofix Swift 6 errors and warnings. And so much more. Start your 7-day free trial today!

Try for free!

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.