Compatibility Android and Windows Phone Encryption

1

I have a C # application that generates information in Windows Desktop and sends files to Android application.

I send the encrypted information with the functions below, but I'm developing the same Android application for Windows Phone and the Encrypt and Decrypt function does not work on Windows Phone because using System.Security.Cryptography is not recognized.

I found references to the use of using Windows.Security.Cryptography and using Windows.Security.Cryptography.Core , but I was not successful.

Someone has already experienced this and can you give me a suggestion that these functions or equivalents can also be used in Windows Phone?

public static string Criptografar(string chave, string Message)
{
    byte[] Results;
    System.Text.UTF8Encoding UTF8                = new System.Text.UTF8Encoding();
    MD5CryptoServiceProvider HashProvider        = new MD5CryptoServiceProvider();
    byte[] TDESKey                               = HashProvider.ComputeHash(UTF8.GetBytes(chave));
    TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider();
    TDESAlgorithm.Key                            = TDESKey;
    TDESAlgorithm.Mode                           = CipherMode.ECB;
    TDESAlgorithm.Padding                        = PaddingMode.PKCS7;
    byte[] DataToEncrypt                         = UTF8.GetBytes(Message);
    try
    {
        ICryptoTransform Encryptor = TDESAlgorithm.CreateEncryptor();
        Results                    = Encryptor.TransformFinalBlock(DataToEncrypt, 0, DataToEncrypt.Length);
    }
    finally
    {
        TDESAlgorithm.Clear();
        HashProvider.Clear();
    }
    return Convert.ToBase64String(Results);
}

public static string Descriptografar(string chave, string Message)
{
    byte[] Results;
    System.Text.UTF8Encoding UTF8                = new System.Text.UTF8Encoding();
    MD5CryptoServiceProvider HashProvider        = new MD5CryptoServiceProvider();
    byte[] TDESKey                               = HashProvider.ComputeHash(UTF8.GetBytes(chave));
    TripleDESCryptoServiceProvider TDESAlgorithm = new TripleDESCryptoServiceProvider();
    TDESAlgorithm.Key                            = TDESKey;
    TDESAlgorithm.Mode                           = CipherMode.ECB;
    TDESAlgorithm.Padding                        = PaddingMode.PKCS7;
    byte[] DataToDecrypt                         = Convert.FromBase64String(Message);
    try
    {
        ICryptoTransform Decryptor = TDESAlgorithm.CreateDecryptor();
        Results                    = Decryptor.TransformFinalBlock(DataToDecrypt, 0, DataToDecrypt.Length);
    }
    finally
    {
        TDESAlgorithm.Clear();
        HashProvider.Clear();
    }
    return UTF8.GetString(Results);
}
    
asked by anonymous 07.05.2017 / 21:09

0 answers