SaltyCrane Blog — Notes on JavaScript and web development

Notes on using keyczar and python

Install keyczar

  • Install pip and virtualenv
  • Install python-keyczar (and pycrypto and pyasn1):
    $ virtualenv kzenv 
    $ source kzenv/bin/activate 
    $ pip install python-keyczar 
    

Create the keyczar keys

$ mkdir -p /tmp/kz 
$ keyczart create --location=/tmp/kz --purpose=crypt 
$ keyczart addkey --location=/tmp/kz --status=primary 

Example python script to encrypt and decrypt

Create my_kz_example.py:

from keyczar import keyczar

s = 'secret string'
location = '/tmp/kz'
crypter = keyczar.Crypter.Read(location)
s_encrypted = crypter.Encrypt(s)
s_decrypted = crypter.Decrypt(s_encrypted)
print s
print s_encrypted
print s_decrypted

Run it:

$ python my_kz_example.py 
secret string
AKshuoLCgWOj0cqhgDKUkwClXl9BQ7IJ0zhG3V5Y7MR2xk6T4L3bCvhFQ-49Or8SKWs2JSMg1xnf
secret string

Obsolete instructions

Create command line tool

  • Create the keyczart script in your ~/bin directory:
    $ mkdir ~/bin
    $ install -m 755 -o $(whoami) $(python -c 'import keyczar.keyczart as kz; print kz.__file__[:-1]') ~/bin/keyczart 
    
  • Edit ~/bin/keyczart:
    • Change the first line from:
      #!/usr/bin/python2.4
      to:
      #!/usr/bin/env python
    • Change the import statements from:
      import errors
      import keyczar
      import keydata
      import keyinfo
      import readers
      import util
      
      to:
      import keyczar.errors as errors
      import keyczar.keyczar as keyczar
      import keyczar.keydata as keydata
      import keyczar.keyinfo as keyinfo
      import keyczar.readers as readers
      import keyczar.util as util
      

Comments