I am doing a symmetric encryption of a field in a database "name", just to test it. I have never done it so I'm kinda new to this. I am getting the inputted name by the user, encrypting it using a symmetric encryption and adding it encrypted in the database. However, the value of the name is being saved as System.IO.MemoryStream
The code for the SymmetricEncryption is this:
public static MemoryStream SymmetricEncryptData(MemoryStream stream)
{
stream.Position = 0;
var myAlg = Rijndael.Create();
var myKeys = GenerateSecretKey();
MemoryStream msOut = new MemoryStream();
CryptoStream cs = new CryptoStream(msOut, myAlg.CreateEncryptor(myKeys.SecretKey,
myKeys.IV), CryptoStreamMode.Write);
stream.CopyTo(cs);
cs.FlushFinalBlock();
return msOut;
}
Then I have the code that is doing the encryption for the name and adding it, as shown below (u is the user instance, Name is the name inputted by the user)
byte[] namee = Encoding.UTF32.GetBytes(u.Name);
MemoryStream stream = new MemoryStream(namee);
u.Name = Encryption.SymmetricEncryptData(stream).ToString();
u.Blocked = false;
ur.AddUser(u);
Am I doing a wrong encryption? What am I doing wrong and why am I getting the value "System.IO.MemoryStream" in the database instead of an encrypted name?