is it possible to use random salt and random iv in encryption and decryption algorithm using AES or RijndaelManaged?
I was learning about encryption and decryption algorithms, I tried using aes or rijndaelmanaged in C#, someone else said, if you use a static salt for encryption and reuses the IV it won't be safe.
Encryption
public static byte[] encryptAES(byte[] bytesToBeEncrypted, byte[]
passwordBytes)
{
byte[] result = null;
byte[] salt = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
using (MemoryStream MS = new MemoryStream())
{
using (RijndaelManaged Rima = new RijndaelManaged())
{
Rima.KeySize = 256;
Rima.BlockSize = 128;
Rfc2898DeriveBytes RFCDB = new Rfc2898DeriveBytes(passwordBytes, salt,
1000);
Rima.Key = RFCDB.GetBytes(Rima.KeySize / 8);
Rima.IV = RFCDB.GetBytes(Rima.BlockSize / 8);
Rima.Mode = CipherMode.CBC;
using (CryptoStream CS = new CryptoStream(MS, Rima.CreateEncryptor(),
CryptoStreamMode.Write))
{
CS.Write(bytesToBeEncrypted, 0, bytesToBeEncrypted.Length);
CS.Close();
}
result = MS.ToArray();
}
}
return result;
}
Decryption
public static byte[] decryptAES(byte[] bytesToBeDecrypted, byte[]
passwordBytes)
{
byte[] result = null;
using (MemoryStream memoryStream = new MemoryStream())
{
Rfc2898DeriveBytes rfc2898DeriveBytes = new
Rfc2898DeriveBytes(passwordBytes, saltBytes, 1000);
Rima.Key = rfc2898DeriveBytes.GetBytes(Rima.KeySize / 8);
Rima.IV = rfc2898DeriveBytes.GetBytes(Rima.BlockSize / 8);
using (CryptoStream cryptoStream = new CryptoStream(memoryStream,
Rima.CreateDecryptor(), CryptoStreamMode.Write))
{
cryptoStream.Write(bytesToBeDecrypted, 0, bytesToBeDecrypted.Length);
cryptoStream.Close();
}
result = memoryStream.ToArray();
}
return result;
}
private static RijndaelManaged Rima = new RijndaelManaged
{
KeySize = 256,
BlockSize = 128,
Mode = CipherMode.CBC
};
private static byte[] saltBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8 };
I tried to find a way to use random or dynamic salt and random IV.