Progressive Upload and Encryption with CryptoJS - filereader

The goal here is to upload a file, encrypt it at the client side, and send the file and its attributes via AJAX to myphpscript.php. To allow larger files, I want to upload in slices using the FileReader slice method and progressively encrypt the slices using the methods described on the CryptoJS site (https://code.google.com/p/crypto-js/). My code below runs, but only ends up storing a a small portion of the intended entire encrypted file. Can I progressively upload and encrypt in the way I am describing?
<script type="text/javascript" src="js/jquery-1.11.0.min.js"></script>
<script src="js/aes.js"></script>
<script>
function readBlob(opt_startByte, opt_stopByte) {
var files = document.getElementById('fileinput').files;
if (!files.length) {
alert('Please select a file!');
return;
}
var file = files[0];
var start = parseInt(opt_startByte) || 0;
var stop = parseInt(opt_stopByte) || file.size - 1;
var reader = new FileReader();
// If we use onloadend, we need to check the readyState.
reader.onloadend = function(evt) {
if (evt.target.readyState == FileReader.DONE) { // DONE == 2
window.bits.push(aesEncryptor.process(evt.target.result));
}
};
var blob = file.slice(start, stop + 1);
reader.readAsBinaryString(blob);
}
function handling(evt) {
// INITIALIZE PROGRESSIVE ENCRYPTION
var key = CryptoJS.enc.Hex.parse(document.getElementById('pass').value);
var iv = CryptoJS.lib.WordArray.random(128 / 8);
window.bits = [];
window.aesEncryptor = CryptoJS.algo.AES.createEncryptor(key, {iv: iv});
// LOOP THROUGH BYTES AND PROGRESSIVELY ENCRYPT
var startByte = 0;
var endByte = 0;
while(startByte < document.querySelector('input[type=file]').files[0].size - 1){
endByte = startByte + 1000000;
readBlob(startByte, endByte);
startByte = endByte;
}
// FINALIZE ENCRYPTION AND UPLOAD
var encrypted = aesEncryptor.finalize();
encrypted = encodeURIComponent(encrypted);
var filename = document.getElementById('fileinput').value;
var file_type = document.getElementById('fileinput').files[0].type;
var url = 'data=' + encrypted + '&filename=' + filename + '&filetype=' + file_type;
$.ajax({
url: 'myphpscript.php',
type: 'POST',
data: url
}).success(function(data){
// Display encrypted data
document.getElementById('status').innerHTML = 'Upload Complete.';
});
alert(encrypted);
}
</script>

So your problem is the line var encrypted = aesEncryptor.finalize();
This is not the encrypted file, but the final 'chunk' produced by the CryptoJS.AES finalizer.
you need to append that to the end of the window.bits buffer to yield the fully encrypted file.
Also, you shouldn't be using window.bits.push, you should keep hold of a reference to each chunk like this (psuedocode):
var prog;
//then in the loop, if chunk is null assign to chunk or else concat:
loop:
if(!prog)
prog = cipher.process()
else
prog.concat(cipher.process())
//then finalize
prog.concat(cipher.finalize())
//now you're free to do whatever with the encrypted file:
var ciphertext = prog.toString()

One important thing to keep in mind is that the chunks may arrive to the encryptor out of order so you either have to keep track of the order that chunks went into aesEncryptor.process so that you can decrypt in the proper order later or queue the chunks and encrypt them in the proper order to begin with.

Related

Object overwritten in firebase

Data sending from google sheet:
Script using to send data to firebase from google sheet.
function writeData() {
var ss = SpreadsheetApp.openById("####");
var sheet = ss.getSheets()[0];
var data = sheet.getDataRange().getValues();
var dataToImport = {};
for(var i = 1; i < data.length; i++) {
var department = data[i][0];
var year = data[i][1];
var course = data[i][2];
dataToImport[department] = {};
dataToImport[department][year] = {}
dataToImport[department][year][course] = {}
dataToImport[department][year][course][i] = {
course: data[i][3],
dateAdded: data[i][4],
fileSize: data[i][5],
fileType:data[i][6],
downloadLink: data[i][7],
};
}
var firebaseUrl = "https:url";
var base = FirebaseApp.getDatabaseByUrl(firebaseUrl);
base.setData("",dataToImport);
}
Data that is sent:
Q: My questions that since i had parent electrical and its two childs that were 1 and 2 and each child had a course dsp and aes respectively but only 1 child is send that is the second one.
Why first child is not sent to firebase ?
This code replacing previous entry with new entry. Replace this -
dataToImport[department] = {};
dataToImport[department][year] = {}
dataToImport[department][year][course] = {}
with this -
dataToImport[department] = dataToImport[department] || {};
dataToImport[department][year] = dataToImport[department][year] || {}
dataToImport[department][year][course] = dataToImport[department][year][course] || {}
As a side note to #ra89fi's answer (because I didn't pick up on the overwrite bug).
The line base.setData("",dataToImport); is setting all data at the root of your database ("") with the given data (dataToImport). Any other data in your database will be deleted.
Instead, you should use an update operation.
I unfortunately can't establish which version of the Firebase API you are using. It's not quite the REST API and not quite JavaScript. So here are some relevant documentation links.
REST Update Guide
REST Update Reference
JavaScript (Web) Update Guide
JavaScript (Web) Update Reference

ZPK Encryption ISO format 9594-1 Format 0

I need to integrate our IVR with ATM switch. In this case IVR needs to send pin block formed in ISO format 9594-1 Format 0 only (Zone pin key – Pin encryption). WE have Clear component - 1 ,2, & 3 & ZMK - Key check value. I need to know steps to generate PIN Block format 0 using ZPK encryption. Also as I am beginner need to know the role played here of clear component & ZMK .
Here is some javascript that will encrypt a pin into a Format-0 (PAN-free) pinblock. In this example it is creating a random DES ZPK (zone pin key), but you can use a fixed zone pin key if you wish. It is also using CryptoJS.
(I have an example in C# if you need it)
_createPinblockFromPin: function(pin) {
var pinBlockString = "1" + pin.length.toString() + pin;
while (pinBlockString.length < 16) {
var r = encoding.b2h((Math.random() * 0x100) & 255);
pinBlockString += r.toString(8).toUpperCase();
}
return pinBlockString;
}
_createZpk: function() {
var key = CryptoJS.enc.Hex.stringify(CryptoJS.lib.WordArray.random(16));
return key.toUpperCase();
}
_encryptPinBlock: function(clearPinBlockHex, zpkHex) {
var clearPinBlock = CryptoJS.enc.Hex.parse(clearPinBlockHex);
var key = CryptoJS.enc.Hex.parse(zpkHex);
key.words[4] = key.words[0];
key.words[5] = key.words[1];
var iv = CryptoJS.lib.WordArray.create(64 / 8);
var options = { iv: iv, mode: CryptoJS.mode.ECB, padding: CryptoJS.pad.NoPadding };
var encrypted = CryptoJS.TripleDES.encrypt(clearPinBlock, key, options);
var encryptedHex = encrypted.toString().toUpperCase();
return encryptedHex;
}
Usage is:
var clearPinBlock = this._createPinblockFromPin(pin);
var clearZpk = this._createZpk();
var ePinBlock = this._encryptPinBlock(clearPinBlock, clearZpk);
Here is a c# example that used BouncyCastle.net
// create zpk
var keyGenParams = new KeyGenerationParameters(new SecureRandom(), 128);
var keyGen = new DesEdeKeyGenerator();
keyGen.Init(keyGenParams);
var zpk = keyGen.GenerateKey();
System.Diagnostics.Debug.WriteLine($"zpk->{DataFormatter.ToHexString(zpk)}");
// create pinblock
var random = new Random();
var pinString = $"1{pin.Length}{pin}";
while (pinString.Length < 16) pinString += random.Next(0, 9).ToString();
var pinBlock = DataFormatter.HexStringToByteArray(pinString);
System.Diagnostics.Debug.WriteLine($"clearPinBlock->{DataFormatter.ToHexString(pinBlock)}");
// encrypt pinblock
var pinBlockZpk = new byte[8];
var keyParam = new DesEdeParameters(zpk);
var desEngine1 = new DesEdeEngine();
desEngine1.Init(true, keyParam);
desEngine1.ProcessBlock(pinBlock, 0, pinBlockZpk, 0);
System.Diagnostics.Debug.WriteLine($"pinBlockZpk-> {DataFormatter.ToHexString(pinBlockZpk)}");
Note that DataFormatter.ToHexString() is just a utility to convert a byte[] to a hex string for diagnostics.

How to write Meteor.wrapAsync fs.readFile?

I need a function that emits individual lines from a file with newlines. Nothing hard.
But with node, it is hard, and with Meteor, there's an additional complication: you must use Meteor.wrapAsync. Surprisingly, there isn't an example of how to use wrapAsync in the docs, and I could only find a couple of examples online, none of which helped.
I have something like:
var readFileAsync = function (file, cb) {
// From here to below comment works synchronously
var instream = fs.createReadStream(file, function () {
var outstream = new stream;
outstream.readable = true;
outstream.writable = true;
var rl = readline.createInterface({
input: instream,
output: outstream,
terminal: false
});
rl.on('line', function(line) {
console.log(line);
return line;
});
});
// Reference to aforementioned comment
};
var readWatFile = Meteor.wrapAsync(readFileAsync);
var line = readWatFile('/path/to/my/file');
console.log(line);
I know this is wrong because it doesn't work, so how do I write this?
There are two ways to go around it.
Load the whole file into memory and do whatever you want. To do that you can use the Private Assets API
Use node.js streams and stream the file line by line. You would have something like this.
Example code that you would need to tweak to your favorite streaming methods:
var Future = Npm.require('fibers/future');
var byline = Npm.require('byline');
var f = new Future;
// create stream in whatever way you like
var instream = fs.createReadStream(...);
var stream = byline.createStream(instream);
// run stream handling line-by-line events asynchronously
stream.on('data', Meteor.bindEnvironment(function (line) {
if (line) console.log(line)
else future.return();
}));
// await on the future yielding to the other fibers and the line-by-line handling
future.wait();

Asynchronous multipart uploads to Amazon S3 with ASP.NET

I am able to initiate asynchronous uploads to S3, however they are somehow not ending up as a file inside my S3 bucket and I see an error 'WithPartETags cannot be empty'. Here is the complete code
InitiateMultipartUploadRequest initRequest =
new InitiateMultipartUploadRequest()
.WithBucketName(existingBucketName)
.WithKey(Path.Combine(S3Path + "/", finfo.Name));
InitiateMultipartUploadResponse initResponse =
s3Client.InitiateMultipartUpload(initRequest);
// 2. Upload Parts.
long contentLength = finfo.Length;
long partSize = 15728640;//52428800-50MB 104857600- 100 MB - 5242880 - 5 MB
try
{
long filePosition = 0;
for (int i = 1; filePosition < contentLength; i++)
{
// Create request to upload a part.
UploadPartRequest uploadRequest = new UploadPartRequest()
.WithBucketName(existingBucketName)
.WithKey(Path.Combine(S3Path + "/", finfo.Name))
.WithUploadId(initResponse.UploadId)
.WithPartNumber(i)
.WithPartSize(partSize)
.WithFilePosition(filePosition)
.WithFilePath(finfo.FullName);
// Upload part and add response to our list.
//uploadResponses.Add(s3Client.UploadPart(uploadRequest));
IAsyncResult ar = s3Client.BeginUploadPart(uploadRequest, null, null);
ListObj.Add(new ThreadList() { _iasyncResult = ar });
filePosition += partSize;
Console.WriteLine("Length Written - " + filePosition + " .Content Length - " + contentLength);
}
bool uploadsComplete = false;
while (!uploadsComplete)
{
bool individualuploadscomplete = true;
foreach (var obj in ListObj)
{
if (!obj._iasyncResult.IsCompleted)
{
individualuploadscomplete = false;
break;
}
}
if (individualuploadscomplete)
{
uploadsComplete = true;
}
}
foreach (var obj in ListObj)
{
s3Client.EndUploadPart(obj._iasyncResult);
}
//// Step 3: complete.
CompleteMultipartUploadRequest compRequest =
new CompleteMultipartUploadRequest()
.WithBucketName(existingBucketName)
.WithKey(Path.Combine(S3Path + "/", finfo.Name))
.WithUploadId(initResponse.UploadId);
//.WithPartETags(uploadResponses);
CompleteMultipartUploadResponse completeUploadResponse =
s3Client.CompleteMultipartUpload(compRequest);
Not sure why you have the setting of the PartETags commented out for the complete multipart upload call but you need to add that code back in. Also when you are calling the EndUploadPart method you need to capture that UploadResponse that comes back from that.
You also might want to look into the TransferUtility found in the Amazon.S3.Transfer namespace. Its upload methods are designed to handle what you are attempting to accomplish for large objects, see Using the High-Level .NET API for Multipart Upload for details and example snippets.

DOS based printing through ASP.NET

Well my situation is like this:
I am generating a report as a text file at the server which needs to be printed using DOS mode on a dot matrix printer. I want to avoid Windows printing because it would be too slow. Is there a way in ASP.NET through which I can carry out DOS based printing as it is best suited for Dot matrix printers. I have scoured the net but could not come across any solution or pointers. Does any body have any pointers/solutions which they might have implemented or stumbled across.
This application is a Web based application.
Thanx.
If I understand you right, one option is to execute a batch file that would do the actual printing from ASP.NET. From here: (Obviously, you can omit some of the code writing the output to the page)
// Get the full file path
string strFilePath = “c:\\temp\\test.bat”;
// Create the ProcessInfo object
System.Diagnostics.ProcessStartInfo psi = new System.Diagnostics.ProcessStartInfo("cmd.exe");
psi.UseShellExecute = false;
psi.RedirectStandardOutput = true;
psi.RedirectStandardInput = true;
psi.RedirectStandardError = true;
psi.WorkingDirectory = “c:\\temp\\“;
// Start the process
System.Diagnostics.Process proc = System.Diagnostics.Process.Start(psi);
// Open the batch file for reading
System.IO.StreamReader strm = System.IO.File.OpenText(strFilePath);
// Attach the output for reading
System.IO.StreamReader sOut = proc.StandardOutput;
// Attach the in for writing
System.IO.StreamWriter sIn = proc.StandardInput;
// Write each line of the batch file to standard input
while(strm.Peek() != -1)
{
sIn.WriteLine(strm.ReadLine());
}
strm.Close();
// Exit CMD.EXE
string stEchoFmt = "# {0} run successfully. Exiting";
sIn.WriteLine(String.Format(stEchoFmt, strFilePath));
sIn.WriteLine("EXIT");
// Close the process
proc.Close();
// Read the sOut to a string.
string results = sOut.ReadToEnd().Trim();
// Close the io Streams;
sIn.Close();
sOut.Close();
// Write out the results.
string fmtStdOut = "<font face=courier size=0>{0}</font>";
this.Response.Write(String.Format(fmtStdOut,results.Replace(System.Environment.NewLine, "<br>")));
The answer from BobbyShaftoe is correct. Here's a pedantic version of it:
public static void CreateProcess(string strFilePath)
{
// Create the ProcessInfo object
var psi = new ProcessStartInfo("cmd.exe")
{
UseShellExecute = false,
RedirectStandardOutput = true,
RedirectStandardInput = true,
RedirectStandardError = true,
WorkingDirectory = "c:\\temp\\"
};
// Start the process
using (var proc = Process.Start(psi))
{
// Attach the in for writing
var sIn = proc.StandardInput;
using (var strm = File.OpenText(strFilePath))
{
// Write each line of the batch file to standard input
while (strm.Peek() != -1)
{
sIn.WriteLine(strm.ReadLine());
}
}
// Exit CMD.EXE
sIn.WriteLine(String.Format("# {0} run successfully. Exiting", strFilePath));
sIn.WriteLine("EXIT");
// Read the sOut to a string.
var results = proc.StandardOutput.ReadToEnd().Trim();
// Write out the results.
string fmtStdOut = "<font face=courier size=0>{0}</font>";
this.Response.Write(String.Format(fmtStdOut,results.Replace(System.Environment.NewLine, "<br>")));
}
}

Resources