0

I have spent several hours trying to have a valid Encrypt/Decrypt function that uses Asymmetric encryption, and the best option seems to be RSA. But The thing is that I want to be able to provide my own public/private key to the function as a string myself in the form : "769de1f1a9dd6e114f81b9490ea42a2967840353edd358a35c84e2c831dd40a2" something very very similar to the 'eth-crypto' npm library for javascript.

but I haven't found any article or documentation that explains that. so has anyone implemented it before or have an article that explains it. keep in mind that when using asp.net core the FromXmlString doesn't work even when using 3.0

Here is my Encrypt function so far :

public static string EncryptAsymmetric(string textToEncrypt, string publicKeyString)
        {


            var bytesToEncrypt = Encoding.UTF8.GetBytes(textToEncrypt);

            using (var rsa = new RSACryptoServiceProvider(2048))
            {
                try
                {

                    rsa.FromXmlString(publicKeyString);
                    var encryptedData = rsa.Encrypt(bytesToEncrypt, true);
                    var base64Encrypted = Convert.ToBase64String(encryptedData);
                    return base64Encrypted;
                }
                finally
                {
                    rsa.PersistKeyInCsp = false;
                }
            }
        }

but it throws an error on the FromXmlString ( operation not supported on this platform ) so if there is any other way maybe......

any help is very appreciated because I have been looking into it for many hours with no result.

MTG
  • 41
  • 1
  • 6
  • Where did you get that key string from? Given its length and format I doubt it's a valid RSA key. – John Wu Feb 20 '20 at 07:33
  • the use will provide his private key and the public key will be available at the database. it is a blockchain public/private key by the way – MTG Feb 20 '20 at 07:34
  • FromXMLString only works on a key defines which this kind of schema: ….

    ….

    …. …. ….
    Use this to import a public key into the cryptoprovider. https://stackoverflow.com/questions/41808094/correctly-create-rsacryptoserviceprovider-from-public-key
    – Colin Smith Feb 20 '20 at 07:35
  • The string you are using only has 64 hex digits, which provides 256 bits of entropy. There is no way it is a 2048-bit RSA key. Also, an RSA key is actually a data structure containing several different values. – John Wu Feb 20 '20 at 07:37
  • I tried this for a public key to see whether the problem is from the format or the function: const string public_key = "uznzVPilsR1rWPkpq6m6IALaafDnVZTDDcnEyBD3A/PBx2JZTKM0DTgiTDDwCEmQBNBpPILcIBdtg3aSUgicair+2ksYrVFT+uiy0Zy1nU6qoJ+SsapLKrpCa1zHpV4LMO/pFo4Foqzw0C1FNe56FXo1xj77GPgeYl0MHUVtAUc=AQAB"; but it still gave me the "Operation is not supported on this platform" error – MTG Feb 20 '20 at 07:38
  • Interesting that you get a PlatformNotSupportedException. The docs say that will be thrown on .NET Core, but the function in .NET Core itself seems to be fully implemented... – yaakov Feb 20 '20 at 07:59
  • @yaakov I even tried to use extension methods to get rid of this error but unfourtunatly it didnt work – MTG Feb 20 '20 at 08:09
  • As a workaround you could use something like OpenSSL to create a certificate using the key you already have, then use the certificate with `RSACryptoServiceProvider`. – John Wu Feb 20 '20 at 08:45

0 Answers0