Encrypt in java and Decrypt in IOS For AES

Actually trying to implement AES+RSA encryption for secure server communication.
I have a scenario , where I have a private key from the server and trying to decrypt the encrypted text and encrypted secret key received from java side to check to be able to decrypt the message and secret key properly.

encryptedText : 9pKpAq2HorRsG1IGX9+6Qg==
 
encryptedSecretKey : bjgZTpZD7DvrjTRXJ6LCA9GpkDnLJLgIVAmFWFXNUVqVWdjXOiOGeRHJx8owW6Pp/a5ECe8Cljlw
ZIAFhRUEPfTks22yntUOhrCUbDzzqYOMiQYfEV/h7/zahl4PV1t4rHXDsQPaf7Rrl8k5jKj0Cf7E
aKA4U/pyAjZlvVhMjH8=

Private Key :
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==

Can anyone please help how to use Private Key received to decrypt the secret key and text.

I am stuck as how to use the private key as it is in plain string format , how to convert it into private key to be used for decryption

I am stuck as how to use the private key as it is in plain string format

This private key is Base64 encoded. If you undo that encoding:

Code Block
% base64 -D > tmp.asn1
MIICdgIBADANBgkqhkiG9w0BAQEFAASCAmAwggJcAgEAAoGBAJitrKyHsfJtNHcr0dXvv0hV8iq0
HTbMYvxt3yKnYvksUINkOB+3sMHuUDdWZlgZ2POnWA+gP93gPogB0/547aR6f/WSBGDA0nWnE4uU
U2I1Eu08rQ==
^D


you’ll find that it’s actually ASN.1:

Code Block
% dumpasn1 -p -a tmp.asn1
SEQUENCE {
INTEGER 0
SEQUENCE {
OBJECT IDENTIFIER rsaEncryption (1 2 840 113549 1 1 1)
NULL
}
OCTET STRING, encapsulates {
SEQUENCE {
INTEGER 0
INTEGER
00 98 AD AC AC 87 B1 F2 6D 34 77 2B D1 D5 EF BF
48 55 F2 2A B4 FF 67 1E 7E 05 E5 81 58 38 1C 68
F5 67 9D 37 E9 9B 8A 09 93 8D C7 7A 45 27 D7 EC
28 CD A4 0E 8B C4 5A C5 00 DA 11 87 42 F6 86 7F
38 E4 B7 5F 69 E8 F9 8D F2 BE 7A 56 74 D7 0E BC
47 40 E2 D7 3C 01 37 4F B7 90 48 0F E0 EC 8A 9F
A9 3D 48 2E 41 97 FF CD 09 DA 3A 8D 44 62 61 0C
AB 19 DC 87 16 6B 54 F8 95 5B 2A 2C 0A 22 34 D3
CD
INTEGER 65537
}
}
}


Note I’m using the dumpasn1 (dub dub dub.cs.auckland.ac.nz/~pgut001/dumpasn1.c) tool here.

This is a PKCS#8 PrivateKeyInfo structure, where the header identifies the type of key (RSA in this case) and the body (everything encapsulated in the OCTET STRING) is the key itself. For an RSA key, this is an RSAPrivateKey, and that’s what iOS routines like SecKeyCreateWithData are expecting.

iOS has a good way to deal with the Base64 encoding (see init(base64Encoded:options:)) but there’s no API for removing the PKCS#8 header. Depending on what sort of keys you’re dealing with, you may need a full-bore ASN.1 parser to do this or you may be able to do something hacky.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"
Thank You so much for your quick and apt reply.

now, you mean i first have to try to remove header from this base64 string(private Key) i.e using ASN.1 parser some other way and then has to do base64 encoding to get the key.

I have tried following method to get the private key from this base64 string but it is crashing.

   var keyData : Data = Data(base64Encoded: privateKeyString)!

        let attributes: [String: Any] = [

                    kSecAttrKeyType as String: kSecAttrKeyTypeRSA,

                    kSecAttrKeyClass as String: kSecAttrKeyClassPrivate

                ]

        var error: Unmanaged<CFError>?

        let privKey : SecKey = SecKeyCreateWithData(base64 as CFData, attributes as CFDictionary, &error)!

Please if you can help in this regard or I am going right way or not.

Thanks!

I somehow managed to strip header from the privateKey and get privateKey now I am stuck at decrypting the Secret Key
as provided from server side as the process is to decrypt first secret key from private key and then encrypted text from secret key the encrypted text as mentioned above :

encryptedText : 9pKpAq2HorRsG1IGX9+6Qg==

encryptedSecretKey : bjgZTpZD7DvrjTRXJ6LCA9GpkDnLJLgIVAmFWFXNUVqVWdjXOiOGeRHJx8owW6Pp/a5ECe8Cljlw
ZIAFhRUEPfTks22yntUOhrCUbDzzqYOMiQYfEV/h7/zahl4PV1t4rHXDsQPaf7Rrl8k5jKj0Cf7E
aKA4U/pyAjZlvVhMjH8=

Any help is deeply appritiated.
Regards...

To decrypt any data using your private key you need to know what padding was used.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"
Thanks eskimo , can you pls help me decrypting this encrypted secret key in java , I am actually now stuck at this step..Please help!!

encryptedSecretKey : bjgZTpZD7DvrjTRXJ6LCA9GpkDnLJLgIVAmFWFXNUVqVWdjXOiOGeRHJx8owW6Pp/a5ECe8Cljlw
ZIAFhRUEPfTks22yntUOhrCUbDzzqYOMiQYfEV/h7/zahl4PV1t4rHXDsQPaf7Rrl8k5jKj0Cf7E
aKA4U/pyAjZlvVhMjH8=

Thanks a lot for your reply!!

can you pls help me decrypting this encrypted secret key in java

I still need to know what padding was used.

Share and Enjoy

Quinn “The Eskimo!” @ Developer Technical Support @ Apple
let myEmail = "eskimo" + "1" + "@apple.com"
Thanks eskimo for your replies..I get a great help from them.
Encrypt in java and Decrypt in IOS For AES
 
 
Q