Linux hex to base64

How can I convert from hex to base64?

Use xxd with the -r argument (and possibly the -p argument) to convert from hex to plain binary/octets and base64 to convert the binary/octet form to base64.

cat file.dat | xxd -r -p | base64 

For a string of hex numbers:

echo "6F0AD0BFEE7D4B478AFED096E03CD80A" | xxd -r -p | base64 

To prove that this is correct, you can perform the reverse operation and verify that the output of the reverse operation matches the input you provided to the above command. Eg. echo -n $UUID | sed ‘s/-//g’ | xxd -r -p | base64 | base64 —decode | xxd -u | cut -d ‘ ‘ -f 2-9 | sed ‘s/ //g’

The important info here is the «-n» option of echo (don’t exist on all plateform) which avoids the newline.

Well, it depends on the exact formatting of your data. But you can do it with a simple shell scripts:

 echo "obase=10; ibase=16; `cat in.dat`" | bc | base64 > out.dat 

Modify as needed depending on your data.

That will convert the string of decimal digits. It’s not clear if this is what the OP wants or if he has hex digits and wants the bytes they represent converted to base64.

Well, if your hex data is the hex view of a file, just attach the file to a outlook or thunderbird message and then save the message to somewhere. Then open the file with a text editor and see B64 code 🙂

It functions on Windows, but I think it is a universal way since saving as .EML the attachment is encoded to B64.

You must log in to answer this question.

Linked

Hot Network Questions

Subscribe to RSS

To subscribe to this RSS feed, copy and paste this URL into your RSS reader.

Site design / logo © 2023 Stack Exchange Inc; user contributions licensed under CC BY-SA . rev 2023.7.14.43533

By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.

Источник

How to convert hex to base64?

The hexadecimal representation of data is a widely used format to represent binary data, while base64 is another widely used encoding that represents binary data as ASCII text. In some cases, it may be necessary to convert data represented in hexadecimal format to base64 encoding, for example, to embed binary data in a text-based file format or to encode binary data to be transported over a text-based communication protocol. In this article, we will explore two methods for converting hex to base64 in programming.

Method 1: Using a Library Function

To convert hex to base64 using a library function, you can use the base64 library in Python. Here are the steps to do it:

hex_string = "48656c6c6f20576f726c64" hex_bytes = bytes.fromhex(hex_string)
base64_bytes = base64.b64encode(hex_bytes)
base64_string = base64_bytes.decode('utf-8')
import base64 hex_string = "48656c6c6f20576f726c64" hex_bytes = bytes.fromhex(hex_string) base64_bytes = base64.b64encode(hex_bytes) base64_string = base64_bytes.decode('utf-8') print(base64_string)
"SGVsbG8gV29ybGQ method-2-implementing-custom-conversion-logic">Method 2: Implementing Custom Conversion Logic

here's an example code to convert hex to base64 using custom conversion logic:

import base64 def hex_to_base64(hex_string): # convert hex string to bytes hex_bytes = bytes.fromhex(hex_string) # convert bytes to base64 string base64_string = base64.b64encode(hex_bytes).decode('utf-8') return base64_string
  1. we start by importing the base64 module, which provides functions for working with base64-encoded data.
  2. we define a function called hex_to_base64 that takes a hex string as input.
  3. we convert the hex string to bytes using the bytes.fromhex method.
  4. we encode the bytes as base64 using the base64.b64encode method.
  5. we decode the resulting bytes as a utf-8 string using the decode method.
  6. we return the base64-encoded string.

that's it! you can use this function to convert any hex string to base64. here's an example usage:

>>> hex_to_base64('48656c6c6f20576f726c64') 'sgvsbg8gv29ybgq='

in this example, we're converting the hex string '48656c6c6f20576f726c64' (which represents the ascii string "hello world") to base64. the result is the base64-encoded string 'sgvsbg8gv29ybgq=' .

Источник

How do I get hex blocks from a base 64 encoded string?

In this article and this XKCD, they both show the password data as groupings of hexadecimal. However, in the file it's base64 encoded. What could I use to match that output with bash scripting? I've tried: echo -n "7WkoOEfwfTTioxG6CatHBw==" | base64 -d echo -n "7WkoOEfwfTTioxG6CatHBw= https://imgs.xkcd.com/comics/encryptic.png" alt="xkcd Encryptic">

2 Answers 2

If I understand this correctly, I think the requirement is to translate a base64 encoded string to a hex string in blocks of 8 bytes (16 hex digits). If so, od -t x8 -An , after the base64 decoding will get you there:

$ echo -n "7WkoOEfwfTTioxG6CatHBw= mt24">

P.S. How did two independent sources 'agree' on a rather specific base 64 decoding to hex string blocks of 8 bytes, and present it the same way?

@Ehryk - I don't think that there is much to have to come to agreement on. A string of ascii characters maps in a 1:1 fashion to its base64 encoding. Similarly a string of ascii characters maps in a 1:1 fashion to its hex string representation. Therefore by association, the base64 encoding maps in a 1:1 fashion to its hex string representation.

