I am working with a number of .plist files. Usually I have opened them in notepad or similar programs, which shows the following first lines (the file is large, so I reduce the example to a couple of lines):
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>cert</key>
<array>
<dict>
<key>KeyNameHere</key>
<data>
MQwwCgwEcHJvdAwCZGs=
</data>
<key>AnotherKeyNameHere</key>
<data>
NjU0QkNBOUYtQjM4Qi00NzY3LUEyRjYtRDkwQ0EyNzcyQTM3
</data>
<key>UUID</key>
<data>
NjY4NzUzRTItNTg3OC00MDNBLUE3NkYtQzZCQzQ3QjVEMzIz
The value-fields in the dict elements contain, among other, strings encoded in base64 (am I using the correct term here?). So my thought was to write a script to parse and decode these values. I began by writing this script to open and print the plist file in Python:
import plistlib
with open("..test.plist", "rb") as file:
pl = plistlib.load(file)
for x in pl:
for y in pl[x]:
for z in y:
print(z, y[z])
However, when I run this script, many of the value-fields in the dictionarys are shown in decoded / human readable format. For example, the UUID field is not shown as a base64 string, but as an UUID.
Why does notepad and similar programs show the value-fields as base64, when opening in python (I have tried open in Python with both "rb" and "r", both giving the same result) does not encode / decode to base64? Any help and input as regards to understanding the difference in decoding / encoding would be greatly appreciated.
Why does notepad and similar programs show the value-fields as base64, when opening in python (I have tried open in Python with both "rb" and "r", both giving the same result) does not encode / decode to base64?
XML-based plists encode NSData/CFData as <data>
elements with the base64-encoded data as element content.
Since you're using plistlib
, it decodes that contents and gives you the actual data. That's kinda the point of using plistlib: it decodes the plist data to native types and content (and should work the same way across both binary and xml plists).
If you want to access the raw plist data for some reason you can open plist files using an XML library instead.