encryption - Python gpgme non-ascii text handling -


I am trying to encrypt-decrypt via GPGME via GPG, whereas it works for Western characters Does decryption fail on a Russian text. I use the GPG suite on Mac to decrypt e-mail. I use code to produce encrypted e-mail body, note that I tried to encode the message in Unicode but it did not make any difference. I use Python 2.7.

Please help, I must say that I'm new to the dragon.

  ctx = gpgme.Context () ctx.armor = true key = ctx Get_key ('0B26AE38098') Payload = 'Start' '#plain = Biteioo (Payload.Incode (' UTF-8 ')) Plain = Baitio (Payload) Cipher = Baitio (ctx.encrypt ([key], gpgme. ENCRYPT_ALWAYS_TRUST, plain, cipher)  

Here are many problems that you should actually read, but I will try to explain


  payload = 'project'  

Python 2.x source code by default, Latin-1 but your source is clearly Latin is not -1, because even those characters in Latin-1 do not have if you type in a program (such as text editor) in UTF-8 with the password tyster What happens, if it is written, then it is read in another program (such as Python) as Latin-1? You get the ÐÑоÑÑо ÑÐμÑÑ . So, what you're doing is a string filled with rubbish. If you are using ISO-8859-5 instead of UTF-8, then it will be a different nonsense, but still nonsense

So first and foremost, you have to find out which encoding you used in your text editor. This probably is UTF-8, if you are on Mac, but just do not guess; Find out.


Secondly, you have to tell Python what encoding you used in it. You do this by using any one. For example, if your text editor uses UTF-8, then add this code at the top of your code:

  # code = utf-8  

You can fix this, payload will be a byte string, which uses your text editor in your encoding. But you can not sync encoded byte strings already, only Unicode strings

Python 2.x will call you encoded in any way, but This is not very useful - what it will do, decoding the string to Unicode using the first sys. Getdefaultencoding , so it can then convert to sign language

The correct way to decide is that payload using the word Unicode, the Unicode string is first To do in Like this:

  payload = u'prostro test ' 

Now, in the end, you can actually enter payload in UOT- Which you did exactly in your first attempt:

  plain = biteioo (payload.acode ('utf-8'))  

Finally, you are encrypting UTF-8 plain text with GPG when you decrypt it on the other side, it should be understood as UTF-8 as well Make sure, or you'll probably see crap.


Comments

Popular posts from this blog

ios - How do I use CFArrayRef in Swift? -

eclipse plugin - Run java code error: Workspace is closed -

c - Error on building source code in VC 6 -