How can I encode strings in base64 unicode in JavaScript and Python?

I need encript arithmetic that injects text into text.

the input text can be unicode, and the output must be az AZ 0-9 -. (64 char max)

and it can be decrypted again for unicode.

It should be implemented in javascript and python.

If some library can already do this, fine, if not, could you tell me.

Let me tell you why

To trick China into the Great Fire Wall, and the GAE https was blocked in China. Angry for this damn government.

+4
source share
2 answers

You might want to look at the base64 module . In Python 2.x (since 2.4):

>>> import base64 >>> s=u"Rückwärts" >>> s u'R\xfcckw\xe4rts' >>> b=base64.b64encode(s.encode("utf-8")) >>> b 'UsO8Y2t3w6RydHM=' >>> d=base64.b64decode(b) >>> d 'R\xc3\xbcckw\xc3\xa4rts' >>> d.decode("utf-8") u'R\xfcckw\xe4rts' >>> print d.decode("utf-8") Rückwärts 
+10
source

You are looking for base64 encoding. In JavaScript and Python 2, this is a bit complicated, since the latter does not support Unicode initially, and for the former you will need to implement Unicode encoding.

Python 3 solution

 >>> from base64 import b64encode, b64decode >>> b64encode( 'Some random text with unicode symbols: äöü今日は'.encode() ) b'U29tZSByYW5kb20gdGV4dCB3aXRoIHVuaWNvZGUgc3ltYm9sczogw6TDtsO85LuK5pel44Gv' >>> b64decode( b'U29tZSByYW5kb20gdGV4dCB3aXRoIHVuaWNvZGUgc3ltYm9sczogw6TDtsO85LuK5pel44Gv' ) b'Some random text with unicode symbols: \xc3\xa4\xc3\xb6\xc3\xbc\xe4\xbb\x8a\xe6\x97\xa5\xe3\x81\xaf' >>> _.decode() 'Some random text with unicode symbols: äöü今日は' 
+4
source

All Articles