user2697095
user2697095

Reputation: 93

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

`

Upvotes: 5

Views: 3110

Answers (1)

Sani Huttunen
Sani Huttunen

Reputation: 24385

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

Upvotes: 6

Related Questions