【发布时间】:2015-09-05 05:17:52
【问题描述】:
我正在使用 Rijndael 算法加密一个 pdf 文件。加密和解密工作正常。加密会将 pdf 文件转换为扩展名为 .key 的文件。
问题是我可以在notepad(它显示一些Unicode字符)中打开这个文件并破坏它。考虑以下场景:
我已打开文件并删除一些字符/添加一些字符和save the notepad file。如果我将此文件传递给解密方法,我将得到损坏的文件作为输出。我知道这是因为 byteStream 在添加或删除文件中的字符时会发生变化(填充更改)。
这是我的问题:
有什么办法可以解决这个问题吗?也就是说,禁用对加密文件的编辑?**
以下是用于加密的方法,
VB 代码
Dim plainFile As String = basePath & "\cryptoText.pdf"
Dim password As String = "somePass"
Dim UE As New UnicodeEncoding()
Dim key As Byte() = UE.GetBytes(password)
Dim cryptFile As String = basePath & "\cryptoText.key"
Dim fsCrypt As New FileStream(cryptFile, FileMode.Create)
Dim RMCrypto As New RijndaelManaged()
Using csKey As New CryptoStream(fsCrypt, RMCrypto.CreateEncryptor(key, key), CryptoStreamMode.Write)
Dim FsIn As New FileStream(plainFile, FileMode.Open)
Dim data As Integer
While (data = FsIn.ReadByte()) <> -1
csKey.WriteByte(CByte(data))
End While
FsIn.Close()
End Using
fsCrypt.Close()
C# 代码
string plainFile = basePath + "\\cryptoText.pdf";
string password = "somePass";
UnicodeEncoding UE = new UnicodeEncoding();
byte[] key = UE.GetBytes(password);
string cryptFile = basePath + "\\cryptoText.key";
FileStream fsCrypt = new FileStream(cryptFile, FileMode.Create);
RijndaelManaged RMCrypto = new RijndaelManaged();
using (CryptoStream csKey = new CryptoStream(fsCrypt, RMCrypto.CreateEncryptor(key, key), CryptoStreamMode.Write)) {
FileStream FsIn = new FileStream(plainFile, FileMode.Open);
int data = 0;
while ((data == FsIn.ReadByte()) != -1) {
csKey.WriteByte(Convert.ToByte(data));
}
FsIn.Close();
}
fsCrypt.Close();
注意:我已经在 c# 和 vb.net 中尝试过,所以我将我的问题标记到两者。
【问题讨论】:
标签: c# vb.net encryption cryptography rijndael