Generate SecKeyRef from base64 RSA public key - ios

I have looked at other answers on StackOverflow and I didn't find what I was looking for.
I have a public RSA key in Base64 format generated by some other system.
It is in x509 format and the encryption is 512 bits strong.
I need to create a SecKeyRef in order to use an encrypt some data and send back home. (for all purposes the public key is sent from the server side)
I can't find anything on the web that helps me in this situation, all other examples and answers by-pass the problem. I can't create a PEM or DER files or use any other 'tricks', Base64 -> SecKeyRef nothing less.
I know this is a hard one so thank you all for helping.
p.s this is for a framework and not an app.

I have found this amazing GitHub project which helped me a lot!
https://github.com/ideawu/Objective-C-RSA
I hope this helps!

Related

Can I decrypt data which is encrypted by Keyczar using Google Tink?

I have been using Google Keyczar for encrypting data in my JAVA app. And I want to change the crypto solution to Google Tink.
But the problem is the already encrypted data by Keyczar. Can I decrypt them by Tink?
If yes, I want to change the crypto solution from Keyczar to Tink. If no, I have to think about another solution.
Thank you.
I did it.
Keyczar is using AES. So I use TinyAES.
Keyczar is also using HMAC. So I use HMAC of avr-crypto-lib.
Just one thing is I have to extract the key from Keyczar key.

Is it possible to parse a X509 certificate with Alamofire?

I need to parse a X509 certificate. Specifically I need to find the Policy ID #1 (e.g. when I have a look at the certificate of https://www.apple.com I find somewhere in the Extensions an entry Policy ID #1 (2.16.840...). I need to extract this number.
Until now I tried to use openSSL to do the job, but since I am not really familiar with ObjC or C and the documentation seems to be sparse I failed miserably.
My question now: Is it possible to get this information with the help of Alamofire in Swift? If yes I would be grateful for any tips on how to do this.
If anyone knows how to extract this information in ObjC with openSSL I'm also thankful for any tips.

How to generate Public key from Public key bytes using RSA in iOS?

I want to create Public key from Public key bytes using RSA and x.509 certificate. After that I want to encrypt data with public key, which will decrypt by java program on server side using private key.
I have tried many libraries to get the solution but I got nil output every-time after encryption.
Any help would be greatly appreciated. Thanks!
if you generate key in you ios app, it need to some modification before import outside (non-ios) otherwise it will not work and also if you want to use outsider key in your ios app it also need reduce some byte from key, you can find it on web, for import and export Rsa key I use swiftyRSA ,
https://github.com/TakeScoop/SwiftyRSA
It work fine for me.

image encrypt/decrypt between php and Android

I want to encrypt an image using PHP and decrypt it in an Android app. I found someone suggest to use MCrypt. However, I noticed that ImageMagick, which I use to convert pdf into jpg, seemed to have ability for encryption. Can I use ImageMagick to encrypt the jpg at the server side and decrypt it using JAVA? Thanks very much.
As per documentation
"ImageMagick only scrambles the image pixels. The image metadata remains untouched and readable by anyone with access to the image file.
ImageMagick uses the AES cipher in Counter mode. We use the the first half of your passphrase to derive the nonce. The second half is the cipher key."
To decrypt the image on the client side, you would have to keep the image header as is and decrypt the remainder of the file using the password with which it was encrypted with. That will require custom coding with knowledge of the image format internals. You will also have to find out how the nonce is derived from the passphrase.
You can alternatively use a SSL connection between the client and server or use any cryptographic scheme available in both PHP and Java either with symetric key or public key encryption as per your requirements.

Is it safe to write RSA private key in my code

I have an important file saving in my app's document directory. I don't allow other to view its content so I encrypt the file with auto-generated AES key and encrypt this AES key with RSA public key, then save the encrypted AES key to NSUserDefaults.
When using the important file, I will fetch the encrypted AES key and decrypt the AES key with RSA public key, then decrypt the file.
But I don't know where to store the RSA private key, is it safe to write it in my code like NSString *rsaPrivateKey = #"%^^&*(())";, if not, is there a safer solution ?
I know how to generate RSA public and private key using openssl (This link helps me)
Edit:
The important file is only open to the user himself, you can consider it as a photo with some sensitive content and the photo is saved by the user.
In principle you are trying to solve the DRM problem. As long as you cannot trust the runtime system, there is no complete solution. Your best bet is to store keys in system provided containers and hope those are well protected (i.e. the iOS key chain as borrrden already mentioned).
If that fails you can encrypt your RSA key with an AES key generated from some static information within the device. This should be thought of as obfuscation rather than encryption as the key is not really secret. You should probably try and use a standard container format such as PKCS#8 to encrypt the keys.
It is possible to generate RSA keys from a static set of data (using a PRNG to feed this data in the RSA key pair generator) but those kind of schemes are not standardized, very brittle and take an unspecified amount of CPU time, so I would strongly recommend not to take that route.

Resources