我无法使用c#中的Rijndael方法正确解密文件中的加密数据?
我有两种使用加密和解密的方法,我做的是读取文件数据加密并用加密数据修改文件,此操作正确完成,但当我解密加密数据时,如果我返回奇怪字符,则不会返回非原始值: 这些是功能:我无法使用c#中的Rijndael方法正确解密文件中的加密数据?,c#,encryption,cryptography,rijndaelmanaged,C#,Encryption,Cryptography,Rijndaelmanaged,我有两种使用加密和解密的方法,我做的是读取文件数据加密并用加密数据修改文件,此操作正确完成,但当我解密加密数据时,如果我返回奇怪字符,则不会返回非原始值: 这些是功能: public void EncryptFile(string[] conexion) { var mydocpath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
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;
}
任何建议……@Artjom B.他意识到我在解密功能中犯了一个错误,该功能将显示:
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
// The mistake is that he was calling the CreateEncryptor function ()
var cs = new CryptoStream(fsEncrypt, rmCrypto.CreateDecryptor(key, iv), CryptoStreamMode.Read);
StreamReader streamReader = new StreamReader(cs);
string conexion = streamReader.ReadLine();
streamReader.Close();
cs.Close();
fsEncrypt.Close();
return conexion;
}
您正在为decryption@Plutonix如果没有太多不适,请按原样给我写一个答案。您在解密过程中使用的是
rmCrypto.CreateEncryptor
。它应该是一个rmCrypto.CreateDecryptor
。这是一个输入错误问题,应该关闭。@Artjom B。错误由加密两次的函数组成,我的错误是检查不好……谢谢