. and then they both separated them into 8 byte/16 digit blocks. I suspect this has something to do with the underlying 3DES encryption, but it seems like this was 'intuitively understood' by both authors. Is it this obviously 'the thing to do' somehow?

Yes - 3des blocksize is 64bits (or 8bytes or 16hex digits or 8*4/3 base64 "digits") en.wikipedia.org/wiki/Triple_DES

Источник

Hexadecimal to base64 conversion

I'm currently working on an hex->base64 converter. How am I suppsoed to handle an odd number of hex-digits? What I did until now is that every hex-digit is an 4-bit number, so 2 hex-digits are 1 byte. If I encounter an odd number of hex-digits do I simply fill the rest of the uncomplete byte with 0? Or am I supposed to return an error?

We have no context here - who is in charge of the requirements? What is this actually for? I'd expect it to normally be an error, but it really depends on the context.

Well if you expect 2 hex digits for a byte but don't get them the input is not valid and not fixable (whats FFF ? 0FFF or FFF0?) - throw an exception.

3 Answers 3

I completed this challenge in C. I use a loop to remove leading zeroes, or 'A's in this case.

#include #include char* hex_to_base64(char *hex, int size) < int size64 = (size * 2) / 3.0; size64 += 1; char *base64 = calloc(size64, 1); size64 -= 1; for (int i = size-1; i>= 0; i-=3, size64-=2) < base64[size64] |= hex[i]; if (i >0) < base64[size64] |= ((hex[i - 1] > 2); > if (i > 1) < base64[size64 - 1] |= ((hex[i - 2] > return base64; > int main(int argc, char **argv) < int i = 0; //49276D206B696C6C696E6720796F757220627261696E206C696B65206120706F69736F6E6F7573206D757368726F6F6D char input[] = < 4, 9, 2, 7, 6, 13, 2, 0, 6, 11, 6, 9, 6, 12, 6, 12, 6, 9, 6, 14, 6, 7, 2, 0, 7, 9, 6, 15, 7, 5, 7, 2, 2, 0, 6, 2, 7, 2, 6, 1, 6, 9, 6, 14, 2, 0, 6, 12, 6, 9, 6, 11, 6, 5, 2, 0, 6, 1, 2, 0, 7, 0, 6, 15, 6, 9, 7, 3, 6, 15, 6, 14, 6, 15, 7, 5, 7, 3, 2, 0, 6, 13, 7, 5, 7, 3, 6, 8, 7, 2, 6, 15, 6, 15, 6, 13 >; char *output; int outputsize = ((sizeof(input)* 2) / 3.0) + 1; char *text = calloc(outputsize + 1, 1); char *formatted; output = hex_to_base64(input, sizeof(input)); for (i = outputsize-1; i >=0; i--) < if (output[i] < 26) < text[i] = output[i] + 65; >else if (output[i] < 52) < text[i] = output[i] + 97 - 26; >else if (output[i] < 62) < text[i] = output[i] + 48 - 52; >else if (output[i] == 62) < text[i] = '+'; >else if (output[i] == 63) < text[i] = '/'; >> i = 0; formatted = text; while (text[i++] == 'A') < formatted++; >printf("%s\n", formatted); free(text); return 0; > 

Is int size64 supposed to be a signed 64 bit variable? On the CPU I'm working with, int is 32 bits. I declare my variables like uint32_t etc. to avoid confusion.

Found this question working on cryptopals challenge myself. The answer is, according to Wikipedia:

  • Add padding, so that the resulting string is divisible with 3 Bytes.
  • Set the padded 4bit values to 0 (depending on wether you do this before or after hexstring conversion, this is an actual 0 or a '0')
  • In the resulting base64 string, the number of padded 4bit values is marked with the same amount of '=' at the end

The implementation of 0x41414141 seems not be a complete solution for the challenge, as the conversion from hexstring to binary is manually hardcoded. Besides, I don't understand why leading zeros should be removed.

I wrote a solution in C myself that handles any generic hex strings.
Note: it may not be 100% correct or not in best style as I am learning C.

#include #include #include char b64[] = < 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' >; char *bin_from_str(const char *str, const unsigned int size) < char *bin = calloc(size + 1, 1); for(int i = 0; i 97) < // deal with lowercase bin[i] -= 87; >else if (str[i] > 65) < // deal with uppercase bin[i] -= 55; >else if (str[i] < 58) < // deal with nums bin[i] -= 48; >> return bin; > char *b64_from_hexstr(const char *hexstr, const unsigned int size) < char *bin = bin_from_str(hexstr, size); char *base64 = calloc((int)ceil(size * 4 / 6.0) + 1, 1); int j = 0; for(int i = 0; i < (int)size; i+=3, j+=2) < base64[j] = (bin[i] > 2) : 0); base64[j + 1] = ((bin[i + 1] & 0x3) if (size % 3 == 1) < j--; >// remove trailing A's for(int i = 0; i < j; ++i) < printf("%c", b64[(int)base64[i]]); // pretty print b64 string >for(int i = 0; i < (int)(size % 3); ++i) < printf("="); // add padding >printf("\n"); free(bin); return base64; > int main(const int argc, const char **argv) < if (argc != 2) < exit(1); >b64_from_hexstr(argv[1], strlen(argv[1])); return 0; > 
>> gcc 1-1.c && ./a.out 49276d206b696c6c696e6720796f757220627261696e206c696b65206120706f69736f6e6f7573206d757368726f6f6d SSdtIGtpbGxpbmcgeW91ciBicmFpbiBsaWtlIGEgcG9pc29ub3VzIG11c2hyb29t 

