C# UWP循环冗余校验中的加密
我正在制作一个安全软件,使用AES加密对数据进行加密 当我使用FileIO进行加密或解密时,它可以正常工作,因为所有字节都会被读取并一次馈送到encrypt函数,但这会导致大型文件出现问题,因为它会将大型文件加载到ram中。 所以我使用datareader和datawriter部分加载文件并将其发送到加密 但问题是,当我解密这个文件时,它会抛出循环冗余错误。我知道部分原因,但不知道如何修复 这是我的密码 使用数据读写器的新系统C# UWP循环冗余校验中的加密,c#,win-universal-app,windows-10,C#,Win Universal App,Windows 10,我正在制作一个安全软件,使用AES加密对数据进行加密 当我使用FileIO进行加密或解密时,它可以正常工作,因为所有字节都会被读取并一次馈送到encrypt函数,但这会导致大型文件出现问题,因为它会将大型文件加载到ram中。 所以我使用datareader和datawriter部分加载文件并将其发送到加密 但问题是,当我解密这个文件时,它会抛出循环冗余错误。我知道部分原因,但不知道如何修复 这是我的密码 使用数据读写器的新系统 public async void EncryptDecryptFi
public async void EncryptDecryptFileNew(StorageFile sourcefile, StorageFolder destinationfolder, string password, string username, bool IsEncrypt)
{
passwordstring = AES_Encrypt(StringToBytes(password), username);
if (sourcefile != null)
{
StorageFile sfile = null;
bool isCompletedSuccesfully = false;
try
{
if (StorageApplicationPermissions.FutureAccessList.CheckAccess(sourcefile))
{
var properties = await sourcefile.GetBasicPropertiesAsync();
string temp = destinationfolder.Path;
StorageFolder sf = await StorageFolder.GetFolderFromPathAsync(temp);
sfile = await sf.CreateFileAsync(sourcefile.Name, CreationCollisionOption.ReplaceExisting);
Debug.WriteLine(sourcefile.DisplayName + properties.Size, "FileStart");
if (properties.Size > 0)
{
using (Stream sstream = await sourcefile.OpenStreamForReadAsync())
{
using (DataReader sourceReader = new DataReader(sstream.AsInputStream()))
{
using (var dstream = await sfile.OpenAsync(FileAccessMode.ReadWrite))
{
using (var outputStream = dstream.GetOutputStreamAt(0))
{
using (DataWriter dWriter = new DataWriter(outputStream))
{
byte[] f;
for (ulong i = 0; i < (properties.Size / 64); i++)
{
await sourceReader.LoadAsync(64);
if (IsEncrypt)
f = AES_Encrypt(sourceReader.ReadBuffer(64).ToArray(), BytesToString(passwordstring));
else
f = AES_Decrypt(sourceReader.ReadBuffer(64).ToArray(), BytesToString(passwordstring));
dWriter.WriteBytes(f);
}
uint remaining = (uint)properties.Size % 64;
if ((remaining) != 0)
{
await sourceReader.LoadAsync(remaining);
if (IsEncrypt)
f = AES_Encrypt(sourceReader.ReadBuffer(remaining).ToArray(), BytesToString(passwordstring));
else
f = AES_Decrypt(sourceReader.ReadBuffer(remaining).ToArray(), BytesToString(passwordstring));
dWriter.WriteBytes(f);
}
await dWriter.StoreAsync();
}
}
}
}
}
isCompletedSuccesfully = true;
GC.Collect();
}
else
isCompletedSuccesfully = true;
Debug.WriteLine(sourcefile.DisplayName, "FileEnd");
}
}
catch (OutOfMemoryException y)
{
GC.Collect();
}
catch (Exception e)
{
Debug.WriteLine(e.Message, "Encryption File");
}
finally
{
System.GC.Collect();
if (isCompletedSuccesfully)
{
if (totalSize != 0)
{
Debug.WriteLine(sourcefile.DisplayName + " " + fileDictionary[sourcefile.Path] + " " + totalSize);
Percentage += fileDictionary[sourcefile.Path] / totalSize;
}
if (FEDD != null)
FEDD();
}
else
{
if (fileDictionary.Count != 0)
{
if (sfile != null)
{
await sfile.DeleteAsync();
}
EncryptDecryptFileNew(sourcefile, destinationfolder, password, username, IsEncrypt);
}
}
await Task.CompletedTask;
}
}
}
用于解密
public byte[] AES_Decrypt(byte[] input, string pass)
{
SymmetricKeyAlgorithmProvider SAP = SymmetricKeyAlgorithmProvider.OpenAlgorithm(SymmetricAlgorithmNames.AesEcbPkcs7);
CryptographicKey AES;
HashAlgorithmProvider HAP = HashAlgorithmProvider.OpenAlgorithm(HashAlgorithmNames.Md5);
CryptographicHash Hash_AES = HAP.CreateHash();
try
{
byte[] hash = new byte[32];
Hash_AES.Append(CryptographicBuffer.CreateFromByteArray(System.Text.Encoding.UTF8.GetBytes(pass)));
byte[] temp;
CryptographicBuffer.CopyToByteArray(Hash_AES.GetValueAndReset(), out temp);
Array.Copy(temp, 0, hash, 0, 16);
Array.Copy(temp, 0, hash, 15, 16);
AES = SAP.CreateSymmetricKey(CryptographicBuffer.CreateFromByteArray(hash));
IBuffer Buffer = GetBufferFromBytes(input);
byte[] Decrypted;
CryptographicBuffer.CopyToByteArray(CryptographicEngine.Decrypt(AES, Buffer, null), out Decrypted);
return Decrypted;
}
catch (Exception ex)
{
Debug.WriteLine(ex.Message);
return null;
}
}
还有那辆旧的车
public async void EncryptDecryptFile(StorageFile sourcefile,StorageFolder destinationfolder,string password,string username,bool IsEncrypt)
{
passwordstring = AES_Encrypt(StringToBytes(password), username);
if (sourcefile != null)
{
StorageFile sfile=null;
bool isCompletedSuccesfully = false;
try
{
if (StorageApplicationPermissions.FutureAccessList.CheckAccess(sourcefile))
{
var properties = await sourcefile.GetBasicPropertiesAsync();
string temp = destinationfolder.Path;
StorageFolder sf = await StorageFolder.GetFolderFromPathAsync(temp);
sfile = await sf.CreateFileAsync(sourcefile.Name, CreationCollisionOption.ReplaceExisting);
Debug.WriteLine(sourcefile.DisplayName+properties.Size,"FileStart");
if (properties.Size > 0)
{
IBuffer buffer = await FileIO.ReadBufferAsync(sourcefile);
byte[] f = null;
Debug.WriteLine("tw1", sourcefile.Name);
GC.Collect();
if (IsEncrypt)
f = AES_Encrypt(buffer.ToArray(), BytesToString(passwordstring));
else
f = AES_Decrypt(buffer.ToArray(), BytesToString(passwordstring));
Debug.WriteLine("tw2", sourcefile.Name);
GC.Collect();
var rt = FileIO.WriteBufferAsync(sfile, GetBufferFromBytes(f));
rt.AsTask().Wait();
isCompletedSuccesfully = true;
GC.Collect();
Debug.WriteLine("tw3", sourcefile.Name);
}
else
isCompletedSuccesfully = true;
Debug.WriteLine(sourcefile.DisplayName, "FileEnd");
}
}
catch (OutOfMemoryException y)
{
GC.Collect();
}
catch (Exception e)
{
Debug.WriteLine(e.Message,"Encryption File");
}
finally
{
System.GC.Collect();
if (isCompletedSuccesfully)
{
if (totalSize != 0)
{
Debug.WriteLine(sourcefile.DisplayName + " " + fileDictionary[sourcefile.Path] + " " + totalSize);
Percentage += fileDictionary[sourcefile.Path] / totalSize;
}
if(FEDD!=null)
FEDD();
}
else
{
if (fileDictionary.Count != 0)
{
if (sfile != null)
{
await sfile.DeleteAsync();
}
EncryptDecryptFile(sourcefile, destinationfolder, password, username, IsEncrypt);
}
}
await Task.CompletedTask;
}
}
}
public async void EncryptDecryptFile(StorageFile sourcefile,StorageFolder destinationfolder,string password,string username,bool IsEncrypt)
{
passwordstring = AES_Encrypt(StringToBytes(password), username);
if (sourcefile != null)
{
StorageFile sfile=null;
bool isCompletedSuccesfully = false;
try
{
if (StorageApplicationPermissions.FutureAccessList.CheckAccess(sourcefile))
{
var properties = await sourcefile.GetBasicPropertiesAsync();
string temp = destinationfolder.Path;
StorageFolder sf = await StorageFolder.GetFolderFromPathAsync(temp);
sfile = await sf.CreateFileAsync(sourcefile.Name, CreationCollisionOption.ReplaceExisting);
Debug.WriteLine(sourcefile.DisplayName+properties.Size,"FileStart");
if (properties.Size > 0)
{
IBuffer buffer = await FileIO.ReadBufferAsync(sourcefile);
byte[] f = null;
Debug.WriteLine("tw1", sourcefile.Name);
GC.Collect();
if (IsEncrypt)
f = AES_Encrypt(buffer.ToArray(), BytesToString(passwordstring));
else
f = AES_Decrypt(buffer.ToArray(), BytesToString(passwordstring));
Debug.WriteLine("tw2", sourcefile.Name);
GC.Collect();
var rt = FileIO.WriteBufferAsync(sfile, GetBufferFromBytes(f));
rt.AsTask().Wait();
isCompletedSuccesfully = true;
GC.Collect();
Debug.WriteLine("tw3", sourcefile.Name);
}
else
isCompletedSuccesfully = true;
Debug.WriteLine(sourcefile.DisplayName, "FileEnd");
}
}
catch (OutOfMemoryException y)
{
GC.Collect();
}
catch (Exception e)
{
Debug.WriteLine(e.Message,"Encryption File");
}
finally
{
System.GC.Collect();
if (isCompletedSuccesfully)
{
if (totalSize != 0)
{
Debug.WriteLine(sourcefile.DisplayName + " " + fileDictionary[sourcefile.Path] + " " + totalSize);
Percentage += fileDictionary[sourcefile.Path] / totalSize;
}
if(FEDD!=null)
FEDD();
}
else
{
if (fileDictionary.Count != 0)
{
if (sfile != null)
{
await sfile.DeleteAsync();
}
EncryptDecryptFile(sourcefile, destinationfolder, password, username, IsEncrypt);
}
}
await Task.CompletedTask;
}
}
}