-1
私は暗号化と復号化を使用する2つの方法を持っていますが、私はファイルデータを暗号化して読み込み、暗号化されたデータでファイルを修正します。私は奇妙な文字が返された場合、元のではない: これらの関数です:C#でRijndaelメソッドを使ってファイル内の暗号化されたデータを正しく復号化できませんでしたか?
public void EncryptFile(string[] conexion)
{
var mydocpath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
var inputFile = mydocpath + @"\ProjectCONFIG.sigc";
const string password = @"*PROJECT-CONFIG-FILE-ENCRYPTED-2016*"; // Your Key Here
var cryptFile = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
var fsEncrypt = new FileStream(inputFile, FileMode.Create);
var rmCrypto = new RijndaelManaged();
Rfc2898DeriveBytes pwdGen = new Rfc2898DeriveBytes(password, 10000);
rmCrypto.BlockSize = 256;
byte[] key = pwdGen.GetBytes(rmCrypto.KeySize/8); //This will generate a 256 bits key
byte[] iv = pwdGen.GetBytes(rmCrypto.BlockSize/8); //This will generate a 256 bits IV
var cs = new CryptoStream(fsEncrypt, rmCrypto.CreateEncryptor(key, iv), CryptoStreamMode.Write);
StreamWriter streamWriter = new StreamWriter(cs);
streamWriter.WriteLine(conexion);
streamWriter.Close();
cs.Close();
fsEncrypt.Close();
}
public string DecryptFile(string inputFile)
{
const string password = @"*PROJECT-CONFIG-FILE-ENCRYPTED-2016*"; // Your Key Here
var cryptFile = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
var fsEncrypt = new FileStream(inputFile, FileMode.Create);
var rmCrypto = new RijndaelManaged();
Rfc2898DeriveBytes pwdGen = new Rfc2898DeriveBytes(password, 10000);
rmCrypto.BlockSize = 256;
byte[] key = pwdGen.GetBytes(rmCrypto.KeySize/8); //This will generate a 256 bits key
byte[] iv = pwdGen.GetBytes(rmCrypto.BlockSize/8); //This will generate a 256 bits IV
var cs = new CryptoStream(fsEncrypt, rmCrypto.CreateEncryptor(key, iv), CryptoStreamMode.Read);
StreamReader streamReader = new StreamReader(cs);
string conexion = streamReader.ReadLine();
streamReader.Close();
cs.Close();
fsEncrypt.Close();
return conexion;
}
任意の提案......
解読のために別の鍵とivを生成しています – Plutonix
@Plutonixあまりにも多くの不快感がなければ答えを書いてください。 –
解読中に 'rmCrypto.CreateEncryptor'を使用しています。それは 'rmCrypto.CreateDecryptor'だったはずです。これはタイプミスであり、閉鎖する必要があります。 –