Everytime i debuc i always get this message "length of the data to decrypt is invalid"
here is the PasswordEncryption class
public class PasswordEncryption
{
string passPhrass = "";
public PasswordEncryption()
{
passPhrass = "P333awss";
}
private byte[] Encrypt(byte[] clearData, byte[] Key, byte[] IV)
{
MemoryStream ms = new MemoryStream();
Rijndael algorithm = Rijndael.Create();
algorithm.Key = Key;
algorithm.IV = IV;
CryptoStream cs = new CryptoStream(ms, algorithm.CreateEncryptor(), CryptoStreamMode.Write);
cs.Write(clearData, 0, clearData.Length);
cs.Close();
byte[] encrytedData = ms.ToArray();
return encrytedData;
}
public string Encrypt(string password)
{
byte[] clearBytes = Encoding.Unicode.GetBytes(password);
byte[] saltBytes =
{
0x49,
0x76,
0x61,
0x6e,
0x20,
0x4d,
0x65,
0x64,
0x76,
0x65,
0x64,
0x65,
0x76
};
PasswordDeriveBytes passwordDeriveBytes = new PasswordDeriveBytes(passPhrass, saltBytes);
byte[] encryptedData = Encrypt(clearBytes, passwordDeriveBytes.GetBytes(32), passwordDeriveBytes.GetBytes(16));
return Convert.ToBase64String(encryptedData);
}
private byte[] Decrypt(byte[] cipherData, byte[] Key, byte[] IV)
{
MemoryStream memoryStream = new MemoryStream();
Rijndael algorithm = Rijndael.Create();
algorithm.Key = Key;
algorithm.IV = IV;
CryptoStream cryptoStream = new CryptoStream(memoryStream, algorithm.CreateDecryptor(), CryptoStreamMode.Write);
cryptoStream.Write(cipherData, 0, cipherData.Length);
cryptoStream.Close();
byte[] decryptedData = memoryStream.ToArray();
return decryptedData;
}
public string Decrypt(string password)
{
byte[] passwordBytes = Convert.FromBase64String(password);
byte[] saltBytes =
{
0x49,
0x76,
0x61,
0x6e,
0x20,
0x4d,
0x65,
0x64,
0x76,
0x65,
0x64,
0x65,
0x76
};
PasswordDeriveBytes passwordDeriveBytes = new PasswordDeriveBytes(passPhrass, saltBytes);
byte[] decryptedData = Decrypt(passwordBytes, passwordDeriveBytes.GetBytes(32), passwordDeriveBytes.GetBytes(16));
return Encoding.Unicode.GetString(decryptedData);
}
}