# Base64 decode issue

**URL:** <https://discuss.elastic.co/t/base64-decode-issue/319874>\
**Category:** Logstash\
**Created:** [November 27, 2022, 5:31am UTC](https://discuss.elastic.co/t/base64-decode-issue/319874 "2022-11-27T05:31:33Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Rajesh\_R](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rajesh_r/32/113815_2.png) [@Rajesh\_R](https://discuss.elastic.co/u/Rajesh_R)\
**Post date:** [November 27, 2022, 5:31am UTC](https://discuss.elastic.co/t/base64-decode-issue/319874/1 "2022-11-27T05:31:33Z")

</div>

Hi Team, Does Anyone know how to decode base64 in logstash?  
Logfile :  
{"[ID":"11166946081959","Type":"LOG","pID":"rajesh.r@gmail.com](mailto:ID%22:%2211166946081959%22,%22Type%22:%22LOG%22,%22pID%22:%22rajesh.r@gmail.com)","Interface":"Offboarding","payload": "PD94bWzCoHZlcnNpb249IjEuMCLCoGVuY29kaW5nPSJJU08tODg1OS0xIj8+wqDCoDxub3RlPsKgwqDCoDx0bz5Ub3ZlPC90bz7CoMKgwqDCoDxmcm9tPkphbmk8L2Zyb20+wqDCoMKgwqA8aGVhZGluZz5SZW1pbmRlcjwvaGVhZGluZz7CoMKgwqDCoDxib2R5PkRvbid0wqBmb3JnZXTCoG1lwqB0aGlzwqB3ZWVrZW5kITwvYm9keT7CoMKgPC9ub3RlPsKg"}

Config file:  
input {  
file{  
path =\> "/home/rajesh/ELK/json.log"  
start\_position =\> beginning  
tags =\> ["time"]  
sincedb\_path =\> "/dev/null"  
codec =\> json {  
target =\> "[document]"  
}  
}  
}  
filter {  
ruby { code =\> 'event.set("decoded", Base64.decode64(event.get("payload")))' }  
}  
output {  
if "time" in [tags] {  
elasticsearch {  
hosts =\> ["[https://localhost:9200](https://localhost:9200)"]  
cacert =\> '/home/rajesh/ELK/logstash-8.4.3/config/certs/http\_ca.crt'  
index =\> "time-%{+YYYY.MM.dd}"  
user =\> "rajesh"  
password =\> "rajesh"  
}  
}  
}

---

<div class="post-metadata">

**Author:** ![Badger](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/badger/32/25190_2.png) [@Badger](https://discuss.elastic.co/u/Badger)\
**Post date:** [November 27, 2022, 5:14pm UTC](https://discuss.elastic.co/t/base64-decode-issue/319874/2 "2022-11-27T17:14:45Z")

</div>

> [@Rajesh\_R](#):
>
> ruby { code =\> 'event.set("decoded", Base64.decode64(event.get("payload")))' }

That will decode the base64 and result in

```
"decoded" => "<?xml\xC2\xA0version=\"1.0\"\xC2\xA0encoding=\"ISO-8859-1\"?>\xC2\xA0\xC2\xA0<note>

```

The \xC2\xA0 are non-breaking spaces in UTF-8, but the decoded string is ASCII-8BIT (puts Base64.decode64(event.get("payload")).encoding.name will tell you that).

So the default encoding is wrong. You can change it using

```
ruby { code => 'event.set("decoded", Base64.decode64(event.get("payload")).force_encoding("UTF-8"))' }

```

which will result in

```
"decoded" => "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?> <note> <to>Tove</to>...

```

Note that an xml filter will not parse non-breaking spaces, so you need to replace them if you want to parse the XML

```
mutate { gsub => ["decoded", "[[:space:]]", " " ] }

```

---

<div class="post-metadata">

**Author:** ![Rajesh\_R](https://sea2.discourse-cdn.com/elastic/user_avatar/discuss.elastic.co/rajesh_r/32/113815_2.png) [@Rajesh\_R](https://discuss.elastic.co/u/Rajesh_R)\
**Post date:** [November 28, 2022, 2:55am UTC](https://discuss.elastic.co/t/base64-decode-issue/319874/3 "2022-11-28T02:55:18Z")

</div>

I tried the same thing, but the decoded value not coming in kibana. please find the below snap for reference

 ![image](https://us1.discourse-cdn.com/elastic/original/3X/0/0/00812a463229ddb27b03c0d4565e14e27057073f.png)

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/elastic/original/3X/1/a/1ac57faf039f6b580b3f104ef42a2a89e41014de.png) [@system](https://discuss.elastic.co/u/system)\
**Post date:** [December 26, 2022, 2:55am UTC](https://discuss.elastic.co/t/base64-decode-issue/319874/4 "2022-12-26T02:55:49Z")

</div>

This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.
