0

I have a Windows Code from CCAvenue which works fine in Universal Windows Application but how to achieve the same in Xamarin.Forms?

public string EncryptRSA(string plainText, string publicKeyString) 
{
    IBuffer dataBuffer = CryptographicBuffer.ConvertStringToBinary(plainText, BinaryStringEncoding.Utf8);
    var asymmAlg = AsymmetricKeyAlgorithmProvider.OpenAlgorithm(AsymmetricAlgorithmNames.RsaPkcs1);
    var publicKey = asymmAlg.ImportPublicKey(CryptographicBuffer.DecodeFromBase64String(publicKeyString), CryptographicPublicKeyBlobType.X509SubjectPublicKeyInfo);
    var encryptedData = CryptographicEngine.Encrypt(publicKey, dataBuffer, null);
    return CryptographicBuffer.EncodeToBase64String(encryptedData);
}

If I copy paste the same code its giving this error:

Error

Martin Zikmund
  • 38,440
  • 7
  • 70
  • 91
Sampath R
  • 1
  • 3

1 Answers1

0

Unfortunately, you cannot use this UWP specific API on other platforms in Xamarin.Forms. I haven't found an alternative library, which would provide this functionality in cross-platform manner, so you will probably have to implement it using platform-specific APIs.

You can use Dependency Service and create an interface with your EncryptRSA method and then implement this interface in the platform heads in your solution. You already have the UWP implementation, this SO answer looks like a possible iOS solution and this is a possible Android implementation.

Martin Zikmund
  • 38,440
  • 7
  • 70
  • 91
  • I have used possible android implementation but its showing me error "Base.64 does not exists in the current context". Do I have to import Android.Util or what? – Sampath R Sep 20 '19 at 07:18
  • Can anyone tell me how to implement the above answer using .SO in Xamarin.Android – Sampath R Sep 26 '19 at 06:48