so my problem is simple. I want to Encrypt some text and write it to File like byte of array and than i need read the content of the File and get like parametr to another method which Decrypt that byte of array to string.
Method Encrypt and Decrypt work fine i try it but when i use byte array from FILE it thrown Exception.
private static byte[] EncryptString(string text,byte[] key,byte[] vektor)
{
byte[] array=null;
// Check arguments.
if (text == null || text.Length <= 0)
throw new ArgumentNullException("plainText");
if (key == null || key.Length <= 0)
throw new ArgumentNullException("Key");
if (vektor == null || vektor.Length <= 0)
throw new ArgumentNullException("Vektor");
try
{
using (DESCryptoServiceProvider provider = new DESCryptoServiceProvider())
{
provider.Key = key;
provider.IV = vektor;
using (MemoryStream memory = new MemoryStream())
{
using (CryptoStream crypto = new CryptoStream(memory, provider.CreateEncryptor(provider.Key, provider.IV), CryptoStreamMode.Write))
{
using (StreamWriter writer = new StreamWriter(crypto))
{
writer.WriteLine(text);
}
}
array = memory.ToArray();
}
}
}
catch (ArgumentNullException e)
{
Console.WriteLine("Error in EncryptString {0}", e.Message);
}
catch (Exception e)
{
Console.WriteLine("Error in EncryptString {0}", e.Message);
}
return array;
}
This method Ecnrypt byte array to string
private static string DecryptByte(byte[] text, byte[] key, byte[] vektor)
{
string result = null;
// Check arguments.
if (text == null || text.Length <= 0)
throw new ArgumentNullException("plainText");
if (key == null || key.Length <= 0)
throw new ArgumentNullException("Key");
if (vektor == null || vektor.Length <= 0)
throw new ArgumentNullException("Key");
try
{
using (DESCryptoServiceProvider provider = new DESCryptoServiceProvider())
{
provider.Key=key;
provider.IV=vektor;
using (MemoryStream memory = new MemoryStream(text))
{
using (CryptoStream crypto = new CryptoStream(memory, provider.CreateDecryptor(provider.Key, provider.IV), CryptoStreamMode.Read))
{
using (StreamReader read = new StreamReader(crypto))
{
result=read.ReadToEnd();
}
}
}
}
}
catch(Exception e)
{
Console.WriteLine("Error in DecryptByte:{0}"+e.Message);
}
return result;
}
Well i save the encrypt text and then when button is pushed
private void buttonDecrypt_Click(object sender, RoutedEventArgs e)
{
byte[] text=null,helper=null;
string result = null;
try
{
using (FileStream filestream = File.OpenRead(path))
{
helper = new byte[filestream.Length];
filestream.Read(helper, 0, (int)filestream.Length);
}
using (DESCryptoServiceProvider provider = new DESCryptoServiceProvider())
{
result = DecryptByte(helper, provider.Key, provider.IV);
}
}
catch(Exception ex)
{
Console.WriteLine("Error v ButtonDecrypt.{0}_____{1}",ex.Message,ex.Data);
}
}
So my problem is that data which i read from File is not the same and in DecryptByte thrown Exception. Could you help me?
EDIT yeah Exception message is Invalid Data
The code in your buttonDecrypt_Click you don’t initialize the Key and the IV properties of your Decryptor.
I suspect that this is the cause of your exception.
Moreover, I think you have another problem in the crypting and decrypting methods.
DESCryptoServiceProvider is a wrapper around a SymmetricAlgorithm. This means that it encrypts bytes in blocks of equal size. So the input data needs to be an exact multiple of the block size. This isn’t going to happen all the time. Then padding needs to be used.
will ensure a correct block size in encrypted bytes (also needed in decrypting of course)