Encrypt in UWP and Decrypt in Web API

Asked

Viewed 202 times

2

I’m trying to encrypt a string in a UWP (Universal Windows Platform) application and decrypt the string in a Web API application.

The problem is that I can’t find/modify an algorithm common to both platforms.

Currently I use the code below in the UWP project to encrypt

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);

But when I try to decrypt within the Web API project I get the error: Operation is not supported on this Platform

How to make an algorithm that works on both platforms?

1 answer

0

Solved.

Internalcrypto class

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);
  }
}

Crypto class

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);
  }
}

Source: Cryptography Shared Code

Browser other questions tagged

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