DES-шифрование, проблема с кодировкой
Шифрование:
Код:
FileStream fsInput = new FileStream(openFileDialog1.FileName,
FileMode.Open,
FileAccess.Read);
FileStream fsEncrypted = new FileStream(saveEncFile.FileName,
FileMode.Create,
FileAccess.Write);
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
FileStream fsKey = new FileStream(saveKeyFile.FileName,
FileMode.Create,
FileAccess.Write);
StreamWriter m_streamWriter = new StreamWriter(fsKey);
String sKey = GenerateKey();
StringBuilder sBuffer = new StringBuilder();
for (int i = 0; i < sKey.Length; i++)
{
sBuffer.Append(Convert.ToInt32(sKey[i]).ToString("x"));
}
txtKeyEnc.Text = sBuffer.ToString().ToUpper();
DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
m_streamWriter.Write(sKey);
m_streamWriter.Flush();
m_streamWriter.Close();
ICryptoTransform desencrypt = DES.CreateEncryptor();
CryptoStream cryptostream = new CryptoStream(fsEncrypted,
desencrypt,
CryptoStreamMode.Write);
byte[] bytearrayinput = new byte[fsInput.Length];
fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);
cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);
cryptostream.Close();
fsInput.Close();
fsEncrypted.Close();
FileMode.Open,
FileAccess.Read);
FileStream fsEncrypted = new FileStream(saveEncFile.FileName,
FileMode.Create,
FileAccess.Write);
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
FileStream fsKey = new FileStream(saveKeyFile.FileName,
FileMode.Create,
FileAccess.Write);
StreamWriter m_streamWriter = new StreamWriter(fsKey);
String sKey = GenerateKey();
StringBuilder sBuffer = new StringBuilder();
for (int i = 0; i < sKey.Length; i++)
{
sBuffer.Append(Convert.ToInt32(sKey[i]).ToString("x"));
}
txtKeyEnc.Text = sBuffer.ToString().ToUpper();
DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
m_streamWriter.Write(sKey);
m_streamWriter.Flush();
m_streamWriter.Close();
ICryptoTransform desencrypt = DES.CreateEncryptor();
CryptoStream cryptostream = new CryptoStream(fsEncrypted,
desencrypt,
CryptoStreamMode.Write);
byte[] bytearrayinput = new byte[fsInput.Length];
fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);
cryptostream.Write(bytearrayinput, 0, bytearrayinput.Length);
cryptostream.Close();
fsInput.Close();
fsEncrypted.Close();
Код:
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
using (StreamReader fsKeyFile = new StreamReader(openKeyFile.FileName))
{
String sKey = fsKeyFile.ReadToEnd();
}
DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
// Вывод ключа в текстфилд
StringBuilder sBuffer = new StringBuilder();
for (int i = 0; i < DES.Key.Length; i++)
{
sBuffer.Append(Convert.ToInt32(DES.Key[i]).ToString("x"));
}
txtKeyDec.Text = sBuffer.ToString().ToUpper();
FileStream fsread = new FileStream(openFileDialog2.FileName,
FileMode.Open,
FileAccess.Read);
ICryptoTransform desdecrypt = DES.CreateDecryptor();
CryptoStream cryptostreamDecr = new CryptoStream(fsread,
desdecrypt,
CryptoStreamMode.Read);
StreamWriter fsDecrypted = new StreamWriter(saveDecFile.FileName);
fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());
fsDecrypted.Flush();
fsDecrypted.Close();
using (StreamReader fsKeyFile = new StreamReader(openKeyFile.FileName))
{
String sKey = fsKeyFile.ReadToEnd();
}
DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
// Вывод ключа в текстфилд
StringBuilder sBuffer = new StringBuilder();
for (int i = 0; i < DES.Key.Length; i++)
{
sBuffer.Append(Convert.ToInt32(DES.Key[i]).ToString("x"));
}
txtKeyDec.Text = sBuffer.ToString().ToUpper();
FileStream fsread = new FileStream(openFileDialog2.FileName,
FileMode.Open,
FileAccess.Read);
ICryptoTransform desdecrypt = DES.CreateDecryptor();
CryptoStream cryptostreamDecr = new CryptoStream(fsread,
desdecrypt,
CryptoStreamMode.Read);
StreamWriter fsDecrypted = new StreamWriter(saveDecFile.FileName);
fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());
fsDecrypted.Flush();
fsDecrypted.Close();
Особо не разбирался, но похоже на то, что значение ключа и/или вектора инициализации выбрано неверно. Так, длина ключа должна совпадать с BlockSize, длина IV - с BlockSize / 8.
48 65 6C 6C 6F 20 77 6F 72 6C 64 21 21 21 Hello world!!!
TXT после:
48 65 6C 6C 6F 20 77 6F 72 6C 64 21 21 21 Hello world!!!
BMP до:
42 4D 4A 00 00 00 00 00 00 00 3E 00 00 00 28 00
00 00 03 00 00 00 03 00 00 00 01 00 01 00 00 00
00 00 0C 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 FF FF FF 00 E0 00
00 00 A0 00 00 00 E0 00 00 00
BMP после:
42 4D 4A 00 00 00 00 00 00 00 3E 00 00 00 28 00
00 00 03 00 00 00 03 00 00 00 01 00 01 00 00 00
00 00 0C 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 EF BF BD EF BF BD
EF BF BD 00 EF BF BD 00 00 00 EF BF BD 00 00 00
EF BF BD 00 00 00