from M2Crypto import EC, EVP import binascii, sys, cryptimage from pydmtx import DataMatrix debug=0 ecpubkey = binascii.a2b_hex('02b6fd5e085b9aa66de4bb501d4ede5e2b5215680cc7d8c050aab7a8dc505213c1') ecder = cryptimage.build_asn1(ecpubkey) ecpub = EC.pub_key_from_der(ecder) digest = EVP.MessageDigest("sha1") digest.update(cryptimage.compress_key(ecpubkey)) fingerprint = digest.digest() # NID_X9_62_prime256v1 ephemeral = EC.gen_params(EC.NID_X9_62_prime256v1) ephemeral.gen_key() ephpub=cryptimage.strip_asn1(ephemeral.pub().get_der()) ephpub=cryptimage.compress_key(ephpub) shared = ephemeral.compute_dh_key(ecpub.pub()) #strip second half of key which is y cordinates and can be derived from first half dk=cryptimage.KDF(shared[:len(shared)/2],128,fingerprint) if debug: sys.stderr.write("dk = %s\n" % binascii.b2a_hex(dk)) account="2000111122223333" amount="1500050" pin="7654" data=cryptimage.dataencode(account,amount,pin)
if debug: sys.stderr.write("ct = %s\n" % binascii.b2a_hex(ciphertext)) ecpairpem = """-----BEGIN EC PRIVATE KEY----- MHcCAQEEIH8TNBOfV+JWVBr25KfjJ1007paZ/JnrvjxFzZThUgSToAoGCCqGSM49 AwEHoUQDQgAEtv1eCFuapm3ku1AdTt5eK1IVaAzH2MBQqreo3FBSE8EHxCsEGRvK auFV+AgDEQotZbdqzAojRoCjuhZcYP73Pg== -----END EC PRIVATE KEY----- """ #derive shared key ecbio = BIO.MemoryBuffer() ecbio.write(ecpairpem) ecpair = EC.load_key_bio(ecbio) ecder = ecpair.pub().get_der() ecpub = cryptimage.compress_key(cryptimage.strip_asn1(ecder)) digest = EVP.MessageDigest("sha1") digest.update(ecpub) fingerprint = digest.digest() shared = ecpair.compute_dh_key(ephemeral.pub()) dk=cryptimage.KDF(shared[:len(shared)/2],128,fingerprint) #decrypt message, extract contents and print results plaintext = cryptimage.decrypt_data(dk,ciphertext) [account,amount,pin]=cryptimage.datadecode(plaintext) print "Destination account number is: " + account print "Amount to be transfered: %.2f" % (float(amount)/100) print "Please enter pin %s to verify transaction" % pin