Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Windows store apps, Encryption,The supplied user buffer is not valid for the requested operation

I am trying to implement a simple Encryption of a string.

I am getting "The supplied user buffer is not valid for the requested operation" error. I dont know what is the problem in the implementation.

Below is the code snippet.

var keyHash = GetMD5Hash(key);

var toDecryptBuffer = CryptographicBuffer.ConvertStringToBinary(toEncrypt, BinaryStringEncoding.Utf8);

var aes = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesEcb);

var symetricKey = aes.CreateSymmetricKey(keyHash);

var buffEncrypted = CryptographicEngine.Encrypt(symetricKey, toDecryptBuffer, null);

`

like image 429
user2697095 Avatar asked Sep 16 '13 11:09

user2697095


1 Answers

The length of the data in toEncrypt must be a multiple of the block length of the algorithm unless you are using PKCS7 padding, which you currently aren't. You need to manually pad the data or use PKCS7 padding.

var aes = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.aesEcbPkcs7);
like image 132
Sani Singh Huttunen Avatar answered Nov 14 '22 23:11

Sani Singh Huttunen