decrypt short msg with RSA public key

Ios and swift SMEs, appreciate if I can get some answers on the following


Have very short msg encrypted by a pvt rsa key (pkcs1, in java)


Need to decrypt in ios using public key


How can I achieve that?


[Pls, 'am fairly familiar with cons and pros of using pub key to decrypt, for my use case pub key doesnt fly over internet; If I chose sig based, I need to do a time stamp etc check, which adds overhead; my msgs to encrypt are _very_ short << chars]


Thanks in anticipation

Replies

Note: I did not test it myself.


You say:

for my use case pub key doesnt fly over internet

So, how will the other part get the pu!blic key to decrypt the message encrypted with private key ?


There exists some public RSA library on github.

Did you look at this ?

https://stackoverflow.com/questions/56806553/rsa-encryption-function-in-swift-4-from-public-key-string

Yes, I am fiddling with RSAUtils.swift ;


https://github.com/btnguyen2k/swift-rsautils/blob/master/Swift-RSAUtils/RSAUtils.swift


Precisely after confirming the input params

this one throws -50 error, which I understand is parameter dislike by SecKeyDecyrpt


let status = SecKeyDecrypt(rsaKeyRef, padding, chunkData, idxEnd-idx, &decryptedDataBuffer, &decryptedDataLength)


rsaKeyRef corresponds to public key ; chunkData is enrcyptedData on bytes, idxEnd and idx are indices

padding is pkcs1


let dcr = decryptWithRSAKey(encryptedData, rsaKeyRef: keyRef!, padding: SecPadding.PKCS1) invokes the above func DecryptwithRSAKey


anyone has gotten this working? Many thanks for any pointers...

Need to decrypt in ios using public key

Apple’s crypto APIs do not support this. If you continue down this path — which is something I’d advise against — you’ll need to write (or acquire) your own code for it.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Oh, one more thing… stop using RSA (-:

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

Those two posts are helpful Eskimo1.


Now is there a boilerplate code on ECC swift for IOS that you can point me to?

Hopefully easy to use in my app and takes a few mins to integrate.


I would like to move forward before the swift language team changes the current version and 'need to upgarde the code 😉


Atleast now, I can put some questions to rest.

Now is there a boilerplate code on ECC swift for IOS that you can point me to?

It kinda depends on your specific requirements. A good place to start would be Apple CryptoKit. Specifically, Performing Common Cryptographic Operations shows how to use EC to generate a shared secret, then derive a symmetric key from that, then encrypt bulk data with that symmetric key.

If the CryptoKit approach doesn’t work for you, you can use the lower-level

SecKeyCreateEncryptedData
and
SecKeyCreateDecryptedData
APIs. The trick here is choosing the right algorithm. A good default choice would be
eciesEncryptionStandardVariableIVX963SHA256AESGCM
.

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"