Closed tinonetic closed 1 year ago
Tagging subscribers to this area: @dotnet/area-system-security, @vcsjones See info in area-owners.md if you want to be subscribed.
Author: | tinonetic |
---|---|
Assignees: | - |
Labels: | `area-System.Security`, `untriaged` |
Milestone: | - |
Hello, thanks for taking the time to file an issue.
Using your code, I can't reproduce it. The exact code I used to do so is below.
Can you please create a runnable example that demonstrates the issue? In particular, I had to fill in some gaps with the Options
for default sizes, etc. It would be helpful to know the lengths of the Options
used.
using System.Security.Cryptography;
using System.Text;
var Options = new {
Keysize = 128,
InitVectorBytes = new byte[16],
DefaultPassPhrase = "test",
DefaultSalt = new byte[16]
};
var encrypt = Encrypt("potato", "hotdog");
var decrypt = Decrypt(encrypt, "hotdog");
Console.WriteLine(decrypt);
string Encrypt(string plainText, string passPhrase = null, byte[] salt = null)
{
if (plainText == null)
{
return null;
}
if (passPhrase == null)
{
passPhrase = Options.DefaultPassPhrase;
}
if (salt == null)
{
salt = Options.DefaultSalt;
}
var plainTextBytes = Encoding.UTF8.GetBytes(plainText);
using (var password = new Rfc2898DeriveBytes(passPhrase, salt))
{
var keyBytes = password.GetBytes(Options.Keysize / 8);
using (var symmetricKey = Aes.Create())
{
symmetricKey.Mode = CipherMode.CBC;
using (var encryptor = symmetricKey.CreateEncryptor(keyBytes, Options.InitVectorBytes))
{
using (var memoryStream = new MemoryStream())
{
using (var cryptoStream = new CryptoStream(memoryStream, encryptor, CryptoStreamMode.Write))
{
cryptoStream.Write(plainTextBytes, 0, plainTextBytes.Length);
cryptoStream.FlushFinalBlock();
var cipherTextBytes = memoryStream.ToArray();
return Convert.ToBase64String(cipherTextBytes);
}
}
}
}
}
}
string Decrypt(string cipherText, string passPhrase = null, byte[] salt = null)
{
if (string.IsNullOrEmpty(cipherText))
{
return null;
}
if (passPhrase == null)
{
passPhrase = Options.DefaultPassPhrase;
}
if (salt == null)
{
salt = Options.DefaultSalt;
}
var cipherTextBytes = Convert.FromBase64String(cipherText);
using (var password = new Rfc2898DeriveBytes(passPhrase, salt))
{
var keyBytes = password.GetBytes(Options.Keysize / 8);
using (var symmetricKey = Aes.Create())
{
symmetricKey.Mode = CipherMode.CBC;
using (var decryptor = symmetricKey.CreateDecryptor(keyBytes, Options.InitVectorBytes))
{
using (var memoryStream = new MemoryStream(cipherTextBytes))
{
using (var cryptoStream = new CryptoStream(memoryStream, decryptor, CryptoStreamMode.Read))
{
var plainTextBytes = new byte[cipherTextBytes.Length];
var totalReadCount = 0;
while (totalReadCount < cipherTextBytes.Length)
{
var buffer = new byte[cipherTextBytes.Length];
var readCount = cryptoStream.Read(buffer, 0, buffer.Length);
if (readCount == 0)
{
break;
}
for (var i = 0; i < readCount; i++)
{
plainTextBytes[i + totalReadCount] = buffer[i];
}
totalReadCount += readCount;
}
return Encoding.UTF8.GetString(plainTextBytes, 0, totalReadCount);
}
}
}
}
}
}
This issue has been marked needs-author-action
and may be missing some important information.
This issue has been automatically marked no-recent-activity
because it has not had any activity for 14 days. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will remove no-recent-activity
.
This issue will now be closed since it had been marked no-recent-activity
but received no further activity in the past 14 days. It is still possible to reopen or comment on the issue, but please note that the issue will be locked if it remains inactive for another 30 days.
I have seen the following related issues and can't seem resolve mine given the solutions:
Background
Old/Expected behaviour
New/Current behaviour
The failing code