Encryption UWP web Api Rebex.Legacy.Cryptography

0 votes
asked Nov 8 by Rox (140 points)

Hello, i was trying to encrypt file(byte array) in UWP application with help of Rebex.Legacy.Cryptography class and decrypt byte array at the server side(web api) with help of System.Security.Cryptography. But every time when i try to decrypt array i receive an error "The input is not a complete block".

my code

AesCryptoServiceProvider aes;
    // Key with 256 and IV with 16 length
    private string AES_Key = "password";

singletone

private RabexEnc()
    {
        aes = new AesCryptoServiceProvider();
        aes.Mode = CipherMode.CBC;
        aes.Padding = PaddingMode.PKCS7;

        // generate symmetric key and initialization vector
        aes.GenerateIV();
        byte[] IV = aes.IV;
        //The Salt will be the first 8 bytes of the IV.
        byte[] theSalt = new byte[8];
        Array.Copy(IV, theSalt, 8);
        //A key for AES is generated by expanding the password using the following method.
        Rfc2898DeriveBytes keyGen = new Rfc2898DeriveBytes(AES_Key, theSalt);
        byte[] aesKey = keyGen.GetBytes(16);
        aes.Key = aesKey;
    }

public byte[] Encrypt_Aes(byte[] streamBytes)
    {
        // create an instance of memory-based stream to hold encrypted data
        var ms = new MemoryStream();

        // Create a decrytor to perform the stream transform.
        ICryptoTransform encryptor = aes.CreateEncryptor(aes.Key, aes.IV);
        // create an encryptor, create a CryptoStream and wrap a StreamWriter around it
        using (var cs = new CryptoStream(ms, aes.CreateEncryptor(), CryptoStreamMode.Write))
        {
            cs.Write(streamBytes, 0, streamBytes.Length);
        }

        return ms.ToArray();
    }

    public byte[] Decrypt_Aes(byte[] streamBytes)
    {
        // create an instance of memory-based stream to hold encrypted data
        var ms = new MemoryStream();

        using (MemoryStream msDecrypt = new MemoryStream(streamBytes))
        {
            // Create a decrytor to perform the stream transform.
            ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);

            using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
            {
                using (MemoryStream newstream = new MemoryStream())
                {
                    csDecrypt.CopyTo(newstream);
                    return newstream.ToArray();
                }
            }
        }
    }

Same code working at the server, but namespace at the server set to System.Security.Cryptography instead of Rebex.Legacy.Cryptography. Thanks for a reply.

1 Answer

0 votes
answered Nov 9 by stepantalalayev (2,640 points)

Hello,

seem like we are unable to reproduce the error. It works fine in our replication.

You can download our replication by the link below:
https://www.rebex.net/getfile/e9b07f2e945c4fbb94df03465371dd7b/RebexEncReproduction.zip

commented Nov 9 by Rox (140 points)
Hello, thank you for an answer. Sorry, maybe I did not make myself clear. I mean that for uwp this code with Rebex.Legacy.Cryptography works fine, but my point is to transport ecrypted byte array to the web api and decrypt it there. I'm using same code in web api, but instead of Rebex.Legacy.Cryptography i'm using System.Security.Cryptography, because , it is not possible to use Rebex.Legacy.Cryptography in web api. I proceeded from the fact that Rebex.Legacy.Cryptography it is wrapping above the Windows.Security.Cryptography and should have the same logic as System.Security.Cryptography. Is it possible at all to use such approach? If no, please, could you suggest me solution. Thank you.
asked Nov 10 by Rox (140 points) Encryption Web Api Rebex.Legacy.Cryptography
...