0

how use a .key file private key in Xamarin iOS?

I try import with SecKey.Create but not work. Dont want save in keychain, only make a signature with a private key .key in base64

I try this code in Xamarin Android and work fine. How replicate that in Xamarin.iOS using SecKey class.

PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(Convert.FromBase64String(key));
                KeyFactory kf = KeyFactory.GetInstance("RSA");
                var privateKey = kf.GeneratePrivate(spec);

                X509EncodedKeySpec spec1 = new X509EncodedKeySpec(Convert.FromBase64String(pub));
                KeyFactory kf1 = KeyFactory.GetInstance("RSA");
                var pubKey = kf1.GeneratePublic(spec1);

                var hash = Convert.FromBase64String(challenge);
                var providers = Security.GetProviders();
                Signature s = Signature.GetInstance("SHA256withRSA/PSS");

                s.InitSign(privateKey);
                s.Update(hash);
                byte[] signature = s.Sign();
  • Can you debug the code on which line crashes or which line of code exactly doesn't work as expected? BTW, there are similar threads [use a PKCS8 RSA DER Private Key in iOS?](https://stackoverflow.com/questions/12311840/how-can-i-use-a-pkcs8-rsa-der-private-key-in-ios) and [Encrypt RSA/ECB/OAEPWithSHA-256AndMGF1Padding Swift](https://stackoverflow.com/questions/47237524/encrypt-rsa-ecb-oaepwithsha-256andmgf1padding-swift), you can refer to it. – Alexandar May - MSFT Jun 01 '23 at 06:28

0 Answers0