CryptoJS Encryption and Decryption in UWP C#

Andrew Kayaga 1 Reputation point
2020-04-18T08:25:00.563+00:00

I have a video URL link which is encrypted using CryptoJS on a server and my job in my Universal Windows App in c# is to decrypt the sent content and get the original video URL link.

I have tried to use AES Decryption but still getting errors. Below is the array that is generated when the URL is encrypted on the server

Passphrase: e615242cd9498f2a38995d516bb5f8c819be91a4

Encrypted String: {"ct":"yA8RGCPPvX4f9QhIJ0ga2oOutxnUEcddh5iBQalPoAlNZgutVTefOdvwO54D7Bwl2eTr+i/u9/cDL6sVcvZ9+hXfr1eZ/sYjjKX0qSr+yPjA+CrQWeApBPVtS45JPCY8ekB2pZah8uqqtPpfeV5T8xHzrkJJbS0d+VqFhj1aqao/qWKdCaD6BK75QfnkBz0XzQd/ulK7saoLGO2t7LHhlg==","iv":"0fa9a8205ccb9894e416e8ba85bc4960","s":"60f24ca1a8380cac"}

Below is the code that requires an key and Iv and what to decrpyt

    class AesEnDecryption
    {
        // Key with 256 and IV with 16 length
        private string AES_Key = "";
        private string AES_IV = "";
        private IBuffer m_iv = null;
        private CryptographicKey m_key;

        public AesEnDecryption()
        {


            IBuffer key = Convert.FromBase64String(AES_Key).AsBuffer();
            m_iv = Convert.FromBase64String(AES_IV).AsBuffer();
            SymmetricKeyAlgorithmProvider provider = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesCbcPkcs7);
            m_key = provider.CreateSymmetricKey(key);
        }

        public byte[] Encrypt(byte[] input)
        {

            IBuffer bufferMsg = CryptographicBuffer.ConvertStringToBinary(Encoding.ASCII.GetString(input), BinaryStringEncoding.Utf8);
            IBuffer bufferEncrypt = CryptographicEngine.Encrypt(m_key, bufferMsg, m_iv);
            return bufferEncrypt.ToArray();
        }

        public string Decrypt(byte[] input)
        {
            IBuffer bufferDecrypt = CryptographicEngine.Decrypt(m_key, input.AsBuffer(), m_iv);

            string one = Encoding.Unicode.GetString(bufferDecrypt.ToArray());
            return one;
        }

    }
Universal Windows Platform (UWP)
{count} votes

1 answer

Sort by: Most helpful
  1. Vahid Ghafarpour 17,700 Reputation points
    2023-08-26T05:37:22.5933333+00:00

    The key and IV you've provided in the code are empty strings (AES_Key and AES_IV). You should replace these with the actual key and IV values provided by the server.

    0 comments No comments