图像加密获取填充错误

时间:2016-03-26 14:32:11

标签: c# arrays encryption cryptography rijndael

我正在开发一个图像加密程序。我有两个申请。其中之一,将图像转换为字节数组并使用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              ###################################################################
    }
}

在成为一个疯子之前我该怎么办?谢谢你,等待你的宝贵答案。

1 个答案:

答案 0 :(得分:0)

感谢 Maximilian Gerhardt 解决方案就在这里;

    public static byte[] EncryptBytes(byte[] inputBytes, string passPhrase, string saltValue)
    {
        RijndaelManaged RijndaelCipher = new RijndaelManaged();
        RijndaelCipher.Mode = CipherMode.CBC;
        RijndaelCipher.Padding = PaddingMode.PKCS7;

        byte[] salt = Encoding.UTF32.GetBytes(saltValue);
        //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();
        cryptoStream.Flush();
        byte[] CipherBytes = memoryStream.ToArray();
        memoryStream.Close();
        cryptoStream.Close();
        return CipherBytes;
    }


    public static byte[] DecryptBytes(byte[] encryptedBytes, string passPhrase, string saltValue)
    {
        RijndaelManaged RijndaelCipher = new RijndaelManaged();

        RijndaelCipher.Mode = CipherMode.CBC;
        RijndaelCipher.Padding = PaddingMode.PKCS7;
        byte[] salt = Encoding.UTF32.GetBytes(saltValue);
        //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 static byte[] GetData(string FileName)
    {
        return File.ReadAllBytes(FileName);
    }

    protected bool SaveData(string FileName, byte[] Data)
    {
        try
        {
            File.WriteAllBytes(FileName, Data);
            return true;
        }
        catch
        {
            return false;
        }
    }