#!/usr/bin/env python # # simply.py, as shipped with pyme in examples/, adapted to use CMS (not PGP) # # based upon: $Id: simple.py,v 1.4 2005/05/17 16:49:28 belyi Exp $ # Copyright (C) 2005 Igor Belyi # Copyright (C) 2002 John Goerzen # # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 2 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the Free Software # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA # # usage: # # copy gpgme1.0_1.0.3/tests/gpgsm/pubring.kbx to ~/.gnupg/pubring.kbx # # % echo '3CF405464F66ED4A7DF45BBDD1E4282E33BDB76E S' >> ~/trustlist.txt # % echo disable-crl-checks > ~/.gnupg/gpgsm.conf # % echo faked-system-time 1008241200 >> ~/.gnupg/gpgsm.conf # # % export GPG_TTY=`tty` # % eval `gpg-agent --daemon --use-standard-socket` # % simple.py # Enter name of your recipient: test cert 1 # outputs # -----BEGIN ENCRYPTED MESSAGE----- # MIAGCSqGSIb3DQEHA6CAMIACAQAxggEJMIIBBQIBADBwMGsxCzAJBgNVBAYTAkRF # MRMwEQYDVQQHFApE/HNzZWxkb3JmMRYwFAYDVQQKEw1nMTAgQ29kZSBHbWJIMRkw # # # you should be able to decrypt the message: # # save the output of simple.py in a file named ~/crypt; copy # gpgme1.0_1.0.3/tests/gpgsm/private-keys-v1.d/32100C27173EF6E9C4E9A25D3D69F86D37A4F939.key # to ~/private-keys-v1.d/ and run: # # % gpgsm --decrypt < crypt ; echo # # it should show # # # This is my message. # import sys from pyme import core, constants, errors import pyme.constants.validity # Set up our input and output buffers. plain = core.Data('This is my message.') cipher = core.Data() # Initialize our context. c = core.Context() c.set_armor(1) c.set_protocol(pyme.util.gpgme.GPGME_PROTOCOL_CMS) # Set up the recipients. sys.stdout.write("Enter name of your recipient: ") name = sys.stdin.readline().strip() c.op_keylist_start(name, 0) r = c.op_keylist_next() if r == None: print "The key for user \"%s\" was not found" % name else: # Do the encryption. try: c.op_encrypt([r], 1, plain, cipher) cipher.seek(0,0) print cipher.read() except errors.GPGMEError, ex: print ex.getstring()