无法使用AES算法解密文件,iv dosenot匹配块大小C# [英] Unable to decrypt the file using AES algorithm, iv dosenot match the block size C#
本文介绍了无法使用AES算法解密文件,iv dosenot匹配块大小C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在下面的代码中我无法解密文件
i正在获取其他程序使用的路径错误.Iv dosenot匹配块大小
我尝试了什么:
Hi,
In the below code o am unable to decrypt the file
i am getting error as path used by other program.Iv dosenot match the block size
What I have tried:
namespace File_Encryption
{
class AES
{
public void EncryptFile(string inputFile, string outputFile)
{
try
{
string password = @"pwd123$"; // Your Key Here
UnicodeEncoding UE = new UnicodeEncoding();
byte[] key = UE.GetBytes(password);
string cryptFile = outputFile;
FileStream fsCrypt = new FileStream(cryptFile, FileMode.Create);
RijndaelManaged RMCrypto = new RijndaelManaged();
CryptoStream cs = new CryptoStream(fsCrypt,RMCrypto.CreateEncryptor(key,key),CryptoStreamMode.Write);
FileStream fsIn = new FileStream(inputFile, FileMode.Open);
int data;
while ((data = fsIn.ReadByte()) != -1)
cs.WriteByte((byte)data);
fsIn.Close();
cs.Close();
fsCrypt.Close();
}
catch(Exception e)
{
}
}
///<summary>
/// Steve Lydford - 12/05/2008.
///
/// Decrypts a file using Rijndael algorithm.
///</summary>
///<param name="inputFile"></param>
///<param name="outputFile"></param>
public void DecryptFile(string inputFile, string outputFile)
{
{
string password = @"pwd123$"; // Your Key Here
UnicodeEncoding UE = new UnicodeEncoding();
byte[] key = UE.GetBytes(password);
FileStream fsCrypt = new FileStream(inputFile, FileMode.Open);
RijndaelManaged RMCrypto = new RijndaelManaged();
CryptoStream cs = new CryptoStream(fsCrypt,RMCrypto.CreateDecryptor(key, key),CryptoStreamMode.Read);
FileStream fsOut = new FileStream(outputFile, FileMode.Create);
int data;
while ((data = cs.ReadByte()) != -1)
fsOut.WriteByte((byte)data);
fsOut.Close();
cs.Close();
fsCrypt.Close();
}
}
}
}
using System;
using System.IO;
using System.Text;
using System.Data;
using File_Encryption;
namespace dsfsd
{
class Program
{
public static void Main()
{
File_Encryption.AES a=new AES();
a.EncryptFile("E:\\Anusha\\Path.txt","E:\\Anusha\\path.enc","123456");
a.DecryptFile("E:\\Anusha\\Path.enc","E:\\Anusha\\path.dec","123456");
}
}
}
推荐答案
; //你的钥匙在这里
UnicodeEncoding UE = new UnicodeEncoding();
byte [] key = UE.GetBytes(password);
string cryptFile = outputFile;
FileStream fsCrypt = new FileStream(cryptFile,FileMode.Create);
RijndaelManaged RMCrypto = new RijndaelManaged();
CryptoStream cs = new CryptoStream(fsCrypt,RMCrypto.CreateEncryptor(key,key),CryptoStreamMode.Write);
FileStream fsIn = new FileStream(inputFile, FileMode.Open);
int data;
while((data = fsIn.ReadByte())!= -1)
cs.WriteByte((byte)data);
fsIn.Close();
cs.Close();
fsCrypt.Close();
}
catch(例外e)
{
}
}
///< summary>
/// Steve Lydford - 12/05/2008。
///
///使用Rijndael算法解密文件。
///< / summary>
///< param name =inputFile>< / param>
///< param name =outputFile>< / param>
public void DecryptFile(string inputFile,string outputFile)
{
{
string password = @pwd123
"; // Your Key Here UnicodeEncoding UE = new UnicodeEncoding(); byte[] key = UE.GetBytes(password); string cryptFile = outputFile; FileStream fsCrypt = new FileStream(cryptFile, FileMode.Create); RijndaelManaged RMCrypto = new RijndaelManaged(); CryptoStream cs = new CryptoStream(fsCrypt,RMCrypto.CreateEncryptor(key,key),CryptoStreamMode.Write); FileStream fsIn = new FileStream(inputFile, FileMode.Open); int data; while ((data = fsIn.ReadByte()) != -1) cs.WriteByte((byte)data); fsIn.Close(); cs.Close(); fsCrypt.Close(); } catch(Exception e) { } } ///<summary> /// Steve Lydford - 12/05/2008. /// /// Decrypts a file using Rijndael algorithm. ///</summary> ///<param name="inputFile"></param> ///<param name="outputFile"></param> public void DecryptFile(string inputFile, string outputFile) { { string password = @"pwd123
; //你的钥匙在这里
UnicodeEncoding UE = new UnicodeEncoding();
byte [] key = UE.GetBytes(password);
FileStream fsCrypt = new FileStream(inputFile,FileMode.Open);
RijndaelManaged RMCrypto = new RijndaelManaged();
CryptoStream cs = new CryptoStream(fsCrypt,RMCrypto.CreateDecryptor(key,key),CryptoStreamMode.Read);
FileStream fsOut = new FileStream(outputFile,FileMode.Create);
int数据;
while((data = cs.ReadByte())!= -1)
fsOut.WriteByte((byte)data);
fsOut.Close();
cs.Close();
fsCrypt.Close();
}
}
}
}
"; // Your Key Here UnicodeEncoding UE = new UnicodeEncoding(); byte[] key = UE.GetBytes(password); FileStream fsCrypt = new FileStream(inputFile, FileMode.Open); RijndaelManaged RMCrypto = new RijndaelManaged(); CryptoStream cs = new CryptoStream(fsCrypt,RMCrypto.CreateDecryptor(key, key),CryptoStreamMode.Read); FileStream fsOut = new FileStream(outputFile, FileMode.Create); int data; while ((data = cs.ReadByte()) != -1) fsOut.WriteByte((byte)data); fsOut.Close(); cs.Close(); fsCrypt.Close(); } } } }
using System;
using System.IO;
using System.Text;
using System.Data;
using File_Encryption;
namespace dsfsd
{
class Program
{
public static void Main()
{
File_Encryption.AES a=new AES();
a.EncryptFile("E:\\Anusha\\Path.txt","E:\\Anusha\\path.enc","123456");
a.DecryptFile("E:\\Anusha\\Path.enc","E:\\Anusha\\path.dec","123456");
}
}
}
其他人之前发生过类似的错误(或者使用了网络上的一些代码而没有验证它是正确的。
请参阅SO线程的答案 c# - 指定的初始化向量(IV)与此算法的块大小不匹配 - Stack Overflow [ ^ 。它很好地解释了你的代码有什么问题。
Other's have made similar errors before (or used some code found in the web without verifying that it is correct).
See the answer at the SO thread c# - Specified initialization vector(IV) does not match the block size for this algorithm - Stack Overflow[^]. It explains very well what is wrong with your code.
这篇关于无法使用AES算法解密文件,iv dosenot匹配块大小C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文