📄 super_read_binary.cpp
字号:
#include "inkit/jaya_reader/JayaReaderRegistry.h"#include "inkit/jaya_reader/JayaReader.h"#include "inkit/jaya_reader/jaya_utils.h"#include "inkit/common/simu_launcher.h"#include "common/portability.h"#include <string>#include <stdio.h>using namespace Jayacard;JayaReader * rd = 0;void done(){ if (rd != NULL) rd->close(); printf("Done\n"); stop_simulator(); //getchar(); getchar(); exit(0);}void report_error( const char * msg, jresult ret){ if (ret == JY_OK) return; printf("%s : %s\n", msg, jerror_msg(ret) ); done();}char * getChallenge = "00 84 00 00 08";char * extAuth = "00 82 00 00 08 FF FF FF FF FF FF FF FF";char * bootstrapMf = "80 E0 3F 00 00";char * createBinary = "80 E0 %s 06 20 %02X FF FF %02X %02X";char * selectFid = "00 A4 00 00 02 3F 00";char * getResponse = "00 C0 00 00 00";char * readBinary = "00 B0 %02X %02X 00";char * updateBinary = "00 D6 %02X %02X 00";char * changeKey = "80 D8 00 00 1E FF FF FF FF FF FF FF FF" " 44 03 1F 49 32 07" " 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11 11";void exchange( char * msg, jbyte * command, int cmdLen, jbyte * response, int * respLen ){ jresult ret; printf("%s\n", msg ); ret = rd->exchangeApdu( command, cmdLen, response, respLen ); report_error( msg, ret );}void run(){ jbyte command[1024], response[1024]; int cmdLen, respLen; rd->powerOn(); jbyte challenge[8]; jbyte crypted_challenge[8]; jresult ret; // pwon printf("PowerOn\n"); ret = rd->powerOn(); report_error( "powerOn", ret ); // Get Challenge cmdLen = str2hex( command, getChallenge ); respLen=1024; exchange( "Get Challenge", command, cmdLen, response, &respLen ); memcpy( challenge, response, 8 ); ret = jaya_external_authenticate_keys_in_file( DEFAULT_KEYFILE, "msk_keya", "msk_keyb", challenge, crypted_challenge ); report_error( "crypt keys", ret ); // Auth cmdLen = str2hex( command, extAuth ); memcpy( command + 5, crypted_challenge, 8 ); respLen=1024; exchange( "Ext Auth", command, cmdLen, response, &respLen ); // Bootstrap Filesystem cmdLen = str2hex( command, bootstrapMf ); respLen=1024; exchange( "Bootstrap MF", command, cmdLen, response, &respLen ); // Create EF char buf[200]; char * fid = "30 01"; int sfi=01, size=1000; sprintf( buf, createBinary, fid, sfi, (size>>8) & 0xFF, size&0xFF ); cmdLen = str2hex( command, buf ); respLen=1024; exchange( "Create EF", command, cmdLen, response, &respLen ); // Select EF cmdLen = str2hex( command, selectFid ); str2hex( command + 5, fid ); respLen=1024; exchange( "Select EF", command, cmdLen, response, &respLen ); // Get Response cmdLen = str2hex( command, getResponse ); command[4] = response[1]; respLen=1024; exchange( "Get Response", command, cmdLen, response, &respLen ); // Update Binary int step = 100; int cnt=1; int idx = 0; while( idx < 500 ) { sprintf( buf, updateBinary, (idx>>8) & 0xFF, idx&0xFF ); // offset = 0x0000 cmdLen = str2hex( command, buf ); command[4] = step; cmdLen += step; memset( command + 5, cnt, step ); respLen=1024; exchange( "Update Binary", command, cmdLen, response, &respLen ); cnt += 1; idx += step; } // Read Binary sprintf( buf, readBinary, 0, 0 ); // offset = 0x0000 cmdLen = str2hex( command, buf ); command[4] = 30; respLen=1024; exchange( "Read Binary", command, cmdLen, response, &respLen ); // read it printf("Super read binary\n"); string result; result = rd->extraCommand( "super_read_binary" ); printf("Result : '%s'\n", result.c_str() );}/* ======================================================================== Main function ======================================================================== */#ifdef WIN32BOOL WINAPI ControlHandler ( DWORD dwCtrlType ){ switch( dwCtrlType ) { case CTRL_BREAK_EVENT: /* use Ctrl+C or Ctrl+Break to simulate */ case CTRL_C_EVENT: done(); return TRUE; default: break; } return FALSE;}#elsevoid sigint_handler(int signum){ done();}#endifint main(){ string readerName; #ifdef WIN32 SetConsoleCtrlHandler( ControlHandler, TRUE ); #else signal( SIGINT, sigint_handler ); #endif string reader_name; jresult ret; /*--------------------- connect to the reader --------------------------*/ reader_name = select_rd(); if (strncmp( reader_name.c_str(), "SimuReader", 10 ) == 0) { launch_simulator(); } printf("Connecting to reader %s\n", reader_name.c_str() ); rd = JayaReaderRegistry::instance()->getReader( reader_name, &ret ); if (rd == NULL) { fprintf(stderr, "Could not get pointer to %s\n", reader_name.c_str() ); done(); } report_error( "creation", ret ); printf("Using %s, waiting for card\n", reader_name.c_str() ); while( ! rd->isCardPresent() ) { printf("."); } printf("\nCard found.\n"); run(); done(); return 0;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -