Я разрабатываю программу шифрования изображений. У меня есть два приложения. Один из них, преобразование изображения в массив байтов и шифрование с помощью Rijndael. После этого он сохраняет зашифрованный массив байтов в файл. Второе приложение предназначено для расшифровки. Я читаю массив байтов из файла. После того, как я расшифрую и покажу изображение в окне изображения.
Но я получаю «Заполнение недопустимо и не может быть удалено». ошибка в приложении расшифровки.
Теперь я сохраняю зашифрованный массив байтов в файл с этим кодом (я не уверен, что это верный путь для массива байтов в файл?);
protected bool SaveData(string FileName, byte[] Data)
{
BinaryWriter Writer = null;
try
{
// Create a new stream to write to the file
Writer = new BinaryWriter(File.Open(FileName,FileMode.OpenOrCreate));
// Writer raw data
Writer.Write(Data);
Writer.Flush();
Writer.Close();
}
catch
{
return false;
}
return true;
}
Я даю этому методу сохранить местоположение файла и зашифрованный массив байтов. И это сработало. Но я не знаю, это правильный путь?
И мое приложение для дешифрования, читающее зашифрованный массив байтов из файлового метода;
protected byte[] GetData(string FileName)
{
FileInfo f = new FileInfo(FileName);
BinaryReader br = new BinaryReader(File.Open(FileName, FileMode.Open));
byte[] a = br.ReadBytes(Convert.ToInt32(f.Length));
return a;
}
И метод расшифровки местоположения ошибки.
public static byte[] DecryptBytes(byte[] encryptedBytes, string passPhrase, string saltValue)
{
RijndaelManaged RijndaelCipher = new RijndaelManaged();
RijndaelCipher.Mode = CipherMode.CBC;
byte[] salt = Encoding.ASCII.GetBytes(saltValue);
PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, salt, "SHA1", 2);
ICryptoTransform Decryptor = RijndaelCipher.CreateDecryptor(password.GetBytes(32), password.GetBytes(16));
MemoryStream memoryStream = new MemoryStream(encryptedBytes);
CryptoStream cryptoStream = new CryptoStream(memoryStream, Decryptor, CryptoStreamMode.Read);
byte[] plainBytes = new byte[encryptedBytes.Length];
int DecryptedCount = cryptoStream.Read(plainBytes, 0, plainBytes.Length); // I am getting error this line. Padding is invalid and cannot be removed.
memoryStream.Flush();
cryptoStream.Flush();
memoryStream.Close();
cryptoStream.Close();
return plainBytes;
}
Код шифрования
public static byte[] EncryptBytes(byte[] inputBytes, string passPhrase, string saltValue)
{
RijndaelManaged RijndaelCipher = new RijndaelManaged();
RijndaelCipher.Mode = CipherMode.CBC;
byte[] salt = Encoding.ASCII.GetBytes(saltValue);
PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, salt, "SHA1", 2);
ICryptoTransform Encryptor = RijndaelCipher.CreateEncryptor(password.GetBytes(32), password.GetBytes(16));
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream, Encryptor, CryptoStreamMode.Write);
cryptoStream.Write(inputBytes, 0, inputBytes.Length);
cryptoStream.FlushFinalBlock();
byte[] CipherBytes = memoryStream.ToArray();
memoryStream.Close();
cryptoStream.Close();
return CipherBytes;
}
Приложение для полной расшифровки кода
namespace ImageDecrypte
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private string EncPass;
private string line;
private string OkunanVeri;
private byte[] SifreliDosyaDizi;
private byte[] CozulmusDosyaDizi;
private const string SaltPass = "CodeWork";
private string Sfre;
private string dyol;
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog file = new OpenFileDialog();
file.Filter = "Şifrelenmiş Dosyalar (*.cw)|*.cw";
file.FilterIndex = 2;
file.RestoreDirectory = true;
file.CheckFileExists = false;
file.Title = "Şifrelenmiş Dosya Seçiniz..";
file.InitialDirectory =
Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
if (file.ShowDialog() == DialogResult.OK)
{
dyol = file.FileName;
string DosyaAdi = file.SafeFileName;
label1.Text = DosyaAdi;
Sfre = textBox1.Text;
}
}
private void button2_Click(object sender, EventArgs e)
{
Sfre = textBox1.Text;
SifreliDosyaDizi = GetData(dyol);
CozulmusDosyaDizi = DecryptBytes(SifreliDosyaDizi, Sfre, SaltPass);
pictureBox1.Image = byteArrayToImage(CozulmusDosyaDizi);
}
public static byte[] DecryptBytes(byte[] encryptedBytes, string passPhrase, string saltValue)
{
RijndaelManaged RijndaelCipher = new RijndaelManaged();
RijndaelCipher.Mode = CipherMode.CBC;
byte[] salt = Encoding.ASCII.GetBytes(saltValue);
PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, salt, "SHA1", 2);
ICryptoTransform Decryptor = RijndaelCipher.CreateDecryptor(password.GetBytes(32), password.GetBytes(16));
MemoryStream memoryStream = new MemoryStream(encryptedBytes);
CryptoStream cryptoStream = new CryptoStream(memoryStream, Decryptor, CryptoStreamMode.Read);
byte[] plainBytes = new byte[encryptedBytes.Length];
int DecryptedCount = cryptoStream.Read(plainBytes, 0, plainBytes.Length);
memoryStream.Flush();
cryptoStream.Flush();
memoryStream.Close();
cryptoStream.Close();
return plainBytes.Take(DecryptedCount).ToArray();
}
public Image byteArrayToImage(byte[] byteArrayIn)
{
MemoryStream ms = new MemoryStream(byteArrayIn);
Image returnImage = Image.FromStream(ms);
return returnImage;
}
//File To Byte Array ###################################################################
protected byte[] GetData(string FileName)
{
FileInfo f = new FileInfo(FileName);
BinaryReader br = new BinaryReader(File.Open(FileName, FileMode.Open));
byte[] a = br.ReadBytes(Convert.ToInt32(f.Length));
return a;
}
//File To Byte Array ###################################################################
}
}
Приложение для полного шифрования кода
namespace ImageEncrypte
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private string EncPass;
private byte[] byteArrayForImage;
private byte[] byteArrayCoded;
private const string SaltPass = "CodeWork";
private void button1_Click(object sender, EventArgs e)
{
OpenFileDialog file = new OpenFileDialog();
file.Filter = "Jpeg Dosyası |*.jpg| Png Dosyası|*.png";
file.FilterIndex = 2;
file.RestoreDirectory = true;
file.CheckFileExists = false;
file.Title = "Bir İmaj Dosyası Seçiniz..";
file.InitialDirectory =
Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
EncPass = textBox1.Text;
if (file.ShowDialog() == DialogResult.OK)
{
string DosyaYolu = file.FileName;
string DosyaAdi = file.SafeFileName;
label1.Text = DosyaAdi;
Image img = Image.FromFile(DosyaYolu);
pictureBox1.Image = img;
byteArrayForImage = imageToByteArray(img);
byteArrayCoded = EncryptBytes(byteArrayForImage, EncPass, SaltPass);
}
}
private void button2_Click(object sender, EventArgs e)
{
SaveFileDialog sf = new SaveFileDialog();
sf.Title = "Şifrelenmiş Dosyayı Kaydet";
sf.CheckFileExists = false;
sf.CheckPathExists = true;
sf.RestoreDirectory = true;
sf.DefaultExt = "cw";
sf.FileName = "EncodedFile";
sf.SupportMultiDottedExtensions = false;
sf.Filter = "Şifrelenmiş Dosyalar (*.cw)|*.cw";
sf.InitialDirectory =
Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
if (sf.ShowDialog() == DialogResult.OK)
{
string DosyaYolu = sf.FileName;
bool cevap = SaveData(DosyaYolu, byteArrayCoded);
if (cevap)
{
MessageBox.Show("OK");
}
else
{
MessageBox.Show("PROBLEM");
}
}
}
//Image To Byte Array ####################################################################
public byte[] imageToByteArray(System.Drawing.Image imageIn)
{
using (var ms = new MemoryStream())
{
imageIn.Save(ms, System.Drawing.Imaging.ImageFormat.Gif);
return ms.ToArray();
}
}
//Image To Byte Array ####################################################################
//Byte Array To File ###################################################################
protected bool SaveData(string FileName, byte[] Data)
{
BinaryWriter Writer = null;
try
{
// Create a new stream to write to the file
Writer = new BinaryWriter(File.Open(FileName,FileMode.OpenOrCreate));
// Writer raw data
Writer.Write(Data);
Writer.Flush();
Writer.Close();
}
catch
{
return false;
}
return true;
}
//Bytte Array To File ###################################################################
//EncryptBytes ###################################################################
public static byte[] EncryptBytes(byte[] inputBytes, string passPhrase, string saltValue)
{
RijndaelManaged RijndaelCipher = new RijndaelManaged();
RijndaelCipher.Mode = CipherMode.CBC;
byte[] salt = Encoding.ASCII.GetBytes(saltValue);
PasswordDeriveBytes password = new PasswordDeriveBytes(passPhrase, salt, "SHA1", 2);
ICryptoTransform Encryptor = RijndaelCipher.CreateEncryptor(password.GetBytes(32), password.GetBytes(16));
MemoryStream memoryStream = new MemoryStream();
CryptoStream cryptoStream = new CryptoStream(memoryStream, Encryptor, CryptoStreamMode.Write);
cryptoStream.Write(inputBytes, 0, inputBytes.Length);
cryptoStream.FlushFinalBlock();
byte[] CipherBytes = memoryStream.ToArray();
memoryStream.Close();
cryptoStream.Close();
return CipherBytes;
}
//EncryptBytes ###################################################################
}
}
Что я могу сделать, прежде чем стать сумасшедшим? Спасибо и жду ваших драгоценных ответов.
Encrypt()
. - person Maximilian Gerhardt   schedule 26.03.2016DecryptedCount
для обрезки массива после нее. (pastebin.com/jAw6qcU8). Дайте ему несколько путей к файлам и посмотрите, что произойдет. - person Maximilian Gerhardt   schedule 26.03.2016File.WriteAllBytes(string path, byte[] content)
иbyte[] content = File.ReadAllBytes(string path)
. Но явных ошибок в этих функциях я не заметил. Используйте отладчик, чтобы убедиться, что то, что вы загружаете и сохраняете, действительно имеет правильный размер массива, и дайте нам код, который вызывает Encrypt() и Decrypt(). - person Maximilian Gerhardt   schedule 26.03.2016Ş ç
) и т. д. не охватываются кодовой страницей ASCII, поэтомуbyte[] salt = Encoding.ASCII.GetBytes(saltValue);
опасно, должно бытьbyte[] salt = Encoding.UTF32.GetBytes(saltValue);
(или, возможно, UTF16) . Попробуйте использовать только символы ASCII. - person Maximilian Gerhardt   schedule 26.03.2016