Base64 Encode Algorithm
The Base64 encode algorithm converts any data into plain text. Technically, it can be said that it converts eight-bit bytes into six-bit bytes. To understand how the encoding algorithm works, check the example below that describes step by step how to manually encode strings to Base64 (if you are looking for an automatic converter, use the Base64 online encoder).
For example, you have the “ABC” string and want to convert it to Base64:
-
First, you need to split the string letter by letter. Thus, you got 3 groups:
A
B
C
-
Next you need to convert each group to binary. To do this, for each letter you need to find the corresponding binary value in the ASCII table. Thus, now you have 3 groups of ones and zeros:
01000001
01000010
01000011
-
Now concatenate all the binary values together (that is, glue all the groups along and make sure you get a total of 24 characters):
010000010100001001000011
-
Then, divide the resulting string into groups so that each one has 6 characters (if the last group has less than 6 characters, you need to fill it with zeros until it reaches the desired length). Well and good, now you have 4 groups:
010000
010100
001001
000011
-
At this step you have to convert six-bit bytes into eight-bit bytes. To do this, prepend the prefix “00” (two zeros) in front of each group:
00010000
00010100
00001001
00000011
-
There you have to convert each group from binary to decimal by finding its corresponding decimal value in the ASCII table. If you did everything right, each group will be transformed into its integer number as follows:
16
20
9
3
-
Integer numbers obtained in the previous step are called “Base64 indices”. They are easy to remember, because it is a zero-based numbering, where each index corresponds to a Latin letter. It starts with the letter “A” in alphabetical order (i.e., A=0, B=1, C=2, D=3, and so on). For complete list, see Base64 Characters Table. So, matching indexes, convert them to corresponding letters:
Q
U
J
D
-
The final chord, concatenate all letters to get the Base64 string:
QUJD
To summarize, you learned that encoding “ABC” to Base64 yields the result “QUJD”. As you can see, this is a very simple process and you can encode text to Base64 even by hand. I hope that you managed to get the right encoding result. Otherwise, let me know and I will try to help you.
If you need more step by step examples, use the form below to get encoding instructions for custom strings (once you submit the form, the article above will be updated accordingly in real time):
Comments (41)
I hope you enjoy this discussion. In any case, I ask you to join it.
I am stuck in encoding from hex to base64 in Android. Would you please show me how? I tried
Base64.encode(hexString.getBytes(), Base64.DEFAULT); and Base64.encodeToString but am not successful yet.
Best regards,
Ian
https://stackoverflow.com/questions/7372268/how-to-convert-hex-to-base64
rtrim()
function). If you plan to replace it (=
) with another character after encoding, you must reverse the process before decoding.Convert stuff lower or equals to 127 is fine, but how about higher than that? lets say the character '¤', according to ASCII Table the corresponding binary value is `10100100` (which is 145 in decimal) but in here I got `1100001010100100`, what's happening here?
how do you convert any character that has a decimal value higher than 127?
Thank you for your message and I'm sorry for confusing you.
The problem is that the extended ASCII table (decimal > 127) and the encoder provided on this page use different character encodings. Instead of the extended ASCII table, please use the converter provided on the ASCII Table page.
Meanwhile I'll try to figure out how to make it clear for everyone (perhaps the best option is to remove the extended ASCII table, because it can also be confusing at decoding).
By the way, it doesn’t matter the character encoding as long as you use the same character set for encoding and decoding. For example, if you use Windows-1252 table to encode
A¤B
to Base64 you’ll get theQaRC
result. And you can decode it back to the original string if you still use Windows-1252. But if you try to decodeQaRC
using the CP850 character set, it will be decoded asAñB
. For more differences, try to decodeQaRC
using the Character Encoding Detection tool.So I've been really interested in the Advanced Encryption Standard, but it's kind of hard because I'm not the best coder ever. When I've discovered Base64, I've been really interested in it. But is there a way to encode with a password kind of like AES (Advanced Encryption Standard)? I read that Base64 isn't the best encoding algorithm as people can easily decode it.
Thanks
Base64 is only an encoding algorithm which is based on a defined character set table, therefore it is very easily completely reversible.
AES on the other hand, is an actual cryptographic algorithm used for real encryption and decryption. Due to the fact that the AES decryption algorithm requires the **same exact password or "secret" data as used during the encryption process** to return the encrypted AES output back into it's original un-encrypted/plaintext input data, then that's something that is considered to be a
Symmetric Encryption Algorithm
. When you can decrypt the encrypted data with the same key that was used to encryot it in the first place, it's a symmetric algorithm.With that all being said, if you are looking to store passwords in a secure way in which they cannot be reversed, you shouldn't use any two-way cryptographic algorithm at all.. and in that case you should only be using a
one-way cryptographic hashing algorithm
.One of the most popular and widely used hashing algorithm (that is still considered to be "highly secure" as of November 7th, 2024) is known as SHA-256.
However, security basics suggest that you use a randomly generated "salt" and "pepper" when you hash sensitive data such as passwords when you store them - which basically means to perform the hashing function like this:
SALT = (Uniquely generated random string or data to *prepend* [or add in front of] whatever INPUT it is that you are hashing)
INPUT = (The password or data you are wanting to store securely)
PEPPER = (Uniquely generated random string or data to *append* [or add to the end of] whatever INPUT it is that you are hashing)
And then finally perform your hash function as:
HASH = sha256(SALT+INPUT+PEPPER)
Finally, let me end this comment by saying even with all of that, any hash can still be brute-forced in order to attempt to retrieve the original input. In the scenario above, a person would have to know the final encrypted HASH value itself, along with both the SALT and PEPPER values as well.
To combat this problem, there are even stronger algorithms that make brute-forcing much harder to do as the algorithm computations are much more resource intensive (making them "slower" therefore it would take an extremely long time to brute-force a hash to attempt to get the original INPUT value) when compared to SHA-256 alone.
Some examples of the *even more secure than SHA-256 algorithm* that have strong protection against brute-force and dictionary-based attacks would be:
Argon2id
bcrypt
PBKDF2/SHA-256 - (NIST recommends this is the strongest according to some Google searches)
They all also automatically salt the INPUT/passwords as well, and their algorithm settings can be customized to your specific needs.
Personally, I use Argon2, and even though the algorithm salts the passwords automatically, I prefer to do it again myself as well. You can never be too careful when you're storing user credentials! :)
que não tem quando vejo na sua base a mesma imagem
Function Base64Encode(sText)
Dim oXML, oNode
Set oXML = Server.CreateObject("MSXML2.DomDocument")
Set oNode = oXML.CreateElement("b64")
oNode.DataType = "bin.base64"
oNode.Text = Mid(base64String, InStr(base64String, ",") + 1)
oNode.nodeTypedValue = Stream_StringToBinary(sText)
Base64Encode = oNode.text
Set oNode = nothing
Set oXML = nothing
End Function
Private Function Stream_StringToBinary(Text)
Const adTypeText = 2
Const adTypeBinary = 1
Dim BinaryStream ' como novo fluxo
Set BinaryStream = CreateObject("ADODB.Stream")
'Specify stream type - we want To save text/string data.
BinaryStream.Type = adTypeText
BinaryStream.CharSet = "us-ascii"
'Open the stream And write text/string data To the object
BinaryStream.Open
BinaryStream.WriteText = Text
'Change stream type To binary
BinaryStream.Position = 0
BinaryStream.Type = adTypeBinary
'Ignore first two bytes - sign of
BinaryStream.Position = 0
'Open the stream And get binary data from the object
Stream_StringToBinary = BinaryStream.Read
Set BinaryStream = nothing
End Function
const raw = atob(str);
let result = '';
for (let i = 0; i < raw.length; i++) {
const hex = raw.charCodeAt(i).toString(16);
result += (hex.length === 2 ? hex : '0' + hex);
}
return result.toUpperCase();
}
console.log(base64ToHex("oAAABTUAAg=="))
47 ed 79 20 80 31 c5 57 ae e7 5e 4c 56 05 c3 af G.y .1.W..^LV...
b4 df dc bd 3b 44 98 20 c6 e4 0f 47 a8 30 53 5d ....;D. ...G.0S]
b4 63 bc f3 58 c9 aa 50 ad d3 e7 f2 f8 51 4d d2 .c..X..P.....QM.
3d ba 14 c3 89 b8 6d 39 61 c4 53 60 16 2f 34 45 =.....m9a.S`./4E
61 8b 01 d6 38 bf 8a f5 50 8f 60 01 c4 6b 0c 3b a...8...P.`..k.;
25 5c 3c b9 49 23 78 db 5d 09 5e ad f5 70 50 cd %\<.I#x.].^..pP.
63 af eb 62 a7 c0 88 ce 77 66 b3 73 14 28 b0 6c c..b....wf.s.(.l
03 aa 4a d6 b4 01 9c b4 fd 56 27 96 85 10 2b f5 ..J......V'...+.
8d 0f f1 0d f3 dc 89 ac df 09 4d de 2e a3 1e f9 ..........M.....
5f 65 71 63 f0 c9 f3 5d 11 bb d6 9e a5 5a 2b 47 _eqc...].....Z+G
b3 84 d2 c3 5a 09 5b fb 41 5a 52 a2 18 65 0a 6d ....Z.[.AZR..e.m
86 32 fc f5 97 7e 0f d6 bf d7 7d 0d 1d 3e 4b 98 .2...~....}..>K.
ca 61 dc a9 06 2d 0e 5f d0 53 1d 58 c6 18 73 3e .a...-._.S.X..s>
60 0a 83 95 70 9f 7f f4 7f df 49 62 c8 47 0a 8c `...p.....Ib.G..
10 34 4b 43 d9 95 07 fd a8 0b 5f d9 4d 71 51 e9 .4KC......_.MqQ.
c7 01 cd 06 63 df bf ec 59 70 15 f6 15 a2 af fa ....c...Yp......
76 7c 18 30 fc c9 fb 5b 28 8b 67 a4 3a fc 1c 14 v|.0...[(.g.:...
6a cd 29 70 43 7e 3f a6 3e 89 2f 4e 7c 3b 61 cd j.)pC~?.>./N|;a.
dd 7b 6e 1f e3 68 95 fa 68 73 dc f7 64 5f 6c 54 .{n..h..hs..d_lT
07 09 a4 f1 7c 25 35 ce 97 5a fd f3 0c 81 bc d2 ....|%5..Z......
22 c8 80 92 ed c2 ee 05 3d d3 05 e8 ba 33 e8 f8 ".......=....3..
a6 1d 8b 66 cb 4a 61 95 ac cb a5 01 7b f5 af ea ...f.Ja.....{...
36 8f 07 e9 38 93 79 43 8b c6 15 18 e1 4d 9c cc 6...8.yC.....M..
eb 2b ce 0f 7d cd 46 e5 b6 55 dc 41 d2 42 4e bd .+..}.F..U.A.BN.
ae de 15 3e 7c 3d 56 99 c4 68 19 e1 4e ca 2d 0e ...>|=V..h..N.-.
d4 96 52 b3 84 ae b0 8d 6d 15 30 41 6d 5b 50 22 ..R.....m.0Am[P"
70 0e 6c a3 be 31 f3 28 0f 66 13 41 7e a6 a9 40 p.l..1.(.f.A~..@
78 61 d0 01 2a 34 81 b7 8f e9 7f ae 50 b5 0e 15 xa..*4......P...
08 5b 22 0e e6 99 63 f2 25 a2 d2 db 9f 7f e1 00 .["...c.%.......
7a bb 4d 45 1d 0a 58 d0 cf fc 95 4f 33 4b 68 77 z.ME..X....O3Khw
70 b3 92 f7 5a 23 a5 52 48 b5 6a 55 02 99 8e b1 p...Z#.RH.jU....
9b e1 22 57 85 a9 ea dd 28 de bd ca 9d 67 f3 f0 .."W....(....g..
fa 8f 4c 62 d2 22 52 3a c7 b1 eb f6 e0 33 de 09 ..Lb."R:.....3..
1e 9b 07 a4 98 9c 91 12 b3 81 73 44 2c 70 46 5b ..........sD,pF[
b3 80 80 30 fd b5 9a af 8b 15 09 97 d6 77 61 55 ...0.........waU
25 46 00 06 e0 32 1e 48 99 ed 98 f8 8a ab f8 a2 %F...2.H........
dc 89 f4 04 bf 61 e1 a1 e3 3f c9 35 f0 ce 17 9e .....a...?.5....
7b 3d 5a 27 25 57 99 6f ae 72 81 b6 64 c5 81 1f {=Z'%W.o.r..d...
76 a4 7b 21 b1 93 45 42 b8 f6 8a 67 b5 7d c1 2f v.{!..EB...g.}./
93 78 c5 28 bb c3 50 3f 11 15 f3 3b cb 1c 17 40 .x.(..P?...;...@
a6 57 e4 ef 9a 9b 7b dc 07 46 f5 5c ac 78 f8 65 .W....{..F.\.x.e
5e b2 85 23 8c c5 45 83 7e b8 2e d7 fe 89 60 bb ^..#..E.~.....`.
1d 30 1b c8 88 aa 51 2f 48 54 ff 46 2b 9e 17 a3 .0....Q/HT.F+...
80 b2 ce 4b ed bc 19 e8 60 3b 16 9b 18 a1 77 60 ...K....`;....w`
98 fa 7d 5b bb e6 d2 c1 0f aa 0b ac 09 7a f7 f0 ..}[.........z..
32 fe 9a 1c d2 cb 89 9e 94 74 6f f8 3f 12 ef 78 2........to.?..x
d8 56 3d c9 10 44 94 26 f2 4b 3f 84 a3 01 e8 e5 .V=..D.&.K?.....
89 86 a5 03 cd 04 99 db 01 27 2b 65 0b a0 87 5e .........'+e...^
8c 96 e0 d7 d7 39 90 b0 80 38 f1 e8 e6 ae d3 45 .....9...8.....E
f3 0e 2c 15 48 66 02 ce bc 61 25 0c ..,.Hf...a%.
Or this, same as above just different for some reason.....
'!����u
G�y �1�W��^LVï��ܽ;D� ��G�0S]�c��XɪP�����QM�=�É�m9a�S`/4Ea��8���P�`�k;%\<�I#x�] ^��pP�c��b����wf�s(�l�Jִ���V'��+���
�܉�� M�.��_eqc���]�֞�Z+G����Z [�AZR�e
m�2���~ֿ�}
>K��aܩ-_�SX�s>`
��p���Ib�G
�4KCٕ��_�MqQ���c߿�Yp����v|0���[(�g�:�j�)pC~?�>�/N|;a��{n�h��hs��d_lT ��|%5ΗZ�����"Ȁ����=��3����f�Ja��˥{���6��8�yC���M���+�}�F�U�A�BN���>|=V��h�N�-ԖR�����m0Am[P"pl��1�(fA~��@xa�*4�����P�["�c�%��۟