I have data encrypted by using Windows.Security.Cryptography. The working decryption code is the following:
public static string DecryptSymetric(byte[] buffEncrypt, string sKey)
{
// Declare a buffer to contain the decrypted data.
IBuffer buffDecrypted;
// Open an symmetric algorithm provider for the specified algorithm.
SymmetricKeyAlgorithmProvider objAlg = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesEcbPkcs7);
BinaryStringEncoding encoding = BinaryStringEncoding.Utf8;
// Create the symmetric key.
IBuffer keyMaterial = CryptographicBuffer.ConvertStringToBinary(sKey, encoding);
CryptographicKey key = objAlg.CreateSymmetricKey(keyMaterial);
//Only CBC algorithms require an initialization vector. For others, the initialization vector can be null
buffDecrypted = CryptographicEngine.Decrypt(key, buffEncrypt, null);
return CryptographicBuffer.ConvertBinaryToString(encoding, buffDecrypted);
}
Now, I have to use System.Security.Cryptography of .Net to decrypt the data. I have tried System.Security.Cryptography.Aes, but failed. I am wondering if anyone could provide a tip on which I should go about this. At this point of time, I just need a direction. I can try it and post the results here.
[Edit]: I have tried the following code:
public static string DecryptSymetric(byte[] buffEncrypt, string sKey)
{
using (Aes aes = Aes.Create())
{
aes.Key = Encoding.UTF8.GetBytes(sKey);
aes.IV = new byte[16];
// Create a decryptor to perform the stream transform.
ICryptoTransform decryptor = aes.CreateDecryptor(aes.Key, aes.IV);
// Create the streams used for decryption.
using (MemoryStream msDecrypt = new MemoryStream(buffEncrypt))
{
using (CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (StreamReader srDecrypt = new StreamReader(csDecrypt))
{
// Read the decrypted bytes from the decrypting stream
// and place them in a string.
string sDecrypted = srDecrypt.ReadToEnd();
return sDecrypted;
}
}
}
}
}
srDecrypt.ReadToEnd() throws the following exception:
System.Security.Cryptography.CryptographicException: Specified padding mode is not valid for this algorithm.
at Internal.Cryptography.UniversalCryptoDecryptor.DepadBlock(Byte[] block, Int32 offset, Int32 count)
at Internal.Cryptography.UniversalCryptoDecryptor.UncheckedTransformFinalBlock(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount)
at Internal.Cryptography.UniversalCryptoTransform.TransformFinalBlock(Byte[] inputBuffer, Int32 inputOffset, Int32 inputCount)
at System.Security.Cryptography.CryptoStream.FlushFinalBlock()
at System.Security.Cryptography.CryptoStream.Dispose(Boolean disposing)
at System.IO.Stream.Close()
at System.IO.StreamReader.Dispose(Boolean disposing)
at System.IO.TextReader.Dispose()
[edit] 2019-05-22 Second edit in the hope of reopening this question to allow a proven good answer to be posted an accepted.
[edit] 2019-05-24
Third edit in the hope of reopening this question to allow a proven good answer to be posted an accepted. If you would like to help, please click reopen below.