Skip to main content

A Python package for EMV cryptograhy in payment systems

Project description


PyPI Test coverage

This package provides methods for the generation of the Application Cryptograms (TC, ARQC, or AAC) generated by the ICC and the Authorisation Response Cryptogram (ARPC) generated by the issuer and verified by the ICC. In addition, this package supports secure messaging integrity and confidentiality.


pip install pyemv

PyEMV consists of the following modules:

  • - Application Cryptogram support for ARQC, AAC, TC, and ARPC
  • - Secure Messaging support for script command integrity and confidentiality
  • emv.kd - Key Derivation support for ICC master keys and session keys
  • emv.cvn - Cryptogram Version Number module to showcase how EMV security and key management concepts connect together


ICC Master Key derivation method A and B:

>>> from emv import kd
>>> iss_mk = bytes.fromhex('0123456789ABCDEFFEDCBA9876543210')
>>> pan = b'99012345678901234'
>>> psn = b'45'
>>> icc_mk_a = kd.icc_mkd_a(iss_mk, pan, psn)
>>> icc_mk_a.hex().upper()
>>> icc_mk_b = kd.icc_mkd_b(iss_mk, pan, psn)
>>> icc_mk_b.hex().upper()

Common Session Key derivation:

>>> r = bytes.fromhex('1234567890123456')
>>> sk = kd.common_skd(icc_mk_a, r)
>>> sk.hex().upper()

Application Request Cryptogram generation:

>>> from emv import ac
>>> ac_data = bytes.fromhex('0123456789ABCDEF0123456789ABCDEF')
>>> arqc = ac.generate_ac(sk, ac_data)
>>> arqc.hex().upper()

Application Response Cryptogram generation method 1 and 2:

>>> arpc_rc = bytes.fromhex('0000')
>>> arpc = ac.generate_arpc_1(sk, arqc, arpc_rc)
>>> arpc.hex().upper()
>>> csu = bytes.fromhex('00000000')
>>> arpc = ac.generate_arpc_2(sk, arqc, csu)
>>> arpc.hex().upper()

Secure Messaging Integrity (MAC):

>>> from emv import sm
>>> sk_smi = bytes.fromhex('0123456789ABCDEFFEDCBA9876543210')
>>> command = bytes.fromhex('8424000008')
>>> mac = sm.generate_command_mac(sk_smi, command)
>>> mac.hex().upper()

Secure Messaging Confidentiality:

>>> sk_smc = bytes.fromhex('0123456789ABCDEFFEDCBA9876543210')
>>> command_data = bytes.fromhex('049999FFFFFFFFFF')
>>> enc_data = sm.encrypt_command_data(sk_smc, command_data,
...                                    sm.CommandDataEncryptionType.emv)
>>> enc_data.hex().upper()


PyEMV is hosted on GitHub.

Feel free to fork and send contributions over.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for pyemv, version 0.0a3
Filename, size File type Python version Upload date Hashes
Filename, size pyemv-0.0a3-py3-none-any.whl (17.0 kB) File type Wheel Python version py3 Upload date Hashes View hashes
Filename, size pyemv-0.0a3.tar.gz (34.8 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page