/usr/bin/env python
import sys
from binascii import hexlify, unhexlify
from Crypto.Cipher import DES, DES3
def str2bytes(data):
"""
"""
return bytes(data, 'utf-8')
def raw2str(raw_data):
"""
Convert raw binary data to string representation, e.g.
b'\xdf\x12g\xee\xdc\xba\x98v'-> 'DF1267EEDCBA9876'
"""
return hexlify(raw_data).decode('utf-8').upper()
def raw2B(raw_data):
"""
Convert raw binary data to hex representation, e.g.
b'\xdf\x12g\xee\xdc\xba\x98v'-> b'DF1267EEDCBA9876'
"""
return bytes(raw2str(raw_data), 'utf-8')
def B2raw(bin_data):
"""
Convert hex representation to raw binary data, e.g. b'DF1267EEDCBA9876' ->
b'\xdf\x12g\xee\xdc\xba\x98v'
"""
return unhexlify(bin_data)
def hexify(number):
"""
Convert integer to hex string representation, e.g. 12 to '0C'
"""
if( isinstance(number, int) == False ):
raise TypeError('hexify(): expected integer, not {}'.format(type(number)))
if number < 0:
raise ValueError('Invalid number to hexify - must be positive')
return encrypted[:kcv_length]
"""
The algorigthm is used for PVV and CVV calculation.
"""
2. If, at the end of the first scan, less than four decimal digits
have been selected, a second scan is performed from left to right.
During the second scan, all decimal digits are skipped and only nondecimal
digits can be processed. Nondecimal digits are converted to decimal
digits by subtracting 10. The process proceeds until four digits of
PVV are found.
"""
if len(digits) < length:
for c in cyphertext:
if len(digits) >= length:
break
return digits
def get_visa_pvv(account_number, key_index, pin, PVK):
"""
The algorithm generates a 4-digit PIN verification value (PVV) based on the
transformed security parameter (TSP).
For VISA PVV algorithms, the leftmost 11 digits of the TSP are the personal
account number (PAN),
the leftmost 12th digit is a key table index to select the PVV generation key,
and the rightmost
4 digits are the PIN. The key table index should have a value between 1 and 6,
inclusive.
"""
tsp = account_number[-12:-1] + key_index + pin
if len(PVK) != 32:
raise ValueError('Incorrect key length')
encrypted_tsp =
left_key_cypher.encrypt(right_key_cypher.decrypt((left_key_cypher.encrypt(B2raw(tsp
)))))
return bytes(get_digits_from_string(raw2str(encrypted_tsp)), 'utf-8')
return get_digits_from_string(raw2str(block2), 3)
try:
raw_message = bytes.fromhex(block1)
raw_key = bytes.fromhex(block2)
except ValueError:
return ''
def parityOf(int_type):
"""
Calculates the parity of an integer, returning 0 if there are an even number of
set bits, and -1 if there are an odd number.
"""
parity = 0
while (int_type):
parity = ~parity
int_type = int_type & (int_type - 1)
return(parity)
def check_key_parity(key):
"""
Perform the parity check for a given key.
def modify_key_parity(key):
"""
The prior use of the function is to return the parity-validated key.
The incoming key is expected to be hex data binary representation, e.g.
b'E7A3C8B1'
"""
validated_key = b''
for byte in key:
if parityOf(int(byte)) == -1:
byte_candidate = int(byte) + 1
while parityOf(byte_candidate) == -1:
byte_candidate = divmod(byte_candidate + 1, 256)[1]
validated_key += bytes([byte_candidate])
else:
validated_key += bytes([byte])
return validated_key
a =
get_visa_cvv(b'4999988887777000',b'9105',b'101',b'0123456789ABCDEFFEDCBA9876543210'
)
print (a)
https://repl.it/repls/HonestTrivialAtlanticridleyturtle