Compatibility Android and Windows Phone encryption

Asked

Viewed 42 times

1

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

I send the encrypted information with the functions below, but I’m developing the same Android app for Windows Phone and the Encrypt and Decrypt function don’t 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 unsuccessful.

Someone has already been through this and can give me a suggestion so that these or equivalent functions can also be used on 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);
}
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.