暗号化/復号化のためのWindowsフォームプロジェクトは面白いですが、私の解読アプリケーションにエラーが表示されます: 未処理の 'System.Security.Cryptography.CryptographicException 'mscorlib.dllで発生しましたなぜ私のC#の暗号化が機能していますが、復号化に悪いデータがあります
追加情報:不正なデータ。
私はインターネット上で何か修正を見つけることができませんし、C#ではうまくいかないので、おそらくあなたが私を助けることができます。
暗号化:
static void EncryptFile(string sInputFile,
string sOutputFile,
string sKey)
{
FileStream fsInput = new FileStream(sInputFile, FileMode.Open, FileAccess.Read);
FileStream fsEncrypted = new FileStream(sOutputFile, FileMode.Create, FileAccess.Write);
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
ICryptoTransform desencrypt = DES.CreateEncryptor();
CryptoStream cryptoStream = new CryptoStream(fsEncrypted, desencrypt, CryptoStreamMode.Write);
byte[] bytearrayinput = new byte[fsInput.Length - 1];
fsInput.Read(bytearrayinput, 0, bytearrayinput.Length);
cryptoStream.Write(bytearrayinput, 0, bytearrayinput.Length);
}
が復号化:私はそれらを使用する方法
static void DecryptFile(string sInputFilename,
string sOutputFilename,
string sKey)
{
DESCryptoServiceProvider DES = new DESCryptoServiceProvider();
DES.Key = ASCIIEncoding.ASCII.GetBytes(sKey);
DES.IV = ASCIIEncoding.ASCII.GetBytes(sKey);
FileStream fsread = new FileStream(sInputFilename, FileMode.Open, FileAccess.Read);
ICryptoTransform desdecrypt = DES.CreateDecryptor();
CryptoStream cryptostreamDecr = new CryptoStream(fsread, desdecrypt, CryptoStreamMode.Read);
StreamWriter fsDecrypted = new StreamWriter(sOutputFilename);
fsDecrypted.Write(new StreamReader(cryptostreamDecr).ReadToEnd());
fsDecrypted.Flush();
fsDecrypted.Close();
}
:
EncryptFile(fileBox.Text, fileOutFolder+"/encrypted.txt", sSecretKey);
DecryptFile(fileBox.Text, saveFileDialog1.FileName, keyBox.Text)
cryptoStreamにFlush()およびClose()がありません。 – Cologler
より良い方法は、 'using(){}'を使うことです。 – Cologler
私は復号化を使用するときにエラーが発生します。しかし、病気のチェック。 –