I have a C# .NET project that provides Encrypt
and Decrypt
methods using AES.
(I don't expect to win any awards (or upvotes) for the C# code you're about to see but in my humble defence, I was not responsible for it, I've just inherited it, I recognise some real problems with it, and I would be open to improving it under a separate SO question.)
public static string Encrypt(string PlainText)
{
string Password = "password replaced for this question";
string Salt = "salt replaced for this question";
string HashAlgorithm = "SHA1";
int PasswordIterations = 2;
string InitialVector = "iv replaced for this question";
int KeySize = 256;
if (string.IsNullOrEmpty(PlainText))
return "";
byte[] InitialVectorBytes = Encoding.ASCII.GetBytes(InitialVector);
byte[] SaltValueBytes = Encoding.ASCII.GetBytes(Salt);
byte[] PlainTextBytes = Encoding.UTF8.GetBytes(PlainText);
PasswordDeriveBytes DerivedPassword = new PasswordDeriveBytes(Password, SaltValueBytes, HashAlgorithm, PasswordIterations);
byte[] KeyBytes = DerivedPassword.GetBytes(KeySize / 8);
RijndaelManaged SymmetricKey = new RijndaelManaged();
SymmetricKey.Mode = CipherMode.CBC;
byte[] CipherTextBytes = null;
using (ICryptoTransform Encryptor = SymmetricKey.CreateEncryptor(KeyBytes, InitialVectorBytes))
{
using (MemoryStream MemStream = new MemoryStream())
{
using (CryptoStream CryptoStream = new CryptoStream(MemStream, Encryptor, CryptoStreamMode.Write))
{
CryptoStream.Write(PlainTextBytes, 0, PlainTextBytes.Length);
CryptoStream.FlushFinalBlock();
CipherTextBytes = MemStream.ToArray();
MemStream.Close();
CryptoStream.Close();
}
}
}
SymmetricKey.Clear();
return Convert.ToBase64String(CipherTextBytes);
}
public static string Decrypt(string CipherText)
{
string Password = "password replaced for this question";
string Salt = "salt replaced for this question";
string HashAlgorithm = "SHA1";
int PasswordIterations = 2;
string InitialVector = "iv replaced for this question";
int KeySize = 256;
if (string.IsNullOrEmpty(CipherText))
return "";
try
{
byte[] InitialVectorBytes = Encoding.ASCII.GetBytes(InitialVector);
byte[] SaltValueBytes = Encoding.ASCII.GetBytes(Salt);
byte[] CipherTextBytes = Convert.FromBase64String(CipherText);
PasswordDeriveBytes DerivedPassword = new PasswordDeriveBytes(Password, SaltValueBytes, HashAlgorithm, PasswordIterations);
byte[] KeyBytes = DerivedPassword.GetBytes(KeySize / 8);
RijndaelManaged SymmetricKey = new RijndaelManaged();
SymmetricKey.Mode = CipherMode.CBC;
byte[] PlainTextBytes = new byte[CipherTextBytes.Length];
int ByteCount = 0;
using (ICryptoTransform Decryptor = SymmetricKey.CreateDecryptor(KeyBytes, InitialVectorBytes))
{
using (MemoryStream MemStream = new MemoryStream(CipherTextBytes))
{
using (CryptoStream CryptoStream = new CryptoStream(MemStream, Decryptor, CryptoStreamMode.Read))
{
ByteCount = CryptoStream.Read(PlainTextBytes, 0, PlainTextBytes.Length);
MemStream.Close();
CryptoStream.Close();
}
}
}
SymmetricKey.Clear();
return Encoding.UTF8.GetString(PlainTextBytes, 0, ByteCount);
}
catch
{
return "";
}
}
I need to perform equivalent encryption/decryption but in PHP 7, e.g. so that PHP can decrypt (to plain text) a string that was encrypted using the .NET Encrypt
method, and encrypt a string in such a way that the .NET Decrypt
function will return the plain text.
I know just enough about encryption to be dangerous, and not nearly enough to know how to do this "properly".