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)
from M2Crypto import EC, EVP, BIO import binascii, sys, cryptimage from pydmtx import DataMatrix from PIL import Image debug=0 #read in the data from the image dm_read = DataMatrix(scheme=DataMatrix.DmtxSchemeBase256) img = Image.open("dm.png") data = dm_read.decode(img.size[0],img.size[1],img.tostring()) #extract public key and encrypted message ephpub, ciphertext = cryptimage.parse_input(data) ephemeral = EC.pub_key_from_der(cryptimage.build_asn1(ephpub)) 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))