イメージ暗号化プログラムを開発中です。私は2つのアプリケーションを持っています。その1つは、画像をバイト配列に変換し、Rijndaelで暗号化することです。暗号化バイト配列をファイルに保存した後。 2番目のアプリケーションは解読用です。ファイルからバイト配列を読み込んでいます。私は解読して、イメージボックスにイメージを表示した後。イメージ暗号化パディングエラーが発生する
しかし、私は "パディングが無効であり、削除できません。"解読アプリケーションでのエラー。
暗号化されたバイト配列をこのコードのファイルに保存しています(バイト配列をファイルに格納する方法はわかりません);
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;
}
私はこのメソッドにファイルの場所と暗号化されたバイト配列を保存しています。それは働いています。しかし、私は知らない、正しい方法ですか?
私の復号化アプリケーションは、ファイルメソッドから暗号化バイト配列を読み込みます。
エラー位置復号化方法。
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 ###################################################################
}
}
私は狂った男になる前に何ができますか?ありがとう、あなたの貴重な答えを待っています。
cryptostreamは、おそらく簡単に、より直接的な – Plutonix
はいますがaccualyこのプログラムベースのメール転送になります。そのため、暗号化されたファイルは別の人にメールを送ります。復号化アプリケーションはメール添付ファイルをダウンロードして解読します。 –
あなたは私たちに 'Encrypt()'関数を表示しませんでした。 –