Try to decipher the garbled attachment file name with Python

Attachment name is garbled

In a small number of environments, when a Windows 8 store app mailer receives an attachment with a Japanese name, it looks like % 1B% 24% 42 ~ .ext or% EF% BF %% BA.ext. There was a case that it ended up. It seems that the original file name is treated as being percent-encoded. I didn't know the basic measures to receive with the correct file name, but for the time being, I wanted to find out only the file name, so I tried decoding using Python.

Decode with Python

In Python you can decode percent encoding by using ʻurllib.parse.unquote`. So, as a result of converting with the following code,

unquote


import urllib.parse
a = '%1B%24%42%4A%3F%40%2E%1B%28%42%32%37%1B%24%42%47%2F%1B%28%42%2E%70%64%66'
urllib.parse.unquote(a)

The result is \ x1b $ BJ? @. \ X1b (B27 \ x1b $ BG / \ x1b (B.pdf and cannot be read. Unquote specifies ʻencoding ='utf-8'` by default. It is interpreted by utf-8, but it seems that it is not decoded normally, so it seems to be a different character code.

Examine the character code

By the way, if you do not know which character code was expressed, you cannot restore to the original character string, so you need to check the character code. Looking at the character string, there are patterns such as % 1B% 24% 42 and% 1B% 28% 42, which is the code to switch the mode used in JIS code. From this, it can be expected that this character string will be JIS code.

Decode with Python again

The JIS code is also called ISO-2022-JP, and it seems that it is handled by the name iso2022-jp in Python (is this the official name?). Specify the character code with ʻencoding ='iso2022-jp'` and try decoding.

unquote


import urllib.parse
s = '%1B%24%42%4A%3F%40%2E%1B%28%42%32%37%1B%24%42%47%2F%1B%28%42%2E%70%64%66'
urllib.parse.unquote(s, encoding='iso2022-jp')

I got the result 2015.pdf, and I was able to know the file name safely.

Recommended Posts

Try to decipher the garbled attachment file name with Python
Try to solve the programming challenge book with python3
Try to solve the internship assignment problem with Python
I tried to touch the CSV file with Python
Extract the xz file with python
[python] Change the image file name to a serial number
[Python] Write to csv file with Python
Try to operate Facebook with Python
Output to csv file with Python
Try to automate the operation of network devices with Python
I tried to divide the file into folders with Python
Try to solve the shortest path with Python + NetworkX + social data
Try adding a wall to your IFC file with IfcOpenShell python
Try to reproduce color film with Python
Try logging in to qiita with Python
Check the existence of the file with python
Let's read the RINEX file with Python ①
Python amateurs try to summarize the list ①
Try rewriting the file with the less command
The road to compiling to Python 3 with Thrift
Character code processing compared to "Are to put on a wound" ~ Garbled file name operation with python3 ~
Put Cabocha 0.68 on Windows and try to analyze the dependency with Python
Try to image the elevation data of the Geographical Survey Institute with Python
The file name was bad in Python and I was addicted to import
Python / numpy> Read the data file with the item name line> Use genfromtxt ()
[Cloudian # 5] Try to list the objects stored in the bucket with Python (boto3)
Try to solve the fizzbuzz problem with Keras
nginxparser: Try parsing nginx config file with Python
How to read a CSV file with Python 2/3
Try to solve the Python class inheritance problem
Try to draw a life curve with python
Specify the Python executable to use with virtualenv
[Python] How to read excel file with pandas
Convert svg file to png / ico with Python
Say hello to the world with Python with IntelliJ
Try to make a "cryptanalysis" cipher with Python
Try to automatically generate Python documents with Sphinx
The easiest way to use OpenCV with python
Introduction to Python with Atom (on the way)
Save the object to a file with pickle
Try to make a dihedral group with Python
Extract zip with Python (Japanese file name support)
Convert the character code of the file with Python3
I want to write to a file with Python
Try to detect fish with python + OpenCV2.4 (unfinished)
Try to solve the traveling salesman problem with a genetic algorithm (Python code)
Python / subprocess> Symbolic link Implementation to get only the destination file name> os.readlink ()
I want to get the file name, line number, and function name in Python 3.4
Python Memorandum: Refer to the text and edit the file name while copying the target file
To automatically send an email with an attachment using the Gmail API in Python
Try to use up the Raspberry Pi 2's 4-core CPU with Parallel Python
I tried searching for files under the folder with Python by file name
I want to see the file name from DataLoader
How to get the variable name itself in python
[First API] Try to get Qiita articles with Python
Template of python script to read the contents of the file
[Python] Try to read the cool answer to the FizzBuzz problem
[Introduction to Python] How to iterate with the range function?
[Cloudian # 8] Try setting the bucket versioning with Python (boto3)
Try translating with Python while maintaining the PDF layout
Try to visualize the room with Raspberry Pi, part 1