3

As per GDPR requirements, I am using Apple's built-in "Security" module to help with the encryption and decryption of information. I was able to generate my Private Key and Public Keys fairly easily using the documentation.

The problem I find myself at now is when trying to do encryption. In the Apple documentation I am following (found here), I was able to get to the SecKeyCreateEncryptedData function step. I run into errors on the line that says "plainText as CFData". Simply put, I am receiving a String object from a UITextField and properly retrieving the input text by adding the ".text" after my UITextField object.

When you do this, Xcode screams that a String object has to be forcibly downcast using "as!" if we want it as CFData. So I abided by Xcode, all to see that this does not work. I get "Thread 1: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0)" on the line where documentation says "plainText as CFData". When I looked into it through the debugger, it was a whole bunch of assembly that's honestly just beyond me.

Regardless, this confuses me because if a String isn't the plainText the documentation is referring to, then what is? My instinct simply told me to do some research on what CFData is in Swift so that I could cast/create my String object into a CFData object, and simply pass that into the argument instead of needing to cast it within the function. That searching led me here, which then led me to what I believe is a good solution here. Now, I could be wrong that's a good work-around, but it's currently all I can think of. I'm stuck now trying to figure out what arguments I pass into the CFDataCreate function. I understand the types, but without any examples around really (not even in the documentation), I just can't figure out how to get it, or what to pass in, and how to get my String object in the proper format it needs.

Thank you for any help, and if you have questions for me, please ask

0

1 Answer 1

12

In crypto-speak, “plaintext” means “unencrypted data”. It doesn’t necessarily have to be human-readable text.

You can convert a String to a CFData like this:

let string = "Hello, world!"
let data = string.data(using: .utf8)! as CFData
Sign up to request clarification or add additional context in comments.

2 Comments

You're a lifesaver. Thank you. You've given me a good idea as to what I've been doing wrong overall. I don't know if you'd know the answer to this too, but I take the cipherText and run .base64EncodedString() on it. I know the documentation says to pass in cipherText, so I was wondering if there was a way to take the base64EncodedString back to cipherText

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.