I want to decode the Base64 result of MS-SQL Server in javascript, but I can't find a true solution. I know SQL Server uses UCS-2 encoding, and I have to use the same encoding to decode in javascript.
For example, for MwZEBicGRQY= the encoded result must be سلام.
Do you have any solution to decode that using javascript?
You can first decode your base64 data into an Uint8Array, and then read the resulting data as UTF-16:
const base64_string = "MwZEBicGRQY";
// from https://stackoverflow.com/a/41106346/3702797
// may not be bullet-proof
const arr = Uint8Array.from(atob(base64_string), c => c.charCodeAt(0))
const decoded = new TextDecoder("UTF-16").decode(arr);
console.log(decoded);
Related
After getting byte array encryptedMessageInBytes from AES encryption function call cipher.doFinal in Java, I convert the byte array to base64 like this:
String encryptedMessageInBase64 = new String(Base64.getEncoder().encode(encryptedMessageInBytes));
In JavaScript, I simply do .toString() to the output I get from CryptoJS.AES.encrypt method and I get exact same base64 string. i.e.
var encryptedMessageInBase64 = CryptoJS.AES.encrypt("Message", "Secret Passphrase").toString();
It gives same base64 string as in Java code.
However, in one of the Java source code, they have done like this:
String encryptedMessageInBase64 = Base64.getUrlEncoder().encodeToString(encryptedMessageInBytes);
What shall I do in JavaScript to obtain same base64 string?
Here is answer:
However, in one of the Java source code, they have done like this:
String encryptedMessageInBase64 = Base64.getUrlEncoder().encodeToString(encryptedMessageInBytes);*
Here, basically they have done UrlEncoding instead of Base64 encoding. It is nothing but replacing + and / characters with - and _ characters. Url encoding is when we want to send encoded string over HTTP where it treats these two as special characters this is why we need to replace them with some other characters which are HTTP friendly.
i am trying to convert a small piece of code into javascript, everything is going well except for this part
new String(encode, Charsets.UTF_8);
I suppose this is creating a string from encoded hash, what i have in javascript is
const encoderUTF8 = new TextEncoder('utf-8');
const utf8 = new Uint8Array(encode.length);
const encoded = encoderUTF8.encodeInto(encode, utf8);
When i run it i get an error that says The "src" argument must be of type string. Received an instance of Buffer .
The encode variable is a strange hash looking like this dD${��d�N��t�\���P� which is generated like so
const encode = Buffer.from(hash, 'base64');
Can someone please help me get this part together?
All I need is to figure out that new String part.
I wrote one of the functions wrong, i should have base64 encoded instead of decoding it
const encode = Buffer.from(hash, 'base64');
to this
const encode = Buffer.from(hash).toString('base64').toString('utf-8');
Now its perfect and matches the string length and works perfectly.
I am receiving the content of a zip file (from an API) as a Base64-encoded string.
If I paste that string into Notepad++ and go
Plugins > MIME Tools > Base64 Decode
and save it as test.zip, it becomes a valid zip file, I can open it.
Now, I am trying to achieve the same thing in JavaScript.
I have tried atob(), and probably everything mentioned in the answers here and the code from Mozilla doc.
atob produces a similar content, but some characters are decoded differently (hence becomes an invalid zip file). The other methods throw an invalid URI error.
How can I reproduce Notepad++ behaviour in JavaScript?
The window.atob is only good for decoding data which fits in a UTF-8 string. Anything which cannot be represented in UTF-8 string will not be equal to its binary form when decoded. Javascript, at most, will try encoding the resultant bytes in to UTF-8 character sequence. This is the reason why your zip archive is rendered invalid eventually.
The moment you do the following:
var data = window.atob(encoded_data)
... you are having a different representation of your data in a UTF-8 string which is referenced by the variable data.
You should decode your binary data directly to an ArrayBuffer. And window.atob is not a good fit for this.
Here is a function which can convert base64 encoded data directly in to an ArrayBuffer.
As mentioned, do not use atob directly for decoding Base64 encoded zip files. You can use this function mentioned in https://stackoverflow.com/a/21797381/3508516 instead.
function _base64ToArrayBuffer(base64) {
var binary_string = window.atob(base64);
var len = binary_string.length;
var bytes = new Uint8Array(len);
for (var i = 0; i < len; i++) {
bytes[i] = binary_string.charCodeAt(i);
}
return bytes.buffer;
}
Client side code (angularjs controller)
var p="gemüse8";
var pb= $base64.encode(p); // pb= Z2Vt/HNlOA==
from server side(C#)
string b64="Z2Vt/HNlOA==";
byte[] data = Convert.FromBase64String(b64);
string decodedString = Encoding.UTF8.GetString(data);
result showing "gem�se8"
How can i properly decode this string?
Whatever $base64.encode is seems to be using ISO-8859-1 (or similar) encoding rather than UTF - which probably needs addressing.
As it stands use:
string decodedString = Encoding.GetEncoding("iso-8859-1").GetString(data);
I am using NodeJS to interact with Amazon Web Services (specifically s3). I am attempting to use Server side encryption with customer keys. Only AES256 is allowed as an encryption method. The API specifies that the keys be base64 encoded.
At the moment I am merely testing the AWS api, I am using throwaway test files, so security (and secure key generation) are not an issue at the moment.
My problem is as follows: Given that I am in posession of a 256bit hexadecimal string, how do I obtain a base64 encoded string of the integer that that represents?
My first instinct was to first parse the Hexadecimal string as an integer, and convert it to a string using toString(radix) specifying a radix of 64. However toString() accepts a maximum radix of 36. Is there another way of doing this?
And even if I do this, is that a base64 encoded string of 256bit encryption key? The API reference just says that it expects a key that is "appropriate for use with the algorithm specified". (I am using the putObject method).
To convert a hex string to a base64 string in node.js, you can very easily use a Buffer;
var key_in_hex = '11223344556677881122334455667788'
var buf = new Buffer(key_in_hex, 'hex')
var str = buf.toString('base64')
...which will set str to the base64 equivalent of the hex string passed in ('112233...')
You could also of course combine it to a one liner;
var key_in_hex = '11223344556677881122334455667788'
var str = new Buffer(key_in_hex, 'hex').toString('base64')