how to decode this string properly in java and output to console? - base64

I have a base64 string encoded in some way, I need to decode it in java, how to do so.
here is the string:
SH78d5yrslxNffkumK+wUR5/riXI+bMPS3f6cJiq5ggYfv0nnPq3Wxws+XCZrrFYFy3+IZimtwobfvEjzfnmXA==

The Java documentation base64 decoding can be found here: base64 encoding/decoding
An example of it in use would look something like:
byte[] decodedBytes = Base64.getDecoder().decode(encodedString);
String decodedString = new String(decodedBytes);
A good jump-off point for understanding base64 encoding/decoding can be found here: understanding base64 encoding

Related

Converting a nodejs buffer to string and back to buffer gives a different result in some cases

I created a .docx file.
Now, I do this:
// read the file to a buffer
const data = await fs.promises.readFile('<pathToMy.docx>')
// Converts the buffer to a string using 'utf8' but we could use any encoding
const stringContent = data.toString()
// Converts the string back to a buffer using the same encoding
const newData = Buffer.from(stringContent)
// We expect the values to be equal...
console.log(data.equals(newData)) // -> false
I don't understand in what step of the process the bytes are being changed...
I already spent sooo much time trying to figure this out, without any result... If someone can help me understand what part I'm missing out, it would be really awesome!
A .docXfile is not a UTF-8 string (it's a binary ZIP file) so when you read it into a Buffer object and then call .toString() on it, you're assuming it is already encoding as UTF-8 in the buffer and you want to now move it into a Javascript string. That's not what you have. Your binary data will likely encounter things that are invalid in UTF-8 and those will be discarded or coerced into valid UTF-8, causing an irreversible change.
What Buffer.toString() does is take a Buffer that is ALREADY encoded in UTF-8 and puts it into a Javascript string. See this comment in the doc,
If encoding is 'utf8' and a byte sequence in the input is not valid UTF-8, then each invalid byte is replaced with the replacement character U+FFFD.
So, the code you show in your question is wrongly assuming that Buffer.toString() takes binary data and reversibly encodes it as a UTF8 string. That is not what it does and that's why it doesn't do what you are expecting.
Your question doesn't describe what you're actually trying to accomplish. If you want to do something useful with the .docX file, you probably need to actually parse it from it's binary ZIP file form into the actual components of the file in their appropriate format.
Now that you explain you're trying to store it in localStorage, then you need to encode the binary into a string format. One such popular option is Base64 though it isn't super efficient (size wise), but it is better than many others. See Binary Data in JSON String. Something better than Base64 for prior discussion on this topic. Ignore the notes about compression in that other answer because your data is already ZIP compressed.

issue decoding base64 text: prefix "77u_" causes junk output

I'm currently trying to decode my base64 encoded string. The issue I'm facing is it gives me junk decoded result but when I remove 77u_ in front from it, the decode result will get fine. My question is that, is there any other way to decode it?
Following is my encoded string:
77u_RGF0ZSxUeXBlLENvbmZpcm1hdGlvbiBDb2RlLFN0YXJ0IERhdGUsTmlnaHRzLEd1ZXN0LExpc3RpbmcsRGV0YWlscyxSZWZlcmVuY2UsQ3VycmVuY3ksQW1vdW50LFBhaWQgT3V0LEhvc3QgRmVlLENsZWFuaW5nIEZlZSxHcm9zcyBFYXJuaW5ncyxPY2N1cGFuY3kgVGF4ZXMNCjEwLzMwLzIwMTgsUmVzZXJ2YXRpb24sSE05U1BUOUNYQywxMC8yOS8yMDE4LDMsQWFyb24gSmFibG9uc2tpLEVsZWdhbnQgUHJpdmF0ZSBSb29tIGluIGEgTW9kZXJuIEx1eHVyeSBCdW5nYWxvdywsLFVTRCwxNDUuNTAsLDQuNTAsMzAuMDAsMTUwLjAwLDIxLjANCjEwLzI2LzIwMTgsUmVzZXJ2YXRpb24sSE0zM0gyWEU0SiwxMC8yNS8yMDE4LDMsWWIgQmFiaWUsRWxlZ2FudCBQcml2YXRlIFJvb20gaW4gYSBNb2Rlcm4gTHV4dXJ5IEJ1bmdhbG93LCwsVVNELDE3NC42MCwsNS40MCwzMC4wMCwxODAuMDAsMjUuMg0KMTAvMjIvMjAxOCxSZXNlcnZhdGlvbixITVdSRk1KNU1LLDEwLzIxLzIwMTgsNCxHYWJyaWVsbGEgRGFsdG9uLEVsZWdhbnQgUHJpdmF0ZSBSb29tIGluIGEgTW9kZXJuIEx1eHVyeSBCdW5nYWxvdywsLFVTRCwxNjQuOTAsLDUuMTAsMzAuMDAsMTcwLjAwLDIzLjgNCjEwLzE4LzIwMTgsUmVzZXJ2YXRpb24sSE1NUFk4UDJRRiwxMC8xNy8yMDE4LDQsUMOpdGVyIFZhcmdhLEVsZWdhbnQgUHJpdmF0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5MC4wMCwxMi42DQoxMC8wNC8yMDE4LFJlc2VydmF0aW9uLEhNQzNGTk04RFcsMTAvMDMvMjAxOCw2LFNhbmRybyBDemVrYWksRWxlZ2FudCBQcml2YXRlIFJvb20gaW4gYSBNb2Rlcm4gTHV4dXJ5IEJ1bmdhbG93LCwsVVNELDM3OC4zMCwsMTEuNzAsMzAuMDAsMzkwLjAwLDU0LjYNCjEwLzAxLzIwMTgsUmVzZXJ2YXRpb24sSE1UUzk0Uk1FMywwOS8zMC8yMDE4LDMsQnJhbmRlbiBIaWNrcyxFbGVnYW50IFByaXZhdGUgUm9vbSBpbiBhIE1vZGVybiBMdXh1cnkgQnVuZ2Fsb3csLCxVU0QsMTE2LjQwLCwzLjYwLDMwLjAwLDEyMC4wMCwxNi44DQo
It's the _ that messes up the result. Everything else is perfectly fine base64 encoded. The base64 code table doesn't contain a _, it is a substitiution character for / in base64url encoding.
When you replace _ with / the decoding works fine.
When I tested it on https://www.base64decode.org/ and chose ASCII as the source character set, I got  in front of the text, which is the byte order mark for UTF-8. When I changed to utf-8, there was nothing visible in front of the text.
A short test in node.js also proves that '77u/' is indeed the base64 code of the BOM:
var messageB64 ='77u/'
var buf = Buffer.from(messageB64, 'base64');
console.log(buf) // output: <Buffer ef bb bf>
Conclusion:
your data is base64url decoded
you should change it back to base64 code before you decode
the extra characters are a harmless byte order marker which is invisible if you use utf-8 encoding.

I have encoded string but i don't know what type is it

I have two encoded strings that used same encoding method but i don't know what type it is.
I have tried using base64 decode but it didn't work.
This is the first encoded string I have 3qpY0Vw86MZykGfqc7jnVg==
This is the second encoded string I have nB6dtl3iA5IE1Z+g9SpBrw==
They are using same encoding method.
I want to know what type of encoding that used in that strings. Also I want to know how to decode it.
Those base64 payload may be containing something else than a string, like a raw binary, an image, a ciphered payload, etc. that can't be displayed as text.
base64 is not exclusively used to encode text.
For example to save to a file:
$ printf %s 'iVBORw0KGgoAAAANSUhEUgAAAGAAAABgCAYAAADimHc4AAAAAXNSR0IArs4c6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nxEQ4DMCAnxGQIDPCAjwGQEBPiMgwGcEBPiMgACfERDgMwICfEZAgM8ICPAZAQG+Avgv4WYH0htgNTQAAAAASUVORK5CYII=' | base64 -d > stackoverflow.png

Python3 Base64 decode of a var containing ==

So Ive got a string of:
YDNhZip1cDg1YWg4cCFoKg==
that needs to be decoded using Pythons Base64 module.
Ive written the code
import base64
test = 'YDNhZip1cDg1YWg4cCFoKg=='
print(test)
print(base64.b64decode(test))
which gives the answer
b'`3afup85ah8p!h'
when, according to the website decoders Ive used, its really
`3afup85ah8p!h
Im guessing that its decoding the additional quotes.
Is there some way that I can save this variable with a delimiter, as another type of variable, or run the b64encode on a section of the string as slice doesnt seem to work?
b' is Python's way of delimiting data from bytes, see: What does the 'b' character do in front of a string literal?
i.e., it is decoding it correctly.

Node.js - how to decode a base64 using node.js cypto

I want to use the cypto package to decode a base64 string. From the docs,
http://nodejs.org/docs/v0.3.1/api/crypto.html#hash.digest
I am rather confused on how do do it.
e.g. mystring - base64(string).decode()

Resources