Источник

How to encode and decode data in base64 and base64URL by using unix commands?

Recent versions of coreutils include basenc(1) which supports several different encodings. From its help screen:

--base64 same as 'base64' program (RFC4648 section 4) --base64url file- and url-safe base64 (RFC4648 section 5) --base32 same as 'base32' program (RFC4648 section 6) --base32hex extended hex alphabet base32 (RFC4648 section 7) --base16 hex encoding (RFC4648 section 8) --base2msbf bit string with most significant bit (msb) first --base2lsbf bit string with least significant bit (lsb) first --z85 ascii85-like encoding (ZeroMQ spec:32/Z85); when encoding, input length must be a multiple of 4; when decoding, input length must be a multiple of 5 

Here is a string that illustrates the difference:

$ printf "%s" "$s" | xxd -b -c1 | cut -d' ' -f2 | nl 1 01111000 2 01110011 3 00111111 4 00111110 5 00111110 6 00111110 

And as 6 bit blocks (as base64 reads the data):

$ printf "%s" "$s" | xxd -b -c1 | cut -d' ' -f2 | tr -d '\n' | fold -w6 | nl 1 011110 2 000111 3 001100 4 111111 5 001111 6 100011 7 111000 8 111110 

Note that block 4 and block 8 map to / and + respectively (Base64 table on Wikipedia):

This is the same suggestion as @jps but shorter. Also remember that echo by default always adds newline at the end, so when you want to encode it, you must add -n .

echo -n "Some_data_to_be_converted" | base64 | tr '/+' '_-' | tr -d '=' 

Decoding it back with bulit-in bash tools is more complicated as I didn't find an easy way to pad the string back with '=' so that the length will dividable by 4. Probably can be done with awk but I didn't dig deep enough. If you have local ruby it becomes trivial:

2.6.2 > require 'base64' 2.6.2 > Base64.urlsafe_encode64('test', padding: false) => "dGVzdA" 2.6.2 > Base64.urlsafe_decode64('dGVzdA') => "test" 

I took it as a challenge to come up with a bash or standard unix commands to pad the string back in order to decode: stackoverflow.com/questions/58957358/…

Adding on to the answer by Kaplan Ilya, here is a command using standard linux/unix commands that can decode base64url , including handling missing padding.

Note: some versions of base64 can handle missing padding, such as Mac/BSD base64 -D . However, GNU base64 -d requires correct padding.

Also, I used the test string ~~~. instead of the one in the original question Some_data_to_be_converted , so that it will generate + , / , = characters.

text='~~~. ' # encode base64 echo "$text" | base64 # fn5+Pz8/Cg== # encode base64url base64url=$( echo "$text" | base64 | tr '/+' '_-' | tr -d '=' ) echo "$base64url" # fn5-Pz8_Cg # decode base64url echo "$base64url"==== | fold -w 4 | sed '$ d' | tr -d '\n' | tr '_-' '/+' | base64 -d # ~~~. 

Explanation of the decode base64url commands:

  • echo "$str"==== appends 4 equal signs
  • fold -w 4 split every 4 characters into separate lines
  • sed '$ d' deletes the last line (the extraneous padding)
  • tr -d '\n' joins all lines. Now the padding is correct.
  • tr '_-' '/+' converts _ to / , - to + .

(Side note: if you're wondering why not use tr '-_' '+/' since that would be in alphanumeric order, it's because that will result in invalid option because it thinks -_ is an option. You could do tr -- '-_' '+/' , but it's easier just to swap the order.)

If you already have a base64 encoded string you just need to replace "+" with "-" and "/" with " _" to get a base64url encoded string. To achieve this, you can use the following command:

echo Some_data_to_be_converted | base64 | sed 's/+/-/g; s,/,_,g'

Base64 encoding maps the input bytes (8 bit) to a 6 bit representation. 4 base64 characters can encode 4*6=24 bits, which equals 3 bytes. Whenever the number of bytes in your input can't be divided by 3, padding is required according to the standard.

As the = character is used for key-value pairs in URLs, you can't use it directly for padding if you intend to use the encoded value in an URL. You can either just ommit the padding, because most implementations will still work and just ignore the 2 or 4 unused bits on the end. Or, if the receiver really needs padding, you have to replace the = by it's URL safe representation %3d .

Источник

Читайте также:  Посмотреть количество сессий linux
Оцените статью
Adblock
detector