I wanted to decrypt a message using RSA Public Key in Swift - swift

I am new to Swift Language . I wanted a swift code for decrypting a message using RSA Public key.
Padding used is "PKCS1". (I am not talking about Signature , I want the code for decryption of message itself using Public Key) .
Thanks in Advance

I wanted a swift code for decrypting a message using RSA Public key.
What you are asking for is not possible.
With RSA public keys are used to encrypt messages, while the private key is used to decrypt encrypted messages. It's the other way around with signatures, and probably the reason why you are confused. Signatures are created using the private key and can be verified by public keys.

Related

I am having an error while decrypting with P256keys swift

I am using p256 keys to generate key pair and then SecKeyCreateEncryptedData to encrypt with public key and SecKeyCreateDecryptedData to decrypt with private key. The encryption was successful and able to transfer encrypted data in base64 format to other devices and then while decrypting truncatedASN1Field this error occurs. Please let me know how I can solve this.
This is the link to code snippet https://swiftfiddle.com/6nx5itskhfg5jbtywruoj64kpm
it works when I am using in the same PC but its not working when I am using different ipads to encrypt and decrypt.

Swift: RSA Encrypt a string with a specific private key

I need to write a method in Swift which uses a particular PRIVATE KEY to encrypt a timestamp using RSA. This is NOT used for authentication (rather it validates the client app to the server), I know that you would normally encrypt with a public key to ensure security.
I have a key:
-----BEGIN RSA PRIVATE KEY-----
MIICXAIBAAKBgQDIg+wteSjhalc1hSHEiUnz9X1pkrObCjaXMHqeSdfFQ/h5Q1Uh
...
o7wjoqFNxFnQMAYvkLzQZ7Y2jjfSJkaTVnhzJIZOfQ0=
-----END RSA PRIVATE KEY-----
And I need to encrypt a string using this particular key. I have written the Android application version of this app, but if I need to re-generate the keys specifically for iOS it's not a problem, but I would need this file for the Android version as well.
I have looked at the following web sites:
http://jslim.net/blog/2013/01/05/rsa-encryption-in-ios-and-decrypt-it-using-php/ Seems useful but it insists that the key be in a .der format - plus it's in Objective-C. Is it possible to generate a Key pair in both DER and PEM format, or convert between the two (using something like https://www.sslshopper.com/ssl-converter.html)?
https://github.com/henrinormak/Heimdall - but you cannot import a custom private key
https://github.com/ideawu/Objective-C-RSA/blob/master/RSA.m - seems long winded and is also in objective-c
One requirement is that all apps use the SAME key - it cannot be generated by each installation of the app. Also my Android app must be able to use the key as well (I am open to rethinking the Android version as leaving the key as a RAW file is not preferred for me).
Any help will be appreciated.
Other references checked:
How to encrypt a string with private key and decrypt with public key?
Using RSA public key to encrypt a string

IPhone Decryption with private key -Data Encrypted in Java

Can anyone help with the code how to decrypt with private key ,As in server side they are using OAEP encryption method .I tried decrypting using private key but the decrypted text is Null,I am getting the Error code as -9809 as decryption code result
When you say "with a private key" I assume you mean you're using SecKeyDecrypt() for asymmetric encryption rather than CommonCryptor for symmetric encryption.
SecKeyDecrypt() does not support OAEP. It only supports PKCS1 v1.5 padding (kSecPaddingPKCS1). It can also technically handle ASN.1 padding + PKCS1 padding, but this isn't usually relevant to decryption. You should have noticed this when you passed the SecPadding parameter. What did you pass?
That error number is errSSLCrypto which is a generic "something went wrong in crypto" message.

Cracking RSA without private key

I was wondering whether RSA can be cracked given:
The public key
A plaintext of a known cipher text.
In other words the decrypted message from an encrypted one (but without knowing the private key)
I.E.: you have managed to find the plaintext of an already encrypted message using the public key, however, you DO NOT know the private key.
No. That's called a known-plaintext attack, and there is no such attack known against RSA.

RSA iphone public key

I have a Public Key generated in JAVA.
I want to use this key and crypt the data using RSA and send it to the server.
How can I do that using the iPhone SDK?
Thanks
Unfortunately, iOS has no public APIs to deal with raw RSA keys.
There are two things you can do:
1) Instead of giving your app a Public Key, give your app a certificate instead. You can import the certificate with SecCertificateCreateWithData. Then create a trust with SecTrustCreateWithCertificates. Once you have the trust, you can extract the public key with SecTrustCopyPublicKey.
2) The other option is to include OpenSSL in your project. It has all the APIs you need, you can google for example code on how to work with RSA keys. This might be the simpler solution.
I have made available a script to easily build OpenSSL from source. You can grab it from:
http://github.com/st3fan/ios-openssl
If your public key is in modulus/exponent form, this question may help: Convert XML Dsig format to DER ASN.1 public key
I figured out how to binary-encode the modulus and exponent into the DER ASN.1 format that the SecKeyWrapper class of Apple's CryptoExercise project uses to import an external key.