System.Security.Cryptography vs PCLCrypto

We are in the process of using a large number of common functions in our system and porting them to PCL libraries. I have a problem using PCLCrypto. I take some existing data in our database and try to decrypt it using the same algorithm. I am returning a value, but at the end there are 16 extra bytes that are just garbage.

See the following code: Old algorithm using System.Security.Cryptography

public static string SymmetricEncrypt(this string plaintext, string key, SymmetricAlgorithm algorithm) { byte[] keyBuffer = Convert.FromBase64String(key.Hash(HashAlgorithm.MD5)); byte[] plainTextBuffer = Encoding.UTF8.GetBytes(plaintext); var symmetricAlgorithm = new AesCryptoServiceProvider(); symmetricAlgorithm.Key = keyBuffer; symmetricAlgorithm.Mode = CipherMode.ECB; var encryptor = symmetricAlgorithm.CreateEncryptor(); byte[] cipherBuffer = encryptor.TransformFinalBlock(plainTextBuffer, 0, plainTextBuffer.Length); symmetricAlgorithm.Clear(); return Convert.ToBase64String(cipherBuffer); } public static string SymmetricDecrypt(this string cipherText, string key, SymmetricAlgorithm algorithm) { byte[] keyBuffer = Convert.FromBase64String(key.Hash(HashAlgorithm.MD5)); byte[] cipherTextBuffer = Convert.FromBase64String(cipherText); var symmetricAlgorithm = new AesCryptoServiceProvider(); symmetricAlgorithm.Key = keyBuffer; symmetricAlgorithm.Mode = CipherMode.ECB; var decryptor = symmetricAlgorithm.CreateDecryptor(); byte[] plainTextBuffer = decryptor.TransformFinalBlock(cipherTextBuffer, 0, cipherTextBuffer.Length); symmetricAlgorithm.Clear(); return Encoding.Default.GetString(plainTextBuffer); } 

Decryption using PCLCrypto

 public static string SymmetricDecrypt(this string cipherText, string key, SymmetricAlgorithm algorithm) { byte[] keyBuffer = Convert.FromBase64String(key.Hash(HashAlgorithm.MD5)); byte[] cipherTextBuffer = Convert.FromBase64String(cipherText); ISymmetricKeyAlgorithmProvider symmetricAlgorithm = WinRTCrypto.SymmetricKeyAlgorithmProvider.OpenAlgorithm(PCLCrypto.SymmetricAlgorithm.AesEcb); var symmetricKey = symmetricAlgorithm.CreateSymmetricKey(keyBuffer); var decryptor = WinRTCrypto.CryptographicEngine.CreateDecryptor(symmetricKey); byte[] plainTextBuffer = decryptor.TransformFinalBlock(cipherTextBuffer, 0, cipherTextBuffer.Length); return UTF8Encoding.UTF8.GetString(plainTextBuffer, 0, plainTextBuffer.Length); } 

Using the old version: plainTextBuffer - 16 bytes, the new version - 32 bytes.

Help!

+7
c # encryption portable-class-library pcl-crypto
source share
1 answer

This sounds like a supplement issue.

Considering the source of the base class SymmetricAlgorithm in .NET, which is the base of AesCryptoServiceProvider, PaddingMode.PKCS7 is used by default. It seems you did not define the fill mode, so I would suggest that the default is still applied.

So far I have not used the PCLCrypto library, quickly looking through github, there are several AesEcb algorithms: AesEcb and AesEcbPkcs7. The lack of padding mode from the name AesEcb would mean that it has no add-on (and therefore, it did not remove any add-ons), which would be equivalent to PaddingMode.None in .NET libraries.

Try using the PCLCrypto.SymmetricAlgorithm.AesEcbPkcs7 algorithm in PCLCrypto and see if this removes the add-on that you see at the end of the output.

Update

I just tested this and it works correctly and removes the registration you see:

 public static string SymmetricDecrypt(this string cipherText, string key, SymmetricAlgorithm algorithm) { byte[] keyBuffer = Convert.FromBase64String(key.Hash(HashAlgorithm.MD5)); byte[] cipherTextBuffer = Convert.FromBase64String(cipherText); ISymmetricKeyAlgorithmProvider symmetricAlgorithm = WinRTCrypto.SymmetricKeyAlgorithmProvider.OpenAlgorithm(PCLCrypto.SymmetricAlgorithm.AesEcbPkcs7); var symmetricKey = symmetricAlgorithm.CreateSymmetricKey(keyBuffer); var decryptor = WinRTCrypto.CryptographicEngine.CreateDecryptor(symmetricKey); byte[] plainTextBuffer = decryptor.TransformFinalBlock(cipherTextBuffer, 0, cipherTextBuffer.Length); return UTF8Encoding.UTF8.GetString(plainTextBuffer, 0, plainTextBuffer.Length); } 

The only change was to change the algorithm from PCLCrypto.SymmetricAlgorithm.AesEcb to PCLCrypto.SymmetricAlgorithm.AesEcbPkcs7

+4
source share

All Articles