0

I'm supposed to encrypt some data with a string public key sent from a server through HTTPS and send back to the server.

I did a lot of research and also studied these topics plus a lot of other things related to it but couldn't solve my problem.

All I'm saying is that I cannot convert a string to a seckey to use this function.

func SecKeyEncrypt(_ key: SecKey, 
             _ padding: SecPadding, 
             _ plainText: UnsafePointer<UInt8>, 
             _ plainTextLen: Int, 
             _ cipherText: UnsafeMutablePointer<UInt8>, 
             _ cipherTextLen: UnsafeMutablePointer<Int>) -> OSStatus

1-how to encrypt and decrypt a String(Plain Text) with RSA public key in ios, swift

2-https://developer.apple.com/documentation/security/certificate_key_and_trust_services/keys/using_keys_for_encryption

This is MyCode for encryption:

let publickey : SecKey = "# -----BEGIN PUBLIC KEY-----  some key  ---- 
END PUBLIC KEY----- #" as! SecKey

let message = "plain text"
let blockSize = SecKeyGetBlockSize(publicKey!)
var messageEncrypted = [UInt8](repeating: 0, count: blockSize)
var messageEncryptedSize = blockSize

var status: OSStatus!
        status = SecKeyEncrypt(publickey!, SecPadding(rawValue: 0), message, 
message.characters.count, &messageEncrypted, &messageEncryptedSize)

app get crashed at the first line.

2 Answers2

0

You can't say

let publickey : SecKey = 
    "# -----BEGIN PUBLIC KEY-----  some key  ----END PUBLIC KEY----- #" as! SecKey

You cannot just take a String and somehow magically cast it as a SecKey. You have to derive the SecKey from the public key.

matt
  • 515,959
  • 87
  • 875
  • 1,141
  • See the link you yourself cited, as well as https://stackoverflow.com/questions/28808101/seckey-from-public-key-string-from-server-in-swift and others. – matt May 24 '18 at 22:16
  • I know it doesn't work but i didn't know what to do,please tell me a little about "derive the seckey from the public key" I started ios programming recently and trying hard to learn some of the concepts..forgive me – hossein May 24 '18 at 22:33
  • You’ve two links with code already. I’m just explaining the crash! :) It’s odd that Swift doesn’t warn that the cast can’t work but that’s another story. – matt May 24 '18 at 22:45
  • CoreFoundation types all are, deep-down, `void*`, so they always succeed `as!` casting. `let x = "" as! CFArray` works, too. You should only ever use `as` casting (never `as!` casting) on CFTypes unless you really really really know what you're doing. – Rob Napier May 25 '18 at 00:22
  • 1
    @RobNapier Thanks, I knew it was something like that, but I feel Swift should be smarter about this. It supposedly knows what CFRef types are toll-free bridged to which object types and which are not. – matt May 25 '18 at 00:46
0

You're 90% there with that first link. The important piece is this:

let data2 = Data.init(base64Encoded: serverPublicKey)

It expects the input to be Base64 encoded, which your data is, except you also have a that # -----BEGIN PUBLIC KEY----- prefix (and the suffix). You need to pull those off before trying to base64-decode the data. Make sure that data2 has legitimate data in it.

Try using String.dropFirst(Int) and String.dropLast(Int), and you should be able to trim your input into something that is just Base64. (You may also need to run it through filter { $0 != "\n" }.)

That should get you where you need to go. Let me know if you're still stuck; I've got code lying around somewhere that does it, but I don't have it on hand.

Rob Napier
  • 286,113
  • 34
  • 456
  • 610