2

Estou tentando criptografar uma string em uma aplicação UWP (Universal Windows Platform) e descriptografar a string em uma aplicação Web API.

O problema é que não consigo encontrar/modificar um algoritmo comum às duas plataformas.

Atualmente uso o código abaixo no projeto UWP para criptografar

SymmetricKeyAlgorithmProvider aesCbcPkcs7 = SymmetricKeyAlgorithmProvider
                   .OpenAlgorithm(SymmetricAlgorithmNames.AesCbcPkcs7);
IBuffer keymaterial = CryptographicBuffer.ConvertStringToBinary(CHAVE, 
                                    BinaryStringEncoding.Utf8);
CryptographicKey k = aesCbcPkcs7.CreateSymmetricKey(keymaterial);
byte[] value = Encoding.UTF8.GetBytes("STRING"); 
IBuffer buff = CryptographicEngine.Encrypt(k, 
               CryptographicBuffer.CreateFromByteArray(value), 
               keymaterial);
 return CryptographicBuffer.EncodeToBase64String(buff);

Mas quando tento descriptografar dentro do projeto Web API recebo o erro: Operation is not supported on this platform

Como fazer um algoritmo que funcione nas duas plataformas?

1 Resposta 1

0

Resolvido.

Classe InternalCrypto

internal static class InternalCrypto
{
  private static readonly string _passwordEncryption = "12345";
  private static readonly int _iterations = 1000;
  private static readonly int _keyLenght = 32;
  private static readonly byte[] _salt = 
                       Encoding.UTF8.GetBytes("123456789");
  private static readonly byte[] _derivedKey =
          NetFxCrypto.DeriveBytes.GetBytes(_passwordEncryption, _salt, _iterations, _keyLenght);

  internal static byte[] Encrypt(string text)
  {
    ISymmetricKeyAlgorithmProvider aesCrypto = WinRTCrypto.SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithm.AesCbcPkcs7);
    ICryptographicKey symmtricKey = aesCrypto.CreateSymmetricKey(_derivedKey);
    ICryptoTransform encryptor = WinRTCrypto.CryptographicEngine.CreateEncryptor(symmtricKey);
    byte[] byteText = Encoding.UTF8.GetBytes(text);
    byte[] encryptedText = encryptor.TransformFinalBlock(byteText, 0, byteText.Length);
    return encryptedText;
  }

  internal static string Decrypt(byte[] encryptedText)
  {
    ISymmetricKeyAlgorithmProvider aesCrypto = WinRTCrypto.SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithm.AesCbcPkcs7);
    ICryptographicKey symmtricKey = aesCrypto.CreateSymmetricKey(_derivedKey);
    ICryptoTransform decryptor = WinRTCrypto.CryptographicEngine.CreateDecryptor(symmtricKey);
    byte[] decryptedText = decryptor.TransformFinalBlock(encryptedText, 0, encryptedText.Length);
    return Encoding.UTF8.GetString(decryptedText, 0, decryptedText.Length);
  }
}

Classe Crypto

public static class Crypto
{
  public static string EncryptToText(string text)
  {
    byte[] encryptedText = InternalCrypto.Encrypt(text);
    return Convert.ToBase64String(encryptedText);
  }

  public static byte[] EncryptToBytes(string text)
  {
    return InternalCrypto.Encrypt(text);
  }

  public static string DecryptFromText(string encryptedText)
  {
    return InternalCrypto.Decrypt(Encoding.UTF8.GetBytes(encryptedText));
  }

  public static string DecryptFromBytes(byte[] encryptedText)
  {
    return InternalCrypto.Decrypt(encryptedText);
  }

  public static void Teste()
  {
    byte[] bytes = EncryptToBytes("Teste");
    string text = DecryptFromBytes(bytes);
  }
}

Fonte: Cryptography Shared Code

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .