0

I have .pfx file using which I want to Sign the data in android in the same way in which my team has done in C#.

I am not finding a way for extracting Private key from .pfx file and sign using RSA with SHA256 in Android (Java).

Code for C#

private static byte[] EncryptUsingDigitalSignature(byte[] hashBytes)
        {
            try
            {
            X509Certificate2 cert = new X509Certificate2(AppDomain.CurrentDomain.BaseDirectory + "\\Certificate\\abc.pfx", "1234", X509KeyStorageFlags.Exportable);

                var cspParams = new CspParameters(24) { KeyContainerName = "XML_DSIG_RSA_KEY" };
                var key = new RSACryptoServiceProvider(cspParams);
                key.FromXmlString(cert.PrivateKey.ToXmlString(true));

                return key.SignHash(hashBytes, CryptoConfig.MapNameToOID("SHA256"));
            }
            catch(Exception ex)
            {
                Write_Log("GetDigitalSignature: " + ex.Message + " Path :" + AppDomain.CurrentDomain.BaseDirectory + @"\Certificate\abc.pfx");
                throw ex;
            }
        }

Thank you in advance.

  • [Does this answer your question?](https://stackoverflow.com/questions/51812671/instantiate-java-security-classes-privatekey-and-x509certificate-from-key-and) – ProgrammingLlama Aug 04 '21 at 07:29
  • Kindly note that Stackoverflow is not a free code converting service. – Michael Fehr Aug 04 '21 at 07:33
  • @Llama ,No I didn't found solution from that, I want to extract private key from .pfx file and encrypt using RSA. – Tejas Telawane Aug 04 '21 at 07:56
  • @MichaelFehr , Sorry If I drafted my question in wrong way, But I wanted to know is it possible to encrypt the same in android(Java) and if yes , please guide on same. – Tejas Telawane Aug 04 '21 at 08:01
  • Yes, it is possible. Search for "android java rsa signature example" and get e.g. "https://stackoverflow.com/questions/30929103/digital-signature-in-java-android-rsa-keys". Btw: I'm not sure if you know what you're talking on: Your C# code is for **signing** and **not for encryption**. – Michael Fehr Aug 04 '21 at 08:58

0 Answers0