I am trying to transfer AES key generate in js:
var AESkey = forge.random.getBytesSync(16);
exapmle if printed condsole i have "§½üå8bdÈP"
but printed in my java server it is "—§‡½Â\u001eĂĽĂĄ8b\u000edĂ?\u0012P›"
(other data are ok)
I tried to get the bytes with Buffer.from(AESkey) but instead of getting a 16 bytes buffer I get a 22/24/25.. buffer lenghts.
In which format can I transfer the key and how can I get my AESkey to that format in js?
A possible solution:
make a base64 encoded string on client side
send base64-encoded string to server
decode base64 in Java to get the byte sequence back
This way the byte sequence can be safely transported.
Related
I am trying to convert image to byte array using only javascript and send to api. I have found this answer but it only shows to convert to base64 then send it to server side. Are there any other ways to convert to byte array using js or I can't just send byte array to api ?
I am typically transferring JSON objects from JavaScript and saving them with PHP.
I then append them to a specific text file like this:
$theFile = fopen("Data/" . FQ . ".txt", "a+");
fwrite($theFile, $data.PHP_EOL);
fclose($theFile);
Can I add code to save this information as an encrypted text file?
Ideally, I want:
All the files on my server to be encrypted
A secret key that is stored on my local computer
To decipher, I would:
Transfer data from server to local computer
Use my local secret key to decipher
I want this so that if my server is compromised, all data is gibberish without the secret key (which is NOT stored on the server anywhere).
You're going to want a symmetric encryption algorithm, such as AES. It turns out that there is a decent JavaScript implementation of it as part of Forge.
https://github.com/digitalbazaar/forge#aes
You'll want to use CBC mode to encrypt the payload and send it to your server.
If you're dead set on keeping this in a text file, you'll have to base64-encode this binary data. Do this server-side. Your client code shouldn't need to know or care how your server is actually storing the data. Plus, you'll save yourself 33% bandwidth, and some client-side CPU.
As a bonus to the base64-encoding, you'll be able to line-delimit the records in your text file.
When you return the data to your client, you should decode the base64 and send them the binary encrypted data. The client will then decrypt it using the key that only it knows.
I have an ArrayBuffer in angularjs that I am trying to send to my backend endpoint in c#, I thought it was a regular byte array but it's not mapping appropriately
What would an ArrayBuffer map to as an object in c# if not a byte[]?.
I have a PDF file represented as that ArrayBuffer but now I would like to send it to the server, or perhaps is there a way to convert that ArrayBuffer to a File type in JavaScript and send it o the endpoint so that I can use something like an IFormFile?.
Thank you
I had a situation of same some long days back. What I did was, I converted the array buffer data to base 64 string in the js side and sent it to the backend c# code using post method,and again I decoded it to array buffer to regenerate the file on server side. By this way I achieved the mapping.
I used this code which helps to convert the array buffer to base 64 string in JS.
and this api of .net framework to convert back the base 64 string to file buffer.
I hope this would help you.
Is there any possibility to have a Restful method (Java EE) that returns back to the client a POJO serialized us bytes (using ByteArrayOutputStream, FlatBuffers or some other library)?
And the client to cast the bytes back to a POJO object by using jquery or javascript in general (us client side)?
What I had found until now, is receiving back a JSON/XML.
Directly converting POJO to bytes won't work as your client (javascript or so) won't know how to convert the bytes back to POJO. What you can do is this. Say your response is objA. The do this. Convert objA to String (JSON maybe) and then Base64 encode it to get byte[]. Put it inside a wrapper object objB. Return objB as a JSON from your service. Your client can just take that byte[] and since it knows that the byte[] is base64 it will be able to get the original information from there.
May I ask the intent of doing this instead of XML/JSON?
I want to send data from Javascript to a WebSocket server and also from a WebSocket server to Javascript.
I want to send this:
Headers
-------
Field 1: 2 byte hex
Field 2: 2 byte hex
Field 3: 4 byte hex
Data
----
Field1 : 2 byte hex
Field1 : 8 byte hex
From Javascript, I can send a two-byte value via
socket = new WebSocket(host);
...
socket.send(0xEF);
But I want to send multiple fields, together...let's say 0xEF, 0x60, and 0x0042.
How do I do this?
And, how to I interpret via Javascript data containing multiple fields coming from the WebSocket server?
You can send data as a string. For example:
socket.send("hello world");
I recommend you to use JSON as data format. You can convert JSON strings directly into objects and vice versa. It's so simple and useful!
You can send data as JSON objects.
socket.send(JSON.stringify({field1:'0xEF', field2:'0x60',field3: '0x0042'}));
Sound like what you are asking is how to send binary data over a WebSocket connection.
This is largely answered here:
Send and receive binary data over web sockets in Javascript?
A bit of extra info not covered in that answer:
The current WebSocket protocol and API only permits strings to be sent (or anything that can be coerced/type-cast to a string) and received messages are strings. The next iteration of the protocol (HyBi-07) supports binary data is currently being implemented in browsers.
Javascript strings are UTF-16 which is 2 bytes for every character internally. The current WebSockets payload is limited to UTF-8. In UTF-8 character values below 128 take 1 byte to encode. Values 128 and above take 2 or more bytes to encode. When you send a Javascript string, it gets converted from UTF-16 to UTF-8. To send and receive binary data (until the protocol and API natively support it), you need to encode your data into something compatible with UTF-8. For example, base64. This is covered in more detail in the answer linked above.