Python gpg decryption not printing non english characters correctly - python

I am trying to decrypt a json message body having mix of numeric and non english characters. The decrypted string is not showing non English characters properly.
Details:-
1) Input is base64 encoded and gpg encrypted
2) I am using python base64 and gnupg modules to decode and decrypt the message.
The output is displayed as (part of the output due to the data sensitivity):-
{"id":"123","name":"ååéå業é¡
I am expecting the output as
{{"id":"123","name":"豐國業銀"
Here is the python code I am using for the above task:
import json
import os
import base64
import gnupg
gpg = gnupg.GPG()
with open('item2.json', 'r') as file:
json_data = json.load(file)
for value in json_data['items']:
data = value['payload']
print (data)
str_data = base64.b64decode(data)
print (str_data)
decrypted_data = gpg.decrypt(str_data, passphrase=output)
print (decrypted_data)

It's probably caused by a character encoding mismatch. It will also make a difference whether you're using Python 2 or Python 3.
Since Python 2 has finally reached its EOL, I'm going to assume you (and everyone to subsequently read this) need Python 3 code. There are also a number of possible explanations stemming from the way the python-gnupg module accesses the gpg executable.
For both security and ease of use reasons, I recommend instead using the Python bindings module which ships whith the GPGME C API instead.
import json
import os
import base64
import gpg
c = gpg.Context(armor=True)
with open('item2.json', 'r') as file:
json_data = json.load(file)
for value in json_data['items']:
data = value['payload']
print (data)
str_data = base64.b64decode(data.encode())
print (str_data)
decrypted_data, result, verify_result = c.decrypt(str_data)
print (decrypted_data)
Now decrypted_data only contains the plaintext, result contains information about the key(s) the encrypted data is encrypted to, and verify_result contains information about the key(s) the data was signed with, if any.
More details are in the included documentation, a draft copy is available here for convenience.
In all likelihood, however, the cause was the base64.b64decoding method, which needs to process bytes, not str. Which means you either need to encode data (as I have done above), or read in the JSON data as bytes instead of strings.

Related

Python method like File.WriteAllBytes() in .Net

There is a .net API sending the byte data as a string in JSON, I am using a python API to read it and write it in a file.
a = io.BytesIO(b"JVBERi0xLjQNJcjIyMjIyMg...")
with open('test.pdf','wb') as g:
g.write(a.getvalue())
I created a file with this code but unable to open the file.
I need another way of doing the same.
instead of using io.BytesIO use base64.
import base64
data = "strin data"
base64_data =base64.b64decode(data)
with open(filename,'wb') as f:
f.write(base64_data)

String that was RSA encrypted using Java contains extra encrypted characters when decrypted using Python

I have been provided with data that was RSA encrypted by a Java application and the RSA public key in DER format. I wrote a simple Python script, using pycrypto , to decrypt the data however the output contains what seems to be encrypted text as well as what I am looking for. This is quite untidy and not suitable for further processing.
Original String, and my expected output is something like:
elem:1234567890:0987654321
The owners of the application that encrypted the data told me to
Decode the string from Base64
Decrypt the string using the private key they supplied me with (DER format)
My Python script looks something like this:
from Crypto.PublicKey import RSA
import base64
import sys
import ast
def decode_rsa(encryptedString, key_path):
key = RSA.importKey(open(key_path).read())
deocdedString = base64.b64decode(encryptedString)
decrpytedString = key.decrypt(deocdedString)
return decrpytedString
print decode_rsa(sys.argv[1], sys.argv[2])
But when I run the code I get:
python decryptRSA.py Hh6+rJdFA0SPWvbLU8gxbrZXTnYXv3M/XlSU2IHgfGvIMXckrJk/3w7OSjadhNeyIHqzfXNXRexn721lmCh7QZbGXB/cKzuEDr9pAZU6kbrc1BWDLkTuOC5e+vAcV21sebuYQUyWjGGkuMrTtXw9nlT0+h9/GAzFS7wVTFE859w= private_key.der
uB▒#▒▒▒{4elem:1234567890:0987654321&i▒_+▒▒,▒I%▒▒▒▒▒▒(d>
As you can see, what I need is there but there are other weird characters coming back too.
You are raw decrypting the RSA encrypted text. RSA requires padding to be secure. You need to use a padding mode - probably OAEP - as indicated in the documentation of PyCrypto.

Reading a raw RAM data in python

I took the dump of RAM data using a freeware called DumpIt(http://www.downloadcrew.com/article/23854-dumpit). The software saved the RAM data as a raw file which can be read using a Hex Editor(http://www.downloadcrew.com/article/10814-hxd).
How do I get the string data as visible in the Hex Editor(see image) in python?
For eg: I want to get the string "http://www.downloadcrew.com/article/23854-dumpit" in red box in image in python by reading the raw file generated by DumpIt.
EDIT
I tried using this code but it just gets stalled and nothing happens
#!/usr/bin/python
import binascii
filename = "LEMARC-20140401-181003.raw"
g = open("out","w")
str=""
with open(filename,"rb") as f:
for lines in f:
str+=lines
str = binascii.unhexlify(str)
f.close()
g.write(str)
g.close
In Python2
"437c2123".decode('hex')
'C|!#'
In Python3 (also works in Python2, for <2.6 you can't have the b prefixing the string)
import binascii
binascii.unhexlify(b"437c2123")
b'C|!#'
So in your case decode the entire hex string to get the ascii, and then you can extract the url with a regex or your own parsing function

how to serialize arbitrary file types to json string in python

My server is going to be sending a JSON, serialized as a string, through a socket to another client machine. I'll take my final json and do this:
import json
python_dict_obj = { "id" : 1001, "name" : "something", "file" : <???> }
serialized_json_str = json.dumps(python_dict_obj)
I'd like to have one of the fields in my JSON have the value that is a file, encoded as a string.
Performance-wise (but also interoperability-wise) what is the best way to encode a file using python? Base64? Binary? Just the raw string text?
EDIT - For those suggestion base64, something like this?
# get file
import base64
import json
with open(filename, 'r') as f:
filecontents = f.read()
encoded = base64.b64encode(filecontents)
python_dict_obj['file'] = encoded
serialized_json_str = json.dumps(python_dict_obj)
# ... sent to client via socket
# decrpyting
json_again = json.loads(serialized)
filecontents_again = base64.b64decode(json_again['file'])
I'd use base64. JSON isn't designed to communicate binary data. So unless your file's content is vanilla text, it "should be" encoded to use vanilla text. Virtually everything can encode and decode base64. If you instead use (for example) Python's repr(file_content), that also produces "plain text", but the receiving end would need to know how to decode the string escapes Python's repr() uses.
JSON cannot handle binary. You will need to encode the data as text before serializing, and the easiest to encode it as is Base64. You do not need to use the URL-safe form of encoding unless there are requirements for it further down the processing chain.

Decode base64 encoded file and print results to the console

So I have a file that I have already encoded with base64, now I want to decode it back but instead of creating another file, I want to decode it in the console and print the results to screen. How to do that?
encoded file string = MUhRRy1ITVRELU0zWDItNlcxSA==
FYI:
this would mean first opening the file in console, then decoding the give string
Thanks
Unless I'm overlooking something, this is as simple as reading in your encoded string and then calling the standard library's base64.b64decode function on it.
Something like:
with open(path_to_encoded_file) as encoded_file:
print base64.b64decode(encoded_file.read().strip())
Using base64.decode, set sys.stdout (sys.stdout.buffer.raw in python 3.x) as output.
import sys
import base64
with open('filepath') as f:
base64.decode(f, sys.stdout)

Categories