Thanks OOPer , sorry my mistake , Correct code
Code Block | let attributesPublic: [String: Any] = [ |
| kSecAttrKeyType as String: kSecAttrKeyTypeRSA, |
| kSecAttrKeyClass as String: kSecAttrKeyClassPublic |
| ] |
| var errorPublic: Unmanaged<CFError>? |
| let publicSecKey : SecKey = SecKeyCreateWithData(serverPublicKeyData as CFData, attributesPublic as CFDictionary, &error)! |
I am now able to get the Public Key through this , Thanks,
Step 4 : Encrypting secret key with this private Key :
Code Block | var errorSecPublic: Unmanaged<CFError>? |
| let SecKeyEncryptedData = SecKeyCreateEncryptedData(publicSecKey, |
| algorithm, |
| secretKeyDataEncryption as CFData, |
| &errorSecPublic) as Data? |
| guard SecKeyEncryptedData != nil else { |
| print("can not encrypt") |
| return |
| } |
|
| print(SecKeyEncryptedData! as NSData) |
| let secKeyEncoded = SecKeyEncryptedData?.base64EncodedString() |
| print(secKeyEncoded) |
Step 4 is also went well ... able to get encrypted secret kay , I think going right way , But issue is
when I am trying to extract the encrypted text , using same steps we did yesterday , but it is failing at retrieving the encrypted Secret Key i.e Step 3 of yesterday's decrypting code process , may be have not encrypted secret key properly.
i.e in following code
Code Block | let algorithm: SecKeyAlgorithm = .rsaEncryptionOAEPSHA1 |
| guard SecKeyIsAlgorithmSupported(privateSecKey, .decrypt, algorithm) else { |
| print("can not decrypt") |
| return |
| } |
|
| var errorSec: Unmanaged<CFError>? |
| let SecKeyDecryptedData = SecKeyCreateDecryptedData(privateSecKey, |
| algorithm, |
| SecretKeyData as CFData, |
| &errorSec) as Data? |
|
| guard SecKeyDecryptedData != nil else { |
| print("can not decrypt") /* failing in this */ |
| return |
| } |
| print(SecKeyDecryptedData! as NSData) |
AS, private sec key is the same as retrieved yesterday have not changed , only encrypted secret key have changed as of generated new one after encryption today.
Please help!!!
My side encrypted text generated is : "xLJSdTqGj/ViCgwMATPMy1hvusHU3pCPpTIo7DlGfjs="
and encrypted Secret key is :
"U9j90pWZHS9TGdEtJmLMThKjjKobcXh9M8Op69kZHdHa8U4GSpRoIw1PRSnms3WdntvreLVMRv8zzSkodg3CnHyRha1tW6PCYQfxTtj5fFpMNZG8K7mWCruIvXm3gWQHJIuUcnqE6LHke6vBzUdFY/i2pWQmHPQIbhcgwPEQoyM="