CryptoJs 文件加密大大增加了文件大小
CryptoJs file encryption increasing file size drastically
通过 cryptojs 加密文件会使文件大小增加近 30%。这导致在 C# 中使用 AESManaged class 解密文件时出现问题。如何在不增加太多大小的情况下将加密对象另存为文件?
JS中的文件加密:
function esp() {
selectedFiles = document.getElementById("MainContent_file1");
var sfile = selectedFiles.files[0];
var read = new FileReader();
read.onload = function (e) {
var encrypted = CryptoJS.AES.encrypt(read.result, '123456');
var ct = encrypted.toString();
debugger;
$.ajax({
async: 'true',
url: "http://localhost:51936/WebService1.asmx/FileUpload",
method: "POST",
processData: 'false',
headers: {
'content-type': "application/json",
'cache-control': "no-cache"
},
data: JSON.stringify({ 'folderPath': folderPath, 'uploadData': ct, 'fileName': sfile.name + '.encrypted' }),
success: function (response) {
console.log(response);
},
error: function (xhr, textStatus, error) {
console.log(xhr.statusText);
}
});
}
read.readAsDataURL(sfile);
}
将加密对象保存为文件的网络服务:
[WebMethod]
public bool FileUpload(string folderPath, string uploadData, string fileName)
{
//NOTE: A CODE SCAN TOOL is showing a PATH TRAVERSAL ERROR. a folderPath can be retrieve from DATABASE for remove the error but would affect the performance which is not advisable.
bool returnValue = false;
try
{
byte[] byteUploadFile = Convert.FromBase64String(uploadData);
BinaryWriter binWriter = new BinaryWriter(File.Open(Path.Combine(folderPath, fileName), FileMode.Create, FileAccess.ReadWrite));
binWriter.Write(byteUploadFile);
binWriter.Close();
returnValue = true;
}
catch (Exception ex)
{
returnValue = false;
}
return returnValue;
}
我将加密字符串(base64)保存为文件的方式是正确的。然而,为了正确解密它并从解密的字符串中生成原始文件,一些剥离是必要的。请参阅下文了解我如何从加密文件中获取解密字符串并将其保存为原始文件。
Mywebservice.Mywebservice dts = new Mywebservice.Mywebservice();
using (var rijAlg = new RijndaelManaged())
{
rijAlg.Key = keybytes;
rijAlg.IV = iv;
var decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);
using (var msDecrypt = new MemoryStream(cipherText))
{
using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (var srDecrypt = new StreamReader(csDecrypt))
{
plaintext = srDecrypt.ReadToEnd();
}
}
}
}
plaintext = plaintext.Substring(23); //This is the part to strip. The first
//23 chars are the mime type of the file. Remove that and just save the string data.
string name = filename.Replace(".encrypted", "");
dts.FileUpload(folderPath, plaintext, name); //call the same webservice used for saving encrypted object.
}
通过 cryptojs 加密文件会使文件大小增加近 30%。这导致在 C# 中使用 AESManaged class 解密文件时出现问题。如何在不增加太多大小的情况下将加密对象另存为文件?
JS中的文件加密:
function esp() {
selectedFiles = document.getElementById("MainContent_file1");
var sfile = selectedFiles.files[0];
var read = new FileReader();
read.onload = function (e) {
var encrypted = CryptoJS.AES.encrypt(read.result, '123456');
var ct = encrypted.toString();
debugger;
$.ajax({
async: 'true',
url: "http://localhost:51936/WebService1.asmx/FileUpload",
method: "POST",
processData: 'false',
headers: {
'content-type': "application/json",
'cache-control': "no-cache"
},
data: JSON.stringify({ 'folderPath': folderPath, 'uploadData': ct, 'fileName': sfile.name + '.encrypted' }),
success: function (response) {
console.log(response);
},
error: function (xhr, textStatus, error) {
console.log(xhr.statusText);
}
});
}
read.readAsDataURL(sfile);
}
将加密对象保存为文件的网络服务:
[WebMethod]
public bool FileUpload(string folderPath, string uploadData, string fileName)
{
//NOTE: A CODE SCAN TOOL is showing a PATH TRAVERSAL ERROR. a folderPath can be retrieve from DATABASE for remove the error but would affect the performance which is not advisable.
bool returnValue = false;
try
{
byte[] byteUploadFile = Convert.FromBase64String(uploadData);
BinaryWriter binWriter = new BinaryWriter(File.Open(Path.Combine(folderPath, fileName), FileMode.Create, FileAccess.ReadWrite));
binWriter.Write(byteUploadFile);
binWriter.Close();
returnValue = true;
}
catch (Exception ex)
{
returnValue = false;
}
return returnValue;
}
我将加密字符串(base64)保存为文件的方式是正确的。然而,为了正确解密它并从解密的字符串中生成原始文件,一些剥离是必要的。请参阅下文了解我如何从加密文件中获取解密字符串并将其保存为原始文件。
Mywebservice.Mywebservice dts = new Mywebservice.Mywebservice();
using (var rijAlg = new RijndaelManaged())
{
rijAlg.Key = keybytes;
rijAlg.IV = iv;
var decryptor = rijAlg.CreateDecryptor(rijAlg.Key, rijAlg.IV);
using (var msDecrypt = new MemoryStream(cipherText))
{
using (var csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read))
{
using (var srDecrypt = new StreamReader(csDecrypt))
{
plaintext = srDecrypt.ReadToEnd();
}
}
}
}
plaintext = plaintext.Substring(23); //This is the part to strip. The first
//23 chars are the mime type of the file. Remove that and just save the string data.
string name = filename.Replace(".encrypted", "");
dts.FileUpload(folderPath, plaintext, name); //call the same webservice used for saving encrypted object.
}