For this first trying to implement a demo where I am trying to decrypt the RSA private key , secret key and encrypted message i have got from Java end.
The private key i got is : MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJitrKyHsfJtNHcr0dXvv0hV8iq0/2cefgXlgVg4HGj1Z5036ZuKCZONx3pFJ9fsKM2kDovEWsUA2hGHQvaGfzjkt19p6PmN8r56VnTX
DrxHQOLXPAE3T7eQSA/g7IqfqT1ILkGX/80J2jqNRGJhDKsZ3IcWa1T4lVsqLAoiNNPNAgMBAAEC
gYAKYFNJ7nbziOR17O534bFYUy8AJAjvkyzxbaWav0V/BJ6kGravsXPxKUOTVbvdetlTEIFEknWw
ydwIMO8mHgHrWtprRI7COkV1l46UgObgXTQp/Nwfqukki2TenbbAo/Ja72+CMycOy3IzvW2+Qd0Z
wFHheUmLE4lF7fHssSMJwQJBAOAc8ph+hR64jvJIydjYzoZUm7K0HMnPZQA/bDpRJ6CeR8EPZ7/x
IXQxaWTN5RzNEb/RY96KkyZ0xWDpAmiBb8UCQQCuZs3lpWFodos0lnVW/WoDVdng7qE8Xqwx68kX
hueclgBYbJR9SYd14AqKbrDJvt+8dtw5qKOkIEVdNuxrqcxpAkEAmd+mPUeZFNe45edOF0H8wsRy
xobdwT5RZZMmNwAjiidCsu5l2Kaxxnpql5i6d0Thq+cTf+d7UwsXvgsd6Sz91QJABGYZaWJre4wJ
5NCqsv//TYg7z52VOYWVyEiPMOW5L8zkw1YxxJs3LHTzLxytntkOoZ1J3rZvMjOSLFC3U9vbiQJA
HTbMYvxt3yKnYvksUINkOB+3sMHuUDdWZlgZ2POnWA+gP93gPogB0/547aR6f/WSBGDA0nWnE4uU
U2I1Eu08rQ==
encryptedSecretKey : bjgZTpZD7DvrjTRXJ6LCA9GpkDnLJLgIVAmFWFXNUVqVWdjXOiOGeRHJx8owW6Pp/a5ECe8Cljlw
ZIAFhRUEPfTks22yntUOhrCUbDzzqYOMiQYfEV/h7/zahl4PV1t4rHXDsQPaf7Rrl8k5jKj0Cf7E
aKA4U/pyAjZlvVhMjH8=
encryptedText : 9pKpAq2HorRsG1IGX9+6Qg==
This is what I have got from server end.
I am now stuck to decrypt the encrypted Secret key.
Java Code for creating secret key is
Code Block public class ClientEncrypt { // private static String publicKeyString = "<your_public_key_here>"; // private static String publicKeyString = "MIICdQIBADANBgkqhkiG9w0BAQEFAASCAl8wggJbAgEAAoGBAJwdPPeUl9Jknt6gcB98xcsUGlp1ISopEfHL4WFPvYB04UKf9aJOzUpAynl0ZNgI2wBejL6+Ap6pGkqmn2AamEdg24G0G8u//0nDIQYdtDpwoxmADEG2UaEz4+DxFTjH2JICD2WQRx2bjd3h6GN/BimJRVYhs8CLtvhr0f6iJZ9VAgMBAAECgYAu8sgL+L7zwdjZQMyXW5GsMf5+56yynilMWzELb8ygngXEYJChYCtywith/TP6mCcsZELVdhUZm+dRPey+HSFgFa3XDQV+IQZG2f3RK/L78cUFnzXjIwa8INHI7dkfZVBBW6fY1pdzjcEVl0lVAkeLvMowb00wMMlrFSVGnFBBYQJBAMg3anp33/GxuUTV8al6UwEVYwXuOCV5R+vc1M0MW4fEXceFX+GUPL4yF/k6h/AT/Qx90jS6BpUs4OfE1L4/KGkCQQDHnDJptJwp5WTzZLErUSSKBotWar7uo+kCpDer8BCrBZUqqff8f8B3rCGohwrvLJkY1H0I/2VuV4pYGuIXqcINAkBAu5IEmJ9iIIj7FYT1u6P8lLKYZ0Xl78luG879oMIzKiz75kZyw5/ECqQvo1nyKDRHhqKhA4g/UOxCYyQ5gmgJAkAeYzgFgZpYgnSY3RZfqs2uOYrS0BNbDxb08P0MrtJKxuHLie6XwPcTAOBq4IwYlyiDcC3MxdyyLJ+CBC2xPoWtAkAOB7PvjFeFcL5JZe/MpM0NW+CkKOAu8Xs4L9qihnMlZE6zZDwbbDFONyxJeDlcRmGlvlCPPxL1blZGgVeQW/M1"; private static String publicKeyString = "MIGfMA0GCSqGSIb3DQEBAQUAA4GNADCBiQKBgQDFzfAPe5h40OHut+gT5JEm5MEqw4SVbsWKdusLXPawqBNPPn6TpWhbHgFJfe3HsqpmpdXBslSWM7T/8Pu9+XzKiPoGTzG91a59kl2kMYNQE0GeIJmzHrqPSiw3QG64fU10mzR/Ys9Yh2FyTcbaekDSUqkL7KpDTPV35eAWs6vHmQIDAQAB"; public static void main(String args[]) { try { // 1. generate secret key using AES KeyGenerator keyGenerator = KeyGenerator.getInstance("AES"); keyGenerator.init(128); // AES is currently available in three key sizes: 128, 192 and 256 bits.The // design and strength of all key lengths of the AES algorithm are sufficient to // protect classified information up to the SECRET level SecretKey secretKey = keyGenerator.generateKey(); // 2. get string which needs to be encrypted String text = "<your_string_which_needs_to_be_encrypted_here>"; // 3. encrypt string using secret key byte[] raw = secretKey.getEncoded(); SecretKeySpec skeySpec = new SecretKeySpec(raw, "AES"); Cipher cipher = Cipher.getInstance("AES/CBC/PKCS5Padding"); cipher.init(Cipher.ENCRYPT_MODE, skeySpec, new IvParameterSpec(new byte[16])); String cipherTextString = Base64.encodeToString(cipher.doFinal(text.getBytes(Charset.forName("UTF-8"))), Base64.DEFAULT); System.out.println("cipherTextString : "+cipherTextString); // 4. get public key X509EncodedKeySpec publicSpec = new X509EncodedKeySpec(Base64.decode(publicKeyString, Base64.DEFAULT)); KeyFactory keyFactory = KeyFactory.getInstance("RSA"); PublicKey publicKey = keyFactory.generatePublic(publicSpec); // 6. encrypt secret key using public key Cipher cipher2 = Cipher.getInstance("RSA/ECB/OAEPWithSHA1AndMGF1Padding"); cipher2.init(Cipher.ENCRYPT_MODE, publicKey); String encryptedSecretKey = Base64.encodeToString(cipher2.doFinal(secretKey.getEncoded()), Base64.DEFAULT); //System.out.println(encryptedSecretKey); System.out.println("encryptedSecretKey : "+encryptedSecretKey); // 7. pass cipherTextString (encypted sensitive data) and encryptedSecretKey to // your server via your preferred way. // Tips: // You may use JSON to combine both the strings under 1 object. // You may use a volley call to send this data to your server. } catch (NoSuchAlgorithmException | InvalidKeyException | NoSuchPaddingException | IllegalBlockSizeException | BadPaddingException | InvalidKeySpecException | InvalidAlgorithmParameterException e) { e.printStackTrace(); } } }
Please help in this regard!!
All the keys and the encrypted text you have shown are Base-64 encoded.
You need to decode them to binary data. In Swift, they are represented by type Data.
You know how to convert Base-64 encoded String to Data, as shown in other threads of yours.
Keep all the Data as Data, do not try to convert them to String. Are you OK with this?
If OK, then step 1 cleared.
Now you have 3 Data,
privateKeyData (634 bytes)
encryptedSecretKeyData (128 bytes)
encryptedTextData (16 bytes)
The second thing you need to do is to retrieve the right private key content to pass to SecKeyCreateWithData as suggested by @eskimo in another thread of yours.
You once said in the same thread:
But you have never shown how have you done it, nor never shown the actual privateKey.I somehow managed to strip header from the privateKey and get privateKey
Thus, your step 2 is not cleared yet.
Please show how have you got the privateKey. Is it really the right data to use with Apple's frameworks?