I'm trying to build a packet using jnetpcap, according to the packets I receive.
I'm currently using jnetpcap-1.4.r1425, and am running into several problems.
The following code is common to all my problems:
JPacket wPacket = new JMemoryPacket(size);
wPacket.order(ByteOrder.BIG_ENDIAN);
wPacket.setUShort(12, 0x0800);
wPacket.scan(JProtocol.ETHERNET_ID);
wEth = wPacket.getHeader(new Ethernet());
wEth.source(sourceMac);
wEth.destination(destMac);
wEth.checksum(wEth.calculateChecksum());
headersize = 14;
wPacket.setUByte(14, 0x40 | 0x05);
wPacket.scan(JProtocol.ETHERNET_ID);
wIp4 = wPacket.getHeader(new Ip4());
if (packet.hasHeader(Icmp.ID))
wIp4.type(Ip4.Ip4Type.ICMP);
else if (packet.hasHeader(Tcp.ID))
wIp4.type(Ip4.Ip4Type.TCP);
else if (packet.hasHeader(Udp.ID))
wIp4.type(Ip4.Ip4Type.UDP);
wIp4.length(500 - wEth.size());
wIp4.source(dIP);
wIp4.destination(sIP);
wIp4.ttl(32);
wIp4.flags(0);
wIp4.offset(0);
wIp4.checksum(wIp4.calculateChecksum());
headersize += 20;
Attempting to get TCP header from above packet gives me null
After the above portion of code, I check the original packet for UDP/TCP/ICMP headers.
The portion for TCP reads as follows:
if (packet.hasHeader(Tcp.ID))
{
wPacket.scan(JProtocol.ETHERNET_ID);
wTcp = wPacket.getHeader(new Tcp());
......
}
This is the same thing I do for the UDP portion, but the TCP portion errors out.
It turns out wTcp == null, and I have no idea why it comes out this way.
ICMP checksum is wrong
The check sums of the common portion work out fine.
However, the ICMP checksum always comes out as being incorrect.
This is what I'm doing for ICMP:
else if (packet.hasHeader(icmp) && icmp.hasSubHeader((new Icmp.EchoRequest())))
{
wPacket.scan(JProtocol.ETHERNET_ID);
wIcmp = wPacket.getHeader(new Icmp());
wIcmp.setUByte(0, 0);
wIcmp.setUByte(1, 0);
JHeader[] tmp = {new Icmp.EchoReply()};
wIcmp.setSubHeaders(tmp);
wIcmp.checksum(wIcmp.calculateChecksum());
headersize += 8 + 4;
}
If anyone can point out what I might be doing wrong, I would much appreciated.
Edit:
toDebugString() and hexdump() after the scan in the above TCP section.
I figure whatever the problem, it can be found here,
JMemory: JMemory#7fd8a0c932d8class org.jnetpcap.packet.JPacket$State: size=304 bytes
JMemory: owner=packet.JScanner.class(size=631888/offset=529976)
JPacket.State#002: sizeof(packet_state_t)=184
JPacket.State#002: sizeof(header_t)=40 and *3=120
JPacket.State#002: pkt_header_map[0]=0x0000000000000007
JPacket.State#002: pkt_header_map[1]=0x0000000000000000
JPacket.State#002: pkt_header_map[2]=0x0000000000000000
JPacket.State#002: pkt_header_map[3]=0x0000000000000000
JPacket.State#002: pkt_flags=0x00000000
JPacket.State#002: pkt_header_count=3
JPacket.State#002: pkt_wirelen=500 bytes
JPacket.State#002: pkt_caplen=500 bytes
JPacket.State#002 : [ Protocol(ID/Flag) | Start | Prefix | Header | Gap | Payload | Postfix ]
JPacket.State#002[0]: [ ETHERNET( 1/0800) | 0 | 0 | 14 | 0 | 486 | 0 ]
JPacket.State#002[1]: [ IP4( 2/0800) | 14 | 0 | 20 | 0 | 466 | 0 ]
JPacket.State#002[2]: [ PAYLOAD( 0/0800) | 34 | 0 | 466 | 0 | 0 | 0 ]
0000:*68 01 00 5e 00 00 01 5f 93 48 01 04 08 00*45 00 h..^..._.H....E.
0010: 01 e6 00 00 00 00 20 06 1d 37 2d 37 80 71 3d f0 ...... ..7-7.q=.
0020: 90 43*13 c0 d8 7f 00 00 01 00 00 00 d8 7f 00 00 .C..............
0030: 02 00 00 00 74 2f 73 74 88 1e 13 c0 d8 7f 00 00 ....t/st........
0040: 00 00 00 00 6c 64 3b 3e 00 00 00 00 00 00 00 00 ....ld;>........
0050: 03 00 01 00 00 00 00 00 b5 00 00 00 00 00 00 00 ................
0060: 01 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
0070: 03 00 00 00 74 70 63 61 e0 1e 13 c0 d8 7f 00 00 ....tpca........
0080: 02 00 00 00 d8 7f 00 00 02 00 00 00 72 6d 61 74 ............rmat
0090: 88 1e 13 c0 d8 7f 00 00 00 00 00 00 75 6d 56 61 ............umVa
00a0: 00 00 00 00 00 00 00 00 03 00 01 00 00 00 00 00 ................
00b0: 19 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
00c0: 00 00 00 00 00 00 00 00 00 00 00 00 49 47 01 00 ............IG..
00d0: 00 00 00 00 00 00 00 00 00 00 00 00 d8 7f 00 00 ................
00e0: 02 00 00 00 6f 42 79 74 88 1e 13 c0 d8 7f 00 00 ....oByt........
00f0: 00 00 00 00 65 03 00 00 00 00 00 00 00 00 00 00 ....e...........
0100: 03 00 01 00 29 56 01 00 b8 00 00 00 00 00 00 00 ....)V..........
0110: 04 00 00 00 00 00 00 00 09 00 0b 00 00 00 00 00 ................
0120: 09 00 06 00 09 00 07 00 f0 1e 13 c0 d8 7f 00 00 ................
0130: 01 00 00 00 00 00 00 00 02 00 00 00 6f 6c 2f 6c ............ol/l
0140: 88 1e 13 c0 d8 7f 00 00 00 00 00 00 45 74 68 65 ............Ethe
0150: 00 00 00 00 00 00 00 00 03 00 01 00 4c 6f 72 67 ............Lorg
0160: b5 00 00 00 d8 7f 00 00 05 00 00 00 00 00 00 00 ................
0170: 00 00 00 00 00 00 00 00 06 00 00 00 61 64 65 72 ............ader
0180: 00 1f 13 c0 d8 7f 00 00 03 00 00 00 72 67 2f 6a ...........rg/j
0190: 02 00 00 00 63 61 70 2f 88 1e 13 c0 d8 7f 00 00 ....cap/........
01a0: 00 00 00 00 4e 31 30 4d 00 00 00 00 00 00 00 00 ....N10M........
01b0: 03 00 01 00 75 69 74 65 b1 00 00 00 6c 72 67 2f ....uite....lrg/
01c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ................
01d0: 61 6e 6e 6f 74 61 74 65 00 00 00 00 00 00 00 00 annotate........
01e0: 00 00 00 00 74 65 3b 01 02 00 00 00 4e 01 00 03 ....te;.....N...
01f0: 1d 31 50 a2* .1P.
And here are the same for the icmp packet at the end.
JMemory: JMemory#7fd8a0c94588class org.jnetpcap.packet.JPacket$State: size=344 bytes
JMemory: owner=packet.JScanner.class(size=631888/offset=534760)
JPacket.State#018: sizeof(packet_state_t)=184
JPacket.State#018: sizeof(header_t)=40 and *4=160
JPacket.State#018: pkt_header_map[0]=0x0000000000001007
JPacket.State#018: pkt_header_map[1]=0x0000000000000000
JPacket.State#018: pkt_header_map[2]=0x0000000000000000
JPacket.State#018: pkt_header_map[3]=0x0000000000000000
JPacket.State#018: pkt_flags=0x00000000
JPacket.State#018: pkt_header_count=4
JPacket.State#018: pkt_wirelen=500 bytes
JPacket.State#018: pkt_caplen=500 bytes
JPacket.State#018 : [ Protocol(ID/Flag) | Start | Prefix | Header | Gap | Payload | Postfix ]
JPacket.State#018[0]: [ ETHERNET( 1/0800) | 0 | 0 | 14 | 0 | 486 | 0 ]
JPacket.State#018[1]: [ IP4( 2/0800) | 14 | 0 | 20 | 0 | 466 | 0 ]
JPacket.State#018[2]: [ ICMP(12/0800) | 34 | 0 | 8 | 0 | 458 | 0 ]
JPacket.State#018[3]: [ PAYLOAD( 0/0800) | 42 | 0 | 458 | 0 | 0 | 0 ]
0000:*68 01 00 5e 00 00 01 5f 93 48 01 04 08 00*45 00 h..^..._.H....E.
0010: 01 e6 00 00 00 00 20 01 64 76 2d 37 80 71 58 c6 ...... .dv-7.qX.
0020: 2e 33*00 00 70 ed 00 00 00 00*00 00 00 00 53 87 .3..p.........S.
0030: 71 42 a6 f7 ad 57 f2 54 11 00 3e 61 ca 6e 49 b0 qB...W.T..>a.nI.
0040: a7 f7 59 4d b7 42 5e 05 0a f4 65 f9 5a 25 46 98 ..YM.B^...e.Z%F.
0050: dc ed 5a d0 2f a7 39 4d 02 73 bd c9 9f 11 bc ab ..Z./.9M.s......
0060: d2 70 34 9b d0 be 15 9c cc 88 3e 9e 4f f4 aa e3 .p4.......>.O...
0070: 98 b5 14 88 e8 2e f5 8b 16 f5 be 47 a1 e2 31 f9 ...........G..1.
0080: 82 68 d3 1e d1 5d fb d0 b9 7f 95 63 0d f6 67 be .h...].....c..g.
0090: 38 df 34 df b5 48 65 f0 ff 79 82 79 72 6d 6b b0 8.4..He..y.yrmk.
00a0: 21 ee 69 6b 0f 0a ba f2 f5 52 d0 93 c2 66 b9 16 !.ik.....R...f..
00b0: 2f 67 dc 56 48 27 40 85 0c 4d b4 b5 2f 81 bf 43 /g.VH'#..M../..C
00c0: 58 f5 2b 9d 19 87 b0 3e 14 a6 f9 d3 3e f7 b0 ca X.+....>....>...
00d0: 41 b1 73 05 89 6e 2c fb 6c 57 de 33 7f 5c 97 be A.s..n,.lW.3.\..
00e0: c1 d9 d0 f5 53 60 70 83 3a a6 3e 54 1e 8e ae 4e ....S`p.:.>T...N
00f0: 80 4b 24 7b 35 4e 48 f3 df 29 f1 c4 6e 5e f0 53 .K${5NH..)..n^.S
0100: 15 f7 18 89 1e 4d 41 93 26 8d 72 90 fa 83 ec a3 .....MA.&.r.....
0110: f8 d1 60 b6 63 49 a3 19 8c e3 c1 1b 20 b3 db 3c ..`.cI...... ..<
0120: a4 cd 42 f9 53 ee 9d 86 7f f3 8b a0 c2 ff f4 9e ..B.S...........
0130: a4 ce 58 28 e0 b9 c2 0f 13 bf 87 a5 85 91 ff e6 ..X(............
0140: 44 8c 93 43 33 b1 b4 ce 1c 02 22 19 9d da 28 7e D..C3....."...(~
0150: 49 a6 84 c9 97 4a 1e 27 27 66 5d f2 b1 45 15 73 I....J.''f]..E.s
0160: 05 22 d6 9c 06 54 e7 17 2d 1d f6 7b f5 3c 3a d0 ."...T..-..{.<:.
0170: 43 40 1e 3e 80 18 a3 eb 2d 2b c5 ad 7b 71 6b 83 C#.>....-+..{qk.
0180: 3f 2b d7 5d 19 01 4d fc 66 57 69 38 97 f8 b2 aa ?+.]..M.fWi8....
0190: 79 1c a7 0a b0 e5 33 7e b9 ef 23 27 78 a3 62 5a y.....3~..#'x.bZ
01a0: e7 26 e7 ff ce 34 31 86 ad d6 01 3c f8 7d 6c 30 .&...41....<.}l0
01b0: ed f7 62 d8 98 ce 21 bf e2 cd 3e 6a 8e a2 f0 48 ..b...!...>j...H
01c0: 30 62 5b 29 ed ab 91 82 d2 56 e8 12 15 2d 17 68 0b[).....V...-.h
01d0: a9 f5 d4 c9 95 3d b7 48 ce 91 c3 e7 a4 92 1f 6d .....=.H......m
01e0: e0 f1 0d ca 6a 67 a1 a3 60 22 6d 98 80 ac b7 62 ....jg..`"m....b
01f0: 03 7f 4a df*
My suggestion is to read and understand the code that is written by the author of jnetpcap.
Specifically the code that reads the data from a pcap file and creates a packet from scratch. Basically how the peering works.
Related
I have this problem constantly on my laptop. I usually go into the windowsbuilder GUI and click around, and the program crashes. Honestly the most annoying thing ever, and I am able to run much more cpu intensive programs on here. Anybody face this issue and resolve it somehow?
log summary from crash:
Command Line: -Dosgi.requiredJavaVersion=1.8 -Dosgi.instance.area.default=#user.home/eclipse-workspace -XX:+UseG1GC -XX:+UseStringDeduplication --add-modules=ALL-SYSTEM -Dosgi.requiredJavaVersion=1.8 -Dosgi.dataAreaRequiresExplicitInit=true -Xms256m -Xmx1024m --add-modules=ALL-SYSTEM
Host: Intel(R) Core(TM) i5-6300U CPU # 2.40GHz, 4 cores, 3G, Windows 10 , 64 bit Build 18362 (10.0.18362.329)
Time: Sat Oct 26 14:48:56 2019 Eastern Daylight Time elapsed time: 244 seconds (0d 0h 4m 4s)
--------------- T H R E A D ---------------
Current thread (0x00000000042c6800): JavaThread "main" [_thread_in_native, id=11372, stack(0x0000000000c00000,0x0000000000d00000)]
Stack: [0x0000000000c00000,0x0000000000d00000], sp=0x0000000000cf8ae0, free space=994k
Native frames: (J=compiled Java code, j=interpreted, Vv=VM code, C=native code)
C [MSCTF.dll+0x2aca8]
C [MSCTF.dll+0x2a6cd]
C [MSCTF.dll+0x426c9]
C [MSCTF.dll+0x3ca24]
C [MSCTF.dll+0x3563d]
C [MSCTF.dll+0x3bc5b]
C [MSCTF.dll+0x1eaab]
C [MSCTF.dll+0x1ed94]
C [USER32.dll+0x163ed]
C [USER32.dll+0x159fc]
C [USER32.dll+0x1238d]
C [USER32.dll+0x28972]
C [USER32.dll+0x12eb4]
C [USER32.dll+0x12cef]
C [USER32.dll+0x163ed]
C [USER32.dll+0x159fc]
C [USER32.dll+0x14e15]
C [USER32.dll+0x147f3]
C 0x00000000145f6ebe
Java frames: (J=compiled Java code, j=interpreted, Vv=VM code)
J 6439 org.eclipse.swt.internal.win32.OS.DefWindowProc(JIJJ)J (0 bytes) # 0x00000000145f6e1e [0x00000000145f6dc0+0x000000000000005e]
J 11969 c2 org.eclipse.swt.widgets.Scrollable.callWindowProc(JIJJ)J (21 bytes) # 0x0000000014414a78 [0x0000000014414a20+0x0000000000000058]
J 17209 c2 org.eclipse.swt.widgets.Control.windowProc(JIJJ)J (1980 bytes) # 0x0000000014b6902c [0x0000000014b68e00+0x000000000000022c]
J 14881 c2 org.eclipse.swt.widgets.Canvas.windowProc(JIJJ)J (50 bytes) # 0x00000000148b61f8 [0x00000000148b61a0+0x0000000000000058]
J 7487 c2 org.eclipse.swt.widgets.Display.windowProc(JJJJ)J (108 bytes) # 0x00000000143cffc0 [0x00000000143cff40+0x0000000000000080]
v ~StubRoutines::call_stub
J 6439 org.eclipse.swt.internal.win32.OS.DefWindowProc(JIJJ)J (0 bytes) # 0x00000000145f6e1e [0x00000000145f6dc0+0x000000000000005e]
J 11969 c2 org.eclipse.swt.widgets.Scrollable.callWindowProc(JIJJ)J (21 bytes) # 0x0000000014414a78 [0x0000000014414a20+0x0000000000000058]
J 17209 c2 org.eclipse.swt.widgets.Control.windowProc(JIJJ)J (1980 bytes) # 0x0000000014b6902c [0x0000000014b68e00+0x000000000000022c]
J 14881 c2 org.eclipse.swt.widgets.Canvas.windowProc(JIJJ)J (50 bytes) # 0x00000000148b61f8 [0x00000000148b61a0+0x0000000000000058]
J 7487 c2 org.eclipse.swt.widgets.Display.windowProc(JJJJ)J (108 bytes) # 0x00000000143cffc0 [0x00000000143cff40+0x0000000000000080]
v ~StubRoutines::call_stub
j org.eclipse.swt.internal.win32.OS.ImmSetCompositionFont(JLorg/eclipse/swt/internal/win32/LOGFONT;)Z+0
j org.eclipse.swt.widgets.Caret.setIMEFont()V+109
j org.eclipse.swt.widgets.Caret.setFocus()V+84
j org.eclipse.swt.widgets.Canvas.WM_SETFOCUS(JJ)Lorg/eclipse/swt/internal/win32/LRESULT;+29
J 17209 c2 org.eclipse.swt.widgets.Control.windowProc(JIJJ)J (1980 bytes) # 0x0000000014b6a118 [0x0000000014b68e00+0x0000000000001318]
J 14881 c2 org.eclipse.swt.widgets.Canvas.windowProc(JIJJ)J (50 bytes) # 0x00000000148b61f8 [0x00000000148b61a0+0x0000000000000058]
J 7487 c2 org.eclipse.swt.widgets.Display.windowProc(JJJJ)J (108 bytes) # 0x00000000143cffc0 [0x00000000143cff40+0x0000000000000080]
v ~StubRoutines::call_stub
j org.eclipse.swt.internal.win32.OS.SetFocus(J)J+0
j org.eclipse.swt.widgets.Control.forceFocus()Z+69
j org.eclipse.swt.widgets.Control.setFocus()Z+18
j org.eclipse.swt.widgets.Composite.setFocus()Z+65
j org.eclipse.wb.internal.core.editor.multi.SourcePage.setFocus()V+4
j org.eclipse.wb.internal.core.editor.multi.DefaultMultiMode.showPage(Lorg/eclipse/wb/core/editor/IEditorPage;)V+100
j org.eclipse.wb.internal.core.editor.multi.MultiPagesMode.showSource()V+16
j org.eclipse.wb.internal.core.editor.DesignPage$9.openSourcePosition(I)V+21
j org.eclipse.wb.internal.core.gefTree.policy.dblclick.OpenEditorLayoutEditPolicy.performDoubleClick()V+24
j org.eclipse.wb.internal.core.gefTree.policy.dblclick.DoubleClickLayoutEditPolicy.performRequest(Lorg/eclipse/wb/gef/core/requests/Request;)V+13
j org.eclipse.wb.gef.core.EditPart.performRequest(Lorg/eclipse/wb/gef/core/requests/Request;)V+31
j org.eclipse.wb.internal.gef.tree.tools.DoubleClickEditPartTracker.handleDoubleClick(I)V+28
j org.eclipse.wb.gef.core.tools.Tool.mouseDoubleClick(Lorg/eclipse/swt/events/MouseEvent;Lorg/eclipse/wb/gef/core/IEditPartViewer;)V+15
j org.eclipse.wb.gef.graphical.tools.SelectionTool.mouseDoubleClick(Lorg/eclipse/swt/events/MouseEvent;Lorg/eclipse/wb/gef/core/IEditPartViewer;)V+19
j org.eclipse.wb.internal.gef.core.EditDomain.mouseDoubleClick(Lorg/eclipse/swt/events/MouseEvent;Lorg/eclipse/wb/gef/core/IEditPartViewer;)V+17
j org.eclipse.wb.internal.gef.tree.TreeEventManager.mouseDoubleClick(Lorg/eclipse/swt/events/MouseEvent;)V+16
v ~StubRoutines::call_stub
J 6407 jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Ljava/lang/reflect/Method;Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object; java.base#13 (0 bytes) # 0x00000000145f4ecf [0x00000000145f4e40+0x000000000000008f]
J 6282 c1 jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object; java.base#13 (104 bytes) # 0x000000000da822f4 [0x000000000da81640+0x0000000000000cb4]
J 6331 c2 jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object; java.base#13 (10 bytes) # 0x00000000145e6774 [0x00000000145e6720+0x0000000000000054]
J 15057 c1 org.eclipse.wb.internal.draw2d.EventManager$8.invoke(Ljava/lang/Object;Ljava/lang/reflect/Method;[Ljava/lang/Object;)Ljava/lang/Object; (40 bytes) # 0x000000000e98873c [0x000000000e9882c0+0x000000000000047c]
j com.sun.proxy.$Proxy22.mouseDoubleClick(Lorg/eclipse/swt/events/MouseEvent;)V+16
J 11845 c1 org.eclipse.swt.widgets.TypedListener.handleEvent(Lorg/eclipse/swt/widgets/Event;)V (1291 bytes) # 0x000000000e0879ac [0x000000000e081f80+0x0000000000005a2c]
J 17061 c2 org.eclipse.swt.widgets.EventTable.sendEvent(Lorg/eclipse/swt/widgets/Event;)V (592 bytes) # 0x0000000014b4be6c [0x0000000014b4bdc0+0x00000000000000ac]
J 15101 c2 org.eclipse.swt.widgets.Display.sendEvent(Lorg/eclipse/swt/widgets/EventTable;Lorg/eclipse/swt/widgets/Event;)V (34 bytes) # 0x0000000014a2f630 [0x0000000014a2f4c0+0x0000000000000170]
J 10559 c1 org.eclipse.swt.widgets.Display.runDeferredEvents()Z (109 bytes) # 0x000000000dd272bc [0x000000000dd26a40+0x000000000000087c]
J 17785 c1 org.eclipse.swt.widgets.Display.readAndDispatch()Z (94 bytes) # 0x000000000ee8e2e4 [0x000000000ee8dc80+0x0000000000000664]
j org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine$5.run()V+564
j org.eclipse.core.databinding.observable.Realm.runWithDefault(Lorg/eclipse/core/databinding/observable/Realm;Ljava/lang/Runnable;)V+12
j org.eclipse.e4.ui.internal.workbench.swt.PartRenderingEngine.run(Lorg/eclipse/e4/ui/model/application/MApplicationElement;Lorg/eclipse/e4/core/contexts/IEclipseContext;)Ljava/lang/Object;+57
j org.eclipse.e4.ui.internal.workbench.E4Workbench.createAndRunUI(Lorg/eclipse/e4/ui/model/application/MApplicationElement;)V+20
j org.eclipse.ui.internal.Workbench.lambda$3(Lorg/eclipse/swt/widgets/Display;Lorg/eclipse/ui/application/WorkbenchAdvisor;[I)V+379
j org.eclipse.ui.internal.Workbench$$Lambda$109.run()V+12
j org.eclipse.core.databinding.observable.Realm.runWithDefault(Lorg/eclipse/core/databinding/observable/Realm;Ljava/lang/Runnable;)V+12
j org.eclipse.ui.internal.Workbench.createAndRunWorkbench(Lorg/eclipse/swt/widgets/Display;Lorg/eclipse/ui/application/WorkbenchAdvisor;)I+16
j org.eclipse.ui.PlatformUI.createAndRunWorkbench(Lorg/eclipse/swt/widgets/Display;Lorg/eclipse/ui/application/WorkbenchAdvisor;)I+2
j org.eclipse.ui.internal.ide.application.IDEApplication.start(Lorg/eclipse/equinox/app/IApplicationContext;)Ljava/lang/Object;+113
j org.eclipse.equinox.internal.app.EclipseAppHandle.run(Ljava/lang/Object;)Ljava/lang/Object;+138
j org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.runApplication(Ljava/lang/Object;)Ljava/lang/Object;+85
j org.eclipse.core.runtime.internal.adaptor.EclipseAppLauncher.start(Ljava/lang/Object;)Ljava/lang/Object;+82
j org.eclipse.core.runtime.adaptor.EclipseStarter.run(Ljava/lang/Object;)Ljava/lang/Object;+105
j org.eclipse.core.runtime.adaptor.EclipseStarter.run([Ljava/lang/String;Ljava/lang/Runnable;)Ljava/lang/Object;+132
v ~StubRoutines::call_stub
j jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Ljava/lang/reflect/Method;Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;+0 java.base#13
j jdk.internal.reflect.NativeMethodAccessorImpl.invoke(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;+100 java.base#13
j jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;+6 java.base#13
j java.lang.reflect.Method.invoke(Ljava/lang/Object;[Ljava/lang/Object;)Ljava/lang/Object;+59 java.base#13
j org.eclipse.equinox.launcher.Main.invokeFramework([Ljava/lang/String;[Ljava/net/URL;)V+201
j org.eclipse.equinox.launcher.Main.basicRun([Ljava/lang/String;)V+159
j org.eclipse.equinox.launcher.Main.run([Ljava/lang/String;)I+4
v ~StubRoutines::call_stub
siginfo: EXCEPTION_ACCESS_VIOLATION (0xc0000005), reading address 0x0000000000000020
Register to memory mapping:
RIP=0x00007ffc3deaaca8 MSCTF.dll
RAX=0x0 is NULL
RBX=0x0000000027e5e9f0 points into unknown readable memory: 78 31 f7 3d fc 7f 00 00
RCX=0x000000002d234c10 points into unknown readable memory: 00 00 00 00 00 00 00 00
RDX=0x0000000000000002 is an unknown value
RSP=0x0000000000cf8ae0 is pointing into the stack for thread: 0x00000000042c6800
RBP=0x0 is NULL
RSI=0x0 is NULL
RDI=0x0000000040000000 is pointing into metadata
R8 =0x000000002bd227c0 points into unknown readable memory: 30 2e f7 3d fc 7f 00 00
R9 =0x0 is NULL
R10=0x000000000167a0c0 points into unknown readable memory: 01 00 00 00 5e 23 7e 23
R11=0x0 is NULL
R12=
[error occurred during error reporting (printing register info), id 0xc0000005, EXCEPTION_ACCESS_VIOLATION (0xc0000005) at pc=0x00007ffbfe38872f]
Registers:
RAX=0x0000000000000000, RBX=0x0000000027e5e9f0, RCX=0x000000002d234c10, RDX=0x0000000000000002
RSP=0x0000000000cf8ae0, RBP=0x0000000000000000, RSI=0x0000000000000000, RDI=0x0000000040000000
R8 =0x000000002bd227c0, R9 =0x0000000000000000, R10=0x000000000167a0c0, R11=0x0000000000000000
R12=0x00000000ffffffff, R13=0x0000000000560183, R14=0x0000000000000000, R15=0x0000000000000000
RIP=0x00007ffc3deaaca8, EFLAGS=0x0000000000010202
Top of Stack: (sp=0x0000000000cf8ae0)
0x0000000000cf8ae0: 0000000000000000 0000000000000000
0x0000000000cf8af0: 0000000008200000 0000000000cf8d00
0x0000000000cf8b00: 00007ffc3ed07d00 00007ffc3c688ba3
0x0000000000cf8b10: 0000000000000000 00007ffc00000000
0x0000000000cf8b20: 0000000000cf9240 0000000000000001
0x0000000000cf8b30: 0000c1973cfd97bf 0000000000000000
0x0000000000cf8b40: 0000000000000001 0000000000000001
0x0000000000cf8b50: 0000000000000000 0000000000000002
0x0000000000cf8b60: 0000000027e5ea28 00007ffc3deaa6cd
0x0000000000cf8b70: 0000000000000000 0000000000000002
0x0000000000cf8b80: 0000000000000000 0000000000000000
0x0000000000cf8b90: 0000000000000000 00007ffc3ea7a7da
0x0000000000cf8ba0: 00000000230a7400 00007ffc3ea35810
0x0000000000cf8bb0: 0000000000000000 000000003b2fca70
0x0000000000cf8bc0: 000000000000000f 000000002bcd5de0
0x0000000000cf8bd0: 0000000000010702 0000000000cf8d40
Instructions: (pc=0x00007ffc3deaaca8)
0x00007ffc3deaaba8: 00 00 48 85 c9 74 1e 48 8b 01 48 8b 40 30 83 bb
0x00007ffc3deaabb8: 10 01 00 00 00 8b 93 e8 00 00 00 41 0f 95 c1 ff
0x00007ffc3deaabc8: 15 fb 0e 0d 00 48 8b 8b 30 01 00 00 48 85 c9 0f
0x00007ffc3deaabd8: 85 15 f8 02 00 89 bb f0 00 00 00 e9 79 fe ff ff
0x00007ffc3deaabe8: 4c 89 6c 24 68 4c 8b e6 4c 8b ab a8 00 00 00 4d
0x00007ffc3deaabf8: 8b 7e 08 49 8b 2e 41 8b 76 10 89 bb f0 00 00 00
0x00007ffc3deaac08: 4d 85 ed 0f 85 d5 f5 02 00 49 83 c6 18 89 bb f0
0x00007ffc3deaac18: 00 00 00 49 83 ec 01 75 cf 8b 74 24 38 33 ed 4c
0x00007ffc3deaac28: 8b 6c 24 68 44 8b fd 85 f6 0f 8e e1 fc ff ff 48
0x00007ffc3deaac38: 8b bb 58 02 00 00 41 bf 01 00 00 00 48 85 ff 0f
0x00007ffc3deaac48: 84 cb fc ff ff e9 d7 f5 02 00 0f 1f 40 00 66 66
0x00007ffc3deaac58: 0f 1f 84 00 00 00 00 00 8b c6 0f af 47 3c 4c 63
0x00007ffc3deaac68: f0 4c 03 77 30 49 8b 0e 48 85 c9 74 0d 48 8b 01
0x00007ffc3deaac78: 48 8b 40 10 ff 15 46 0e 0d 00 49 8b 4e 08 48 85
0x00007ffc3deaac88: c9 74 0d 48 8b 01 48 8b 40 10 ff 15 30 0e 0d 00
0x00007ffc3deaac98: ff c6 3b 77 38 0f 8d 8e fd ff ff eb bb 48 8b 01
0x00007ffc3deaaca8: 48 8b 40 20 e9 05 ff ff ff 4d 8b 66 48 4d 85 e4
0x00007ffc3deaacb8: 0f 85 c0 f5 02 00 4d 8b 76 18 4d 85 f6 0f 84 5d
0x00007ffc3deaacc8: fc ff ff eb e4 0f 1f 00 8b 47 64 0f af c6 48 63
0x00007ffc3deaacd8: c8 48 03 4f 58 83 79 04 00 74 1a 48 8b 49 08 48
0x00007ffc3deaace8: 85 c9 74 11 48 8b 01 ba 01 00 00 00 48 8b 00 ff
0x00007ffc3deaacf8: 15 cb 0d 0d 00 ff c6 3b 77 60 0f 8d 42 fd ff ff
0x00007ffc3deaad08: eb c6 ff 15 40 06 0d 00 e9 42 fd ff ff cc cc cc
0x00007ffc3deaad18: cc cc cc cc cc cc cc cc 48 89 5c 24 08 57 48 83
0x00007ffc3deaad28: ec 20 48 8d 05 4f 92 0c 00 8b da 48 89 01 48 8b
0x00007ffc3deaad38: f9 e8 62 f7 ff ff 48 8d 05 4b 92 0c 00 48 89 07
0x00007ffc3deaad48: f6 c3 01 74 09 48 8b cf ff 15 fa 05 0d 00 48 8b
0x00007ffc3deaad58: 5c 24 30 48 8b c7 48 83 c4 20 5f c3 cc cc cc cc
0x00007ffc3deaad68: cc cc cc cc 40 53 48 83 ec 20 48 8d 05 d7 91 0c
0x00007ffc3deaad78: 00 48 8b d9 48 89 01 48 8d 05 f2 91 0c 00 83 61
0x00007ffc3deaad88: 18 00 48 83 61 10 00 83 61 1c 00 48 89 41 08 ff
0x00007ffc3deaad98: 15 8b 05 0d 00 48 89 43 20 48 8d 05 f8 91 0c 00
Stack slot to memory mapping:
stack at sp + 0 slots: 0x0 is NULL
stack at sp + 1 slots: 0x0 is NULL
stack at sp + 2 slots: 0x0000000008200000 points into unknown readable memory: 00 05 0e 66 07 05 27 fa
stack at sp + 3 slots: 0x0000000000cf8d00 is pointing into the stack for thread: 0x00000000042c6800
stack at sp + 4 slots: 0x00007ffc3ed07d00 combase.dll
stack at sp + 5 slots: 0x00007ffc3c688ba3 KERNELBASE.dll
stack at sp + 6 slots: 0x0 is NULL
stack at sp + 7 slots: 0x00007ffc00000000 is an unknown value
I wrote the below JavaCard program to Sign a 4 byte length data based on ALG_RSA_SHA_256_PKCS1 algorithm and also to verify this signature:
package fileSigner;
import javacard.framework.*;
import javacard.security.KeyPair;
import javacard.security.RSAPrivateKey;
import javacard.security.RSAPublicKey;
import javacard.security.Signature;
public class FileSigning extends Applet {
//Proprietary Status Words
private static final short SIGN_VERIFIED_SW = (short) 0x6701;
private static final short SIGN_NOT_VERIFIED_SW = (short) 0x6702;
//Proprietary Instructions
private static final byte SIGN_INS = (byte) 0x00;
private static final byte VERIFY_INS = (byte) 0x02;
private static final byte RET_PUB_KEY_INS = (byte) 0x04;
//Required Objects
private static RSAPrivateKey privateKey;
private static RSAPublicKey publicKey;
private static KeyPair keyPair;
private static Signature signature;
public static void install(byte[] bArray, short bOffset, byte bLength) {
new FileSigning();
}
protected FileSigning() {
register();
keyPair = new KeyPair(KeyPair.ALG_RSA, (short) 1024);
keyPair.genKeyPair();
publicKey = (RSAPublicKey) keyPair.getPublic();
privateKey = (RSAPrivateKey) keyPair.getPrivate();
signature = Signature.getInstance(Signature.ALG_RSA_SHA_256_PKCS1, false);
}
public void process(APDU apdu) {
if (selectingApplet()) {
return;
}
byte[] buffer = apdu.getBuffer();
switch (buffer[ISO7816.OFFSET_INS]) {
case SIGN_INS:
signature.init(privateKey, Signature.MODE_SIGN);
byte[] dataSignature = JCSystem.makeTransientByteArray((short) 128, JCSystem.CLEAR_ON_RESET);
short signLen = signature.sign(buffer, ISO7816.OFFSET_CDATA, (byte) ISO7816.OFFSET_LC, dataSignature, (byte) 0);
Util.arrayCopyNonAtomic(dataSignature, (short) 0, buffer, (short) 0, signLen);
apdu.setOutgoingAndSend((short) 0, signLen);
break;
case VERIFY_INS:
signature.init(privateKey, Signature.MODE_VERIFY);
boolean isVerified = signature.verify(buffer, (short)ISO7816.OFFSET_CDATA, (short)0x80, buffer, (short)(ISO7816.OFFSET_CDATA + 0X80), (short)0X04);
if (isVerified) {
ISOException.throwIt(SIGN_VERIFIED_SW);
} else {
ISOException.throwIt(SIGN_NOT_VERIFIED_SW);
}
break;
case RET_PUB_KEY_INS:
break;
default:
ISOException.throwIt((short) ISO7816.SW_INS_NOT_SUPPORTED);
}
}
}
Well, It seems that it works fine for sign method.
For example in the follow I sign 11223344:
C:\OpenSCTool:> OSC.exe -s 00a4040006010203040501 -s 000000000411223344
Using reader with a card: ACS CCID USB Reader 0
Sending: 00 A4 04 00 06 01 02 03 04 05 01
Received (SW1=0x90, SW2=0x00)
Sending: 00 00 00 00 04 11 22 33 44
Received (SW1=0x90, SW2=0x00):
7B EE DE 3E 16 D6 82 CF 67 53 CC AF 87 F3 4F 31 {..>....gS....O1
BB A6 66 B6 4E E3 F5 FD 0E 04 18 24 63 C1 98 D4 ..f.N......$c...
17 CC 56 DA A0 5F 78 4D A5 AE DF E4 A6 E0 35 B6 ..V.._xM......5.
F0 F3 59 45 1E 89 EE 02 69 15 8C 27 7B 10 94 02 ..YE....i..'{...
F1 C5 A4 1C F2 3C 1A 75 8A FC 89 4F 59 A4 D5 A0 .....<.u...OY...
84 C5 5E 70 F8 B7 80 10 3D D3 84 56 EC CB 1D 01 ..^p....=..V....
D2 F4 F0 F4 FD 91 5A 52 45 17 A7 08 9D 26 82 A7 ......ZRE....&..
E3 67 A6 2C D8 CC 7F 59 F1 98 4A 4F 5D 78 63 DD .g.,...Y..JO]xc.
But I have problems with the verify method!
In the below I tried to verify the above signature:
C:\OpenSCTool:> OSC.exe -s 00a4040006010203040501 -s 00020000847BEEDE3E16D682CF6
753CCAF87F34F31BBA666B64EE3F5FD0E04182463C198D417CC56DAA05F784DA5AEDFE4A6E035B6F
0F359451E89EE0269158C277B109402F1C5A41CF23C1A758AFC894F59A4D5A084C55E70F8B780103
DD38456ECCB1D01D2F4F0F4FD915A524517A7089D2682A7E367A62CD8CC7F59F1984A4F5D7863DD1
1223344
Using reader with a card: ACS CCID USB Reader 0
Sending: 00 A4 04 00 06 01 02 03 04 05 01
Received (SW1=0x90, SW2=0x00)
Sending: 00 02 00 00 84 7B EE DE 3E 16 D6 82 CF 67 53 CC AF 87 F3 4F 31 BB A6 66
B6 4E E3 F5 FD 0E 04 18 24 63 C1 98 D4 17 CC 56 DA A0 5F 78 4D A5 AE DF E4 A6 E
0 35 B6 F0 F3 59 45 1E 89 EE 02 69 15 8C 27 7B 10 94 02 F1 C5 A4 1C F2 3C 1A 75
8A FC 89 4F 59 A4 D5 A0 84 C5 5E 70 F8 B7 80 10 3D D3 84 56 EC CB 1D 01 D2 F4 F0
F4 FD 91 5A 52 45 17 A7 08 9D 26 82 A7 E3 67 A6 2C D8 CC 7F 59 F1 98 4A 4F 5D 7
8 63 DD 11 22 33 44
Received (SW1=0x6F, SW2=0x00)
Update1:
I was wrongly wrote
signature.init(privateKey, Signature.MODE_VERIFY);
instead of
signature.init(publicKey, Signature.MODE_VERIFY);
in the section of VERIFY_INS. So I correct it and now the output is as below:
C:\OpenSCTool:> OSC.exe -s 00a4040006010203040501 -s 000000000411223344
Using reader with a card: ACS CCID USB Reader 0
Sending: 00 A4 04 00 06 01 02 03 04 05 01
Received (SW1=0x90, SW2=0x00)
Sending: 00 00 00 00 04 11 22 33 44
Received (SW1=0x90, SW2=0x00):
73 87 1C 58 07 EB F5 55 42 E3 37 FC 84 3A CA 91 s..X...UB.7..:..
FB F4 4D AD 70 FC 43 84 C0 26 DC AE 03 47 28 F3 ..M.p.C..&...G(.
F6 77 6B 53 93 96 92 4B 66 C8 BA 05 0F 65 56 CE .wkS...Kf....eV.
0E 7A 56 DE 5A 9C FF 4C FB B5 82 33 81 22 BD BE .zV.Z..L...3."..
76 99 15 87 5D 9C 4C 5F 6A 75 38 99 A9 28 D6 45 v...].L_ju8..(.E
A5 F4 8C A2 AE 89 28 49 ED 72 14 FD 7E 3C 6A F4 ......(I.r..~<j.
EA C2 7C BD AB D3 B3 91 1C 24 E0 29 B4 9C 07 82 ..|......$.)....
32 B1 BF 00 A5 A0 82 48 C2 01 82 A1 90 5A 47 05 2......H.....ZG.
C:\OpenSCTool:> OSC.exe -s 00a4040006010203040501 -s 000200008473871C5807EBF5554
2E337FC843ACA91FBF44DAD70FC4384C026DCAE034728F3F6776B539396924B66C8BA050F6556CE0
E7A56DE5A9CFF4CFBB582338122BDBE769915875D9C4C5F6A753899A928D645A5F48CA2AE892849E
D7214FD7E3C6AF4EAC27CBDABD3B3911C24E029B49C078232B1BF00A5A08248C20182A1905A47051
1223344
Using reader with a card: ACS CCID USB Reader 0
Sending: 00 A4 04 00 06 01 02 03 04 05 01
Received (SW1=0x90, SW2=0x00)
Sending: 00 02 00 00 84 73 87 1C 58 07 EB F5 55 42 E3 37 FC 84 3A CA 91 FB F4 4D
AD 70 FC 43 84 C0 26 DC AE 03 47 28 F3 F6 77 6B 53 93 96 92 4B 66 C8 BA 05 0F 6
5 56 CE 0E 7A 56 DE 5A 9C FF 4C FB B5 82 33 81 22 BD BE 76 99 15 87 5D 9C 4C 5F
6A 75 38 99 A9 28 D6 45 A5 F4 8C A2 AE 89 28 49 ED 72 14 FD 7E 3C 6A F4 EA C2 7C
BD AB D3 B3 91 1C 24 E0 29 B4 9C 07 82 32 B1 BF 00 A5 A0 82 48 C2 01 82 A1 90 5
A 47 05 11 22 33 44
Received (SW1=0x67, SW2=0x02)
As you see above, the card couldn't verify the signature that it create itself.
Updata2:
Based on dear Mr Maarten's answer I added setIncomingAndReceive() method to my program as below:
public class FileSigning extends Applet {
//Proprietary Status Words
private static final short SIGN_VERIFIED_SW = (short) 0x6701;
private static final short SIGN_NOT_VERIFIED_SW = (short) 0x6702;
//Proprietary Instructions
private static final byte SIGN_INS = (byte) 0x00;
private static final byte VERIFY_INS = (byte) 0x02;
private static final byte RET_PUB_KEY_INS = (byte) 0x04;
//Required Objects
private static RSAPrivateKey privateKey;
private static RSAPublicKey publicKey;
private static KeyPair keyPair;
private static Signature signature;
public static void install(byte[] bArray, short bOffset, byte bLength) {
new FileSigning();
}
protected FileSigning() {
register();
keyPair = new KeyPair(KeyPair.ALG_RSA, (short) 1024);
keyPair.genKeyPair();
publicKey = (RSAPublicKey) keyPair.getPublic();
privateKey = (RSAPrivateKey) keyPair.getPrivate();
signature = Signature.getInstance(Signature.ALG_RSA_SHA_256_PKCS1, false);
}
public void process(APDU apdu) {
if (selectingApplet()) {
return;
}
byte[] buffer = apdu.getBuffer();
byte[] incomingData = JCSystem.makeTransientByteArray((short) 256, JCSystem.CLEAR_ON_RESET);
short bytesLeft;
short readCount;
short offSet = 0x00;
switch (buffer[ISO7816.OFFSET_INS]) {
case SIGN_INS:
signature.init(privateKey, Signature.MODE_SIGN);
bytesLeft = (short) (buffer[ISO7816.OFFSET_LC] & 0x00FF);
readCount = apdu.setIncomingAndReceive();
while (bytesLeft > 0) {
Util.arrayCopyNonAtomic(buffer, ISO7816.OFFSET_CDATA, incomingData, offSet, readCount);
bytesLeft -= readCount;
offSet += readCount;
readCount = apdu.receiveBytes(ISO7816.OFFSET_CDATA);
}
short signLen = signature.sign(buffer, ISO7816.OFFSET_CDATA, (byte) ISO7816.OFFSET_LC, incomingData, (byte) 0);
Util.arrayCopyNonAtomic(incomingData, (short) 0, buffer, (short) 0, signLen);
apdu.setOutgoingAndSend((short) 0, signLen);
break;
case VERIFY_INS:
signature.init(publicKey, Signature.MODE_VERIFY);
bytesLeft = (short) (buffer[ISO7816.OFFSET_LC] & 0x00FF);
readCount = apdu.setIncomingAndReceive();
while (bytesLeft > 0) {
Util.arrayCopyNonAtomic(buffer, ISO7816.OFFSET_CDATA, incomingData, offSet, readCount);
bytesLeft -= readCount;
offSet += readCount;
readCount = apdu.receiveBytes(ISO7816.OFFSET_CDATA);
}
boolean isVerified = signature.verify(incomingData, (short) 0, (short) 0x80, incomingData, (short) 0X80, (short) 0X04);
if (isVerified) {
ISOException.throwIt(SIGN_VERIFIED_SW);
} else {
ISOException.throwIt(SIGN_NOT_VERIFIED_SW);
}
break;
case RET_PUB_KEY_INS:
break;
default:
ISOException.throwIt((short) ISO7816.SW_INS_NOT_SUPPORTED);
}
}
}
Well, it seems that there is something wrong still:
C:\OpenSCTool:> OSC.exe -s 00a4040006010203040501 -s 000000000411223344
Using reader with a card: ACS CCID USB Reader 0
Sending: 00 A4 04 00 06 01 02 03 04 05 01
Received (SW1=0x90, SW2=0x00)
Sending: 00 00 00 00 04 11 22 33 44
Received (SW1=0x90, SW2=0x00):
75 08 A1 D0 D7 C6 9E 60 3E 78 F0 3A 6B 50 A0 D8 u......`>x.:kP..
80 2E F3 3E 29 2C 0E 13 15 DC 78 74 14 33 4D 36 ...>),....xt.3M6
7E 5E C5 65 67 92 A0 7E B4 0A A1 C0 DE F2 63 44 ~^.eg..~......cD
84 E3 20 CC 48 96 6E B2 28 9A 1B 07 53 ED 70 AF .. .H.n.(...S.p.
8E 01 24 E0 B9 80 89 98 ED B7 A0 BB 81 37 81 22 ..$..........7."
1C C8 54 A4 91 D0 8D 83 12 31 41 1A 56 76 23 D4 ..T......1A.Vv#.
09 5C BD 1E 75 A6 D8 3A 57 15 D8 5E B0 B9 B5 E7 .\..u..:W..^....
32 46 BE C5 A8 58 79 1B D4 C5 20 DD 48 D3 70 CB 2F...Xy... .H.p.
C:\OpenSCTool:> OSC.exe -s 00a4040006010203040501 -s 00020000847508A1D0D7C69E603
E78F03A6B50A0D8802EF33E292C0E1315DC787414334D367E5EC5656792A07EB40AA1C0DEF263448
4E320CC48966EB2289A1B0753ED70AF8E0124E0B9808998EDB7A0BB813781221CC854A491D08D831
231411A567623D4095CBD1E75A6D83A5715D85EB0B9B5E73246BEC5A858791BD4C520DD48D370CB1
1223344
Using reader with a card: ACS CCID USB Reader 0
Sending: 00 A4 04 00 06 01 02 03 04 05 01
Received (SW1=0x90, SW2=0x00)
Sending: 00 02 00 00 84 75 08 A1 D0 D7 C6 9E 60 3E 78 F0 3A 6B 50 A0 D8 80 2E F3
3E 29 2C 0E 13 15 DC 78 74 14 33 4D 36 7E 5E C5 65 67 92 A0 7E B4 0A A1 C0 DE F
2 63 44 84 E3 20 CC 48 96 6E B2 28 9A 1B 07 53 ED 70 AF 8E 01 24 E0 B9 80 89 98
ED B7 A0 BB 81 37 81 22 1C C8 54 A4 91 D0 8D 83 12 31 41 1A 56 76 23 D4 09 5C BD
1E 75 A6 D8 3A 57 15 D8 5E B0 B9 B5 E7 32 46 BE C5 A8 58 79 1B D4 C5 20 DD 48 D
3 70 CB 11 22 33 44
Received (SW1=0x67, SW2=0x02)
Your verify() arguments are probably swapped (given you have [128 bytes signature][4 bytes data] in the incomingData):
boolean isVerified = signature.verify(incomingData, (short)0x80, (short) 4, incomingData, (short)0, (short)0x80);
Your java card code needs some polishing, for example the dataSignature/incomingData buffers should be allocated only once in the constructor (and CLEAR_ON_DESELECT should suffice).
You are forgetting to call apdu.setIncomingAndSend(), so all data may not be in the buffer. For small data (4 bytes) it usually is, but for larger data (i.e. the one that includes the signature for verification, it isn't.
I had some problems about encoding/decoding audio in my wrapper code, I debugged my code and finally found out that SetByteArrayRegion does not work as expected. Here we go:
// incoming parameters => jbyteArray jencoded
jbyte encoded[320]; //
... // fill encoded with 'encodedSize' number of bytes
int i;
for (i = 0; i < encodedSize; i++) {
fprintf(encodeDbg, "%02X ", (unsigned char)(encoded[i]));
}
fprintf(encodeDbg, "\n");
fflush(encodeDbg);
(*env)->SetByteArrayRegion(env, jencoded, 0, encodedSize, encoded);
jbyte* encoded_test = (*env)->GetByteArrayElements(env, jencoded, NULL);
for (i = 0; i < encodedSize; i++) {
fprintf(encodeDbg, "%02X ", (unsigned char)(encoded_test[i]));
}
fprintf(encodeDbg, "\n");
fflush(encodeDbg);
(*env)->ReleaseByteArrayElements(env, jencoded, encoded_test, JNI_ABORT);
The values in encoded and jencoded (i.e. encoded_test) do not match! After calling this procedure multiple times, the content of encodeDbg file is as follows:
08 40 64 6A 6A 00 00 00 61 16 0D 2B 2D 7C F1 EF 8B D0 AE D3 21 39
D0 F4 40 1B 00 00 00 00 16 00 00 00 00 F3 40 1B 8B D0 AE D3 21 39
08 40 64 6A 6A 00 00 00 74 DB F7 02 23 18 01 86 8E 6D 4A 56 9E 9C 64 41 43 E8 09
D0 F4 40 1B 00 00 00 00 1B 00 00 00 00 F3 40 1B 8E 6D 4A 56 9E 9C 64 41 43 E8 09
08 40 64 6A 6A 00 00 00 D5 A4 A3 5E A3 DC B1 3B 2D 3D 6C 4C A4
D0 F4 40 1B 00 00 00 00 15 00 00 00 00 F3 40 1B 2D 3D 6C 4C A4
08 40 64 6A 6A 00 00 00 AC 59 7A 0E 3D A1 50 43 57 20 58 41 5F 5C 74 88 22 1B 5D
D0 F4 40 1B 00 00 00 00 1B 00 00 00 00 F3 40 1B 57 20 58 41 5F 5C 74 88 22 1B 5D
08 40 64 6A 6A 00 00 00 73 33 13 51 56 77 BF 41 93 0A 61 9C 57 30 A3 DB B9 80
D0 F4 40 1B 00 00 00 00 1A 00 00 00 00 F3 40 1B 93 0A 61 9C 57 30 A3 DB B9 80
08 40 64 6A 6A 00 00 00 9C 05 BB 57 32 9B 14 2B 9E A1 87 DF EF 55 7F 4E B5 FD C2
D0 F4 40 1B 00 00 00 00 1B 00 00 00 00 F3 40 1B 9E A1 87 DF EF 55 7F 4E B5 FD C2
08 40 64 6A 6A 00 00 00 03 4C 7F ED 08 28 18 5D E1 23 D9 7C DD E8 27 BC 46 12 FA 0D 0E 56 29 C2 14 58
D0 F4 40 1B 00 00 00 00 22 00 00 00 00 F3 40 1B E1 23 D9 7C DD E8 27 BC 46 12 FA 0D 0E 56 29 C2 14 58
08 40 64 6A 6A 00 00 00 77 09 3F 7E 57 78 53 64 E2 39 74 6B E1 E5 AB 2C EB C4 D7 FE 80
D0 F4 40 1B 00 00 00 00 1D 00 00 00 00 F3 40 1B E2 39 74 6B E1 E5 AB 2C EB C4 D7 FE 80
There is a problem with the first 16 bytes, while the rest is of the array is ok. Any suggestion is highly appreciated.
EDIT:
I compile the JNI part (C stuff) on linux (with mingw), and copy the dll to Windows, where I run the Java application. May this cross platform operations cause the problem? Do I need to add some extra flag/parameter to the compiler or linker?
SECOND EDIT:
I slightly changed the code to see the contents of encoded after SetByteArrayRegion call. The code and the output is as following:
// incoming parameters => jbyteArray jencoded
jbyte encoded[320]; //
... // fill encoded with 'encodedSize' number of bytes
int i;
for (i = 0; i < encodedSize; i++) {
fprintf(encodeDbg, "%02X ", (unsigned char)(encoded[i]));
}
fprintf(encodeDbg, "\n");
fflush(encodeDbg);
(*env)->SetByteArrayRegion(env, jencoded, 0, encodedSize, encoded);
jbyte* encoded_test = (*env)->GetByteArrayElements(env, jencoded, NULL);
for (i = 0; i < encodedSize; i++) {
fprintf(encodeDbg, "%02X ", (unsigned char)(encoded_test[i]));
}
fprintf(encodeDbg, "\n");
for (i = 0; i < encodedSize; i++) {
fprintf(encodeDbg, "%02X ", (unsigned char)(encoded[i]));
}
fprintf(encodeDbg, "\n\n");
fflush(encodeDbg);
(*env)->ReleaseByteArrayElements(env, jencoded, encoded_test, JNI_ABORT);
Sample output:
08 40 64 6A 6A 00 00 00 61 16 0D 2B 2D 7C F1 EF 8B D0 AE D3 21 39
20 F1 07 1B 00 00 00 00 16 00 00 00 50 EF 07 1B 8B D0 AE D3 21 39
20 F1 07 1B 5C F2 07 1B 20 72 F1 76 AC BF CA B0 FE FF FF FF F6 2C
08 40 64 6A 6A 00 00 00 74 DB F7 02 23 18 01 86 8E 6D 4A 56 9E 9C 64 41 43 E8 09
20 F1 07 1B 00 00 00 00 1B 00 00 00 50 EF 07 1B 8E 6D 4A 56 9E 9C 64 41 43 E8 09
20 F1 07 1B 5C F2 07 1B 20 72 F1 76 AC BF CA B0 FE FF FF FF F6 2C F0 76 B0 18 64
08 40 64 6A 6A 00 00 00 D5 A4 A3 5E A3 DC B1 3B 2D 3D 6C 4C A4
20 F1 07 1B 00 00 00 00 15 00 00 00 50 EF 07 1B 2D 3D 6C 4C A4
20 F1 07 1B 5C F2 07 1B 20 72 F1 76 AC BF CA B0 FE FF FF FF F6
08 40 64 6A 6A 00 00 00 AC 59 7A 0E 3D A1 50 43 57 20 58 41 5F 5C 74 88 22 1B 5D
20 F1 07 1B 00 00 00 00 1B 00 00 00 50 EF 07 1B 57 20 58 41 5F 5C 74 88 22 1B 5D
20 F1 07 1B 5C F2 07 1B 20 72 F1 76 AC BF CA B0 FE FF FF FF F6 2C F0 76 B0 18 64
08 40 64 6A 6A 00 00 00 73 33 13 51 56 77 BF 41 93 0A 61 9C 57 30 A3 DB B9 80
20 F1 07 1B 00 00 00 00 1A 00 00 00 50 EF 07 1B 93 0A 61 9C 57 30 A3 DB B9 80
20 F1 07 1B 5C F2 07 1B 20 72 F1 76 AC BF CA B0 FE FF FF FF F6 2C F0 76 B0 18
08 40 64 6A 6A 00 00 00 4B 2D C9 D8 69 E5 B9 BE 2F 77 13 0F 89 E6 A3 52 89 8E 96 CC
20 F1 07 1B 00 00 00 00 1C 00 00 00 50 EF 07 1B 2F 77 13 0F 89 E6 A3 52 89 8E 96 CC
20 F1 07 1B 5C F2 07 1B 20 72 F1 76 AC BF CA B0 FE FF FF FF F6 2C F0 76 B0 18 64 6A
Hi I have a hex String I need to convert that into jpg image please explain me how to do that i have done like this.This Hex String I need to convert into the jpg image,I am trying here but its not coming.
String hex="ff d8 ff e0 00 11 4a 46 49 46 00 01 01 01 00 00 00 00 00 00 0a ff db 00 43 00 08 06 06 07 06 05 08 07 07 07 09 09 08 0a 0c 14 0d 0c 0b 0b 0c 19 12 13 0f 14 1d 1a 1f 1e 1d 1a 1c 1c 20 24 2e 27 20 22 2c 23 1c 1c 28 37 29 2c 30 31 34 34 34 1f 27 39 3d 38 32 3c 2e 33 34 32 ff db 00 43 01 09 09 09 0c 0b 0c 18 0d 0d 18 32 21 1c 21 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 ff c4 00 1f 00 00 01 05 01 01 01 01 01 01 00 00 00 00 00 00 00 00 01 02 03 04 05 06 07 08 09 0a 0b ff c4 00 b5 10 00 02 01 03 03 02 04 03 05 05 04 04 00 00 01 7d 01 02 03 00 04 11 05 12 21 31 41 06 13 51 61 07 22 71 14 32 81 91 a1 08 23 42 b1 c1 15 52 d1 f0 24 33 62 72 82 09 0a 16 17 18 19 1a 25 26 27 28 29 2a 34 35 36 37 38 39 3a 43 44 45 46 47 48 49 4a 53 54 55 56 57 58 59 5a 63 64 65 66 67 68 69 6a 73 74 75 76 77 78 79 7a 83 84 85 86 87 88 89 8a 92 93 94 95 96 97 98 99 9a a2 a3 a4 a5 a6 a7 a8 a9 aa b2 b3 b4 b5 b6 b7 b8 b9 ba c2 c3 c4 c5 c6 c7 c8 c9 ca d2 d3 d4 d5 d6 d7 d8 d9 da e1 e2 e3 e4 e5 e6 e7 e8 e9 ea f1 f2 f3 f4 f5 f6 f7 f8 f9 fa ff c4 00 1f 01 00 03 01 01 01 01 01 01 01 01 01 00 00 00 00 00 00 01 02 03 04 05 06 07 08 09 0a 0b ff c4 00 b5 11 00 02 01 02 04 04 03 04 07 05 04 04 00 01 02 77 00 01 02 03 11 04 05 21 31 06 12 41 51 07 61 71 13 22 32 81 08 14 42 91 a1 b1 c1 09 23 33 52 f0 15 62 72 d1 0a 16 24 34 e1 25 f1 17 18 19 1a 26 27 28 29 2a35 36 37 38 39 3a 43 44 45 46 47 48 49 4a 53 54 55 56 57 58 59 5a 63 64 65";
// String sb = "0d 02 01 02 4e 52 30 39 47 30 35 36 31 38 00 03 00 fa 01 95 e4 53 c0 a8 51 b1 53 a3 03 40 89 54 66 9c ee 10 01 de 9a 5c 22 e6 a0 2c 5d b2 69 8c 9d 79 e6 a5 41 50 c6 78 ab 31 8c 8a 00 72 83 53 2a e0 67 14 d0 2a 54 1b 85 00 00 76 a6 b4 64 1e 95 32 a1 c8 a5 90 f3 81 da 98 15 88 c7 51 54 6e 63 cb 1e 3a d6 99 19 19 ef 55 6e 13 2b 91 49 89 98 2f a7 49 b8 95 20 8a 88 d9 cc b9 f9 0d 6d 85 e6 9e 23 e3 a5 06 7c a6 02 c6 e8 c3 2a 72 0d 74 90 1d d1 29 f5 14 cf 29 5b 82 01 fa 8a 9d 14 00 00 e0 50 38 ad 46 4c 38 15 89 7a b8 9c fa 11 5b f2 2e 56 a9 5c e9 ad 39 0e 18 03 8e f4 8a 68 c4 14 d2 39 ad 07 d2 ae 17 a0 0d f4 35 5d ed 27 4f bd 1b 0f c2 99 9d 88 06 41 ae 9f 4e 19 b2 88 fb 57 36 54 83 c8 23 eb c5 74 da 70 c5 94 7f 4a 07 1d cb 2c 3e 53 58 77 1c ca 45 6e 37 dd 35 89 38 fd e9 a1 97 2d 88 76 d1 8a 76 29 c8 bb 98 52 32 23 da 4f 6a 96 38 01 19 6e 95 21 c0 e0 0a 98 0d d1 0c 75 14 0a c4 b6 d1 a0 8f 81 8c d3 9d 76 1a 92 24 21 00 a8 e7 38 fc 29 9b c7 62 85 c9 dc f8 f4 aa f8 a7 bb 65 89 a6 1e b4 8c a5 ab 0a 43 41 a6 f7 a0 43 5e 95 54 e0 50 06 48 06 a4 c7 14 0e 28 85 86 2a 32 78 a9 5e a0 90 e0 52 35 7b 12 e2 81 4b 8e 69 71 4c 60 bd 6a 74 a8 50 73 53 31 c0 da 3a d0 00 cd b8 e0 74 14 a0 8c e3 bd 20 18 1c 75 a6 a9 cb 8d c3 9c d3 02 c4 7c 1a b8 83 8a aa 8a 59 b8 ab 89 c0 02 80 1e 38 a5 52 41 e2 90 0a 51 40 0f f3 1b 34 ed e1 ba f5 a8 a9 71 40 12 16 e3 02 a2 75 ca 9a 78 a5 c1 34 01 45 46 3a d4 8b 43 ae d9 0f bd 0b 40 89 00 14 f0 05 31 4d 48 a2 81 8e db 9a 70 5a 45 19 a9 00 a6 16 1a 12 83 1e 6a 40 29 c1 68 15 91 58 db 46 df 79 14 fe 15 24 71 88 d7 6a 8c 01 da a6 c5 21 5a 01 22 0d 0a 2a 4b 57 00 0f 00 01 82 03 00 00 0d 0a ";
// String raw_DeviceId = sb.substring(38, 1040);
byte[] b = HexStringToByteArray(hex);
// imageInFile.read(b);
for (int i = 0; i < b.length; i++) {
byte c = b[i];
System.out.println("c = " + c);
}
/*
* Converting Image byte array into Base64 String
*/
String imageDataString = encodeImage(b);
/*
* Converting a Base64 String into Image byte array
*/
byte[] imageByteArray = decodeImage(imageDataString);
/*
* Write a image byte array into file system
*/
FileOutputStream imageOutFile = new FileOutputStream("D:/img6.jpg");
imageOutFile.write(imageByteArray);
imageInFile.close();
imageOutFile.close();
System.out.println("Image Successfully Manipulated!");
} catch (FileNotFoundException e) {
System.out.println("Image not found" + e);
} catch (IOException ioe) {
System.out.println("Exception while reading the Image " + ioe);
}
}
/**
* Encodes the byte array into base64 string
*
* #param imageByteArray - byte array
* #return String a {#link java.lang.String}
*/
public static String encodeImage(byte[] imageByteArray) {
return Base64.encodeBase64URLSafeString(imageByteArray);
}
/**
* Decodes the base64 string into byte array
*
* #param imageDataString - a {#link java.lang.String}
* #return byte array
*/
public static byte[] decodeImage(String imageDataString) {
return Base64.decodeBase64(imageDataString);
}
}
And HexString to bytearray convertion:
public static byte[] HexStringToByteArray(String hexStr) {
int len = hexStr.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(hexStr.charAt(i), 16) << 4)
+ Character.digit(hexStr.charAt(i + 1), 16));
}
return data;
}
Update:
String hex = "ff d8 ff e0 00 11 4a 46 49 46 00 01 01 01 00 00 00 00 00 00 0a ff db 00 43 00 08 06 06 07 06 05 08 07 07 07 09 09 08 0a 0c 14 0d 0c 0b 0b 0c 19 12 13 0f 14 1d 1a 1f 1e 1d 1a 1c 1c 20 24 2e 27 20 22 2c 23 1c 1c 28 37 29 2c 30 31 34 34 34 1f 27 39 3d 38 32 3c 2e 33 34 32 ff db 00 43 01 09 09 09 0c 0b 0c 18 0d 0d 18 32 21 1c 21 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 ff c4 00 1f 00 00 01 05 01 01 01 01 01 01 00 00 00 00 00 00 00 00 01 02 03 04 05 06 07 08 09 0a 0b ff c4 00 b5 10 00 02 01 03 03 02 04 03 05 05 04 04 00 00 01 7d 01 02 03 00 04 11 05 12 21 31 41 06 13 51 61 07 22 71 14 32 81 91 a1 08 23 42 b1 c1 15 52 d1 f0 24 33 62 72 82 09 0a 16 17 18 19 1a 25 26 27 28 29 2a 34 35 36 37 38 39 3a 43 44 45 46 47 48 49 4a 53 54 55 56 57 58 59 5a 63 64 65 66 67 68 69 6a 73 74 75 76 77 78 79 7a 83 84 85 86 87 88 89 8a 92 93 94 95 96 97 98 99 9a a2 a3 a4 a5 a6 a7 a8 a9 aa b2 b3 b4 b5 b6 b7 b8 b9 ba c2 c3 c4 c5 c6 c7 c8 c9 ca d2 d3 d4 d5 d6 d7 d8 d9 da e1 e2 e3 e4 e5 e6 e7 e8 e9 ea f1 f2 f3 f4 f5 f6 f7 f8 f9 fa ff c4 00 1f 01 00 03 01 01 01 01 01 01 01 01 01 00 00 00 00 00 00 01 02 03 04 05 06 07 08 09 0a 0b ff c4 00 b5 11 00 02 01 02 04 04 03 04 07 05 04 04 00 01 02 77 00 01 02 03 11 04 05 21 31 06 12 41 51 07 61 71 13 22 32 81 08 14 42 91 a1 b1 c1 09 23 33 52 f0 15 62 72 d1 0a 16 24 34 e1 25 f1 17 18 19 1a 26 27 28 29 2a35 36 37 38 39 3a 43 44 45 46 47 48 49 4a 53 54 55 56 57 58 59 5a 63 64 65";
// String sb = "0d 02 01 02 4e 52 30 39 47 30 35 36 31 38 00 03 00 fa 01 95 e4 53 c0 a8 51 b1 53 a3 03 40 89 54 66 9c ee 10 01 de 9a 5c 22 e6 a0 2c 5d b2 69 8c 9d 79 e6 a5 41 50 c6 78 ab 31 8c 8a 00 72 83 53 2a e0 67 14 d0 2a 54 1b 85 00 00 76 a6 b4 64 1e 95 32 a1 c8 a5 90 f3 81 da 98 15 88 c7 51 54 6e 63 cb 1e 3a d6 99 19 19 ef 55 6e 13 2b 91 49 89 98 2f a7 49 b8 95 20 8a 88 d9 cc b9 f9 0d 6d 85 e6 9e 23 e3 a5 06 7c a6 02 c6 e8 c3 2a 72 0d 74 90 1d d1 29 f5 14 cf 29 5b 82 01 fa 8a 9d 14 00 00 e0 50 38 ad 46 4c 38 15 89 7a b8 9c fa 11 5b f2 2e 56 a9 5c e9 ad 39 0e 18 03 8e f4 8a 68 c4 14 d2 39 ad 07 d2 ae 17 a0 0d f4 35 5d ed 27 4f bd 1b 0f c2 99 9d 88 06 41 ae 9f 4e 19 b2 88 fb 57 36 54 83 c8 23 eb c5 74 da 70 c5 94 7f 4a 07 1d cb 2c 3e 53 58 77 1c ca 45 6e 37 dd 35 89 38 fd e9 a1 97 2d 88 76 d1 8a 76 29 c8 bb 98 52 32 23 da 4f 6a 96 38 01 19 6e 95 21 c0 e0 0a 98 0d d1 0c 75 14 0a c4 b6 d1 a0 8f 81 8c d3 9d 76 1a 92 24 21 00 a8 e7 38 fc 29 9b c7 62 85 c9 dc f8 f4 aa f8 a7 bb 65 89 a6 1e b4 8c a5 ab 0a 43 41 a6 f7 a0 43 5e 95 54 e0 50 06 48 06 a4 c7 14 0e 28 85 86 2a 32 78 a9 5e a0 90 e0 52 35 7b 12 e2 81 4b 8e 69 71 4c 60 bd 6a 74 a8 50 73 53 31 c0 da 3a d0 00 cd b8 e0 74 14 a0 8c e3 bd 20 18 1c 75 a6 a9 cb 8d c3 9c d3 02 c4 7c 1a b8 83 8a aa 8a 59 b8 ab 89 c0 02 80 1e 38 a5 52 41 e2 90 0a 51 40 0f f3 1b 34 ed e1 ba f5 a8 a9 71 40 12 16 e3 02 a2 75 ca 9a 78 a5 c1 34 01 45 46 3a d4 8b 43 ae d9 0f bd 0b 40 89 00 14 f0 05 31 4d 48 a2 81 8e db 9a 70 5a 45 19 a9 00 a6 16 1a 12 83 1e 6a 40 29 c1 68 15 91 58 db 46 df 79 14 fe 15 24 71 88 d7 6a 8c 01 da a6 c5 21 5a 01 22 0d 0a 2a 4b 57 00 0f 00 01 82 03 00 00 0d 0a ";
// String raw_DeviceId = sb.substring(38, 1040);
// byte[] b = HexStringToByteArray(hex);
//// imageInFile.read(b);
// for (int i = 0; i < b.length; i++) {
// byte c = b[i];
// System.out.println("c = " + c);
//
// }
Font font = new Font("Arial", Font.PLAIN, 12);
BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = img.createGraphics();
FontMetrics fm = g2d.getFontMetrics(font);
g2d.dispose();
int width = fm.stringWidth(hex);
int height = fm.getHeight();
img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
g2d = img.createGraphics();
g2d.setColor(Color.WHITE);
g2d.fillRect(0, 0, width, height);
g2d.setColor(Color.BLACK);
g2d.setFont(font);
g2d.drawString(hex, 0, fm.getAscent());
g2d.dispose();
try {
ImageIO.write(img, "jpg", new File("Hex.jpg"));
} catch (IOException ex) {
ex.printStackTrace();
}
You have a kind of chicken and egg issue. In order to generate the image, you will need to know how big to make the image. In order to calculate this information, you will need a Graphics context from an image in order to ascertain the FontMetrics for the given Graphics context...
The following example simple creates a 1x1 BufferedImage which is used to obtain a reference to the FontMetrics, which is used to calculate the required width/height of the String in question.
This is then used to create a new instance of the BufferedImage at the correct size and the String is rendered to it.
It is then saved to a jpg file...
A small snippet of the output image...
String hex="ff d8 ff e0 00 11 4a 46 49 46 00 01 01 01 00 00 00 00 00 00 0a ff db 00 43 00 08 06 06 07 06 05 08 07 07 07 09 09 08 0a 0c 14 0d 0c 0b 0b 0c 19 12 13 0f 14 1d 1a 1f 1e 1d 1a 1c 1c 20 24 2e 27 20 22 2c 23 1c 1c 28 37 29 2c 30 31 34 34 34 1f 27 39 3d 38 32 3c 2e 33 34 32 ff db 00 43 01 09 09 09 0c 0b 0c 18 0d 0d 18 32 21 1c 21 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 32 ff c4 00 1f 00 00 01 05 01 01 01 01 01 01 00 00 00 00 00 00 00 00 01 02 03 04 05 06 07 08 09 0a 0b ff c4 00 b5 10 00 02 01 03 03 02 04 03 05 05 04 04 00 00 01 7d 01 02 03 00 04 11 05 12 21 31 41 06 13 51 61 07 22 71 14 32 81 91 a1 08 23 42 b1 c1 15 52 d1 f0 24 33 62 72 82 09 0a 16 17 18 19 1a 25 26 27 28 29 2a 34 35 36 37 38 39 3a 43 44 45 46 47 48 49 4a 53 54 55 56 57 58 59 5a 63 64 65 66 67 68 69 6a 73 74 75 76 77 78 79 7a 83 84 85 86 87 88 89 8a 92 93 94 95 96 97 98 99 9a a2 a3 a4 a5 a6 a7 a8 a9 aa b2 b3 b4 b5 b6 b7 b8 b9 ba c2 c3 c4 c5 c6 c7 c8 c9 ca d2 d3 d4 d5 d6 d7 d8 d9 da e1 e2 e3 e4 e5 e6 e7 e8 e9 ea f1 f2 f3 f4 f5 f6 f7 f8 f9 fa ff c4 00 1f 01 00 03 01 01 01 01 01 01 01 01 01 00 00 00 00 00 00 01 02 03 04 05 06 07 08 09 0a 0b ff c4 00 b5 11 00 02 01 02 04 04 03 04 07 05 04 04 00 01 02 77 00 01 02 03 11 04 05 21 31 06 12 41 51 07 61 71 13 22 32 81 08 14 42 91 a1 b1 c1 09 23 33 52 f0 15 62 72 d1 0a 16 24 34 e1 25 f1 17 18 19 1a 26 27 28 29 2a35 36 37 38 39 3a 43 44 45 46 47 48 49 4a 53 54 55 56 57 58 59 5a 63 64 65";
Font font = new Font("Arial", Font.PLAIN, 12);
BufferedImage img = new BufferedImage(1, 1, BufferedImage.TYPE_INT_RGB);
Graphics2D g2d = img.createGraphics();
FontMetrics fm = g2d.getFontMetrics(font);
g2d.dispose();
int width = fm.stringWidth(hex);
int height = fm.getHeight();
img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
g2d = img.createGraphics();
g2d.setColor(Color.WHITE);
g2d.fillRect(0, 0, width, height);
g2d.setColor(Color.BLACK);
g2d.setFont(font);
g2d.drawString(hex, 0, fm.getAscent());
g2d.dispose();
try {
ImageIO.write(img, "jpg", new File("Hex.jpg"));
} catch (IOException ex) {
ex.printStackTrace();
}
I have a hex file have values like
f0 01 05 00 00 00 00 00 00 3d 61 c1 c1 df 40 02 00 c0 00 00 09 18 16 00 ef 01 40 00 00 3f 20 f0 01 03 00 00 00 00 00 00 c7 a2 c1 c1 df 40 02 00 c0 01 00 09 18 16 00 ef 01 40 00 00 3f 20 f0 01 02 00 00 00 00 00 00 50 e4 c1 c1 df 40 02 00 c0 02 00 09 79 09 04 c7 01 e1 4e 02 3f 00 f0 01 01 00 00 00 00 00 00 d9 25 c2 c1 df 40 02 00 c0 03 00 09 02 19 25 3c 00 22 6a 01 db 09 f0 04 05 00 00 00 00 00 00 62 67 c2 c1 df 40 02 00 c0 04 00 09 02 19 25 3c 00 22 6a 01 db 09 f0 01 07 00 00 00 00 00 00 ec a8 c2 c1 df 40 02 00 c0 05 00 13 7b 06 04 a4 0d 91 0c b6 c4 4e 02 3f 00 01 00 00 00 00 00 00 f0 01 08 00 00 00 00 00 00 6b 13 c3 c1 df 40 02 00 c0 06 00 13 7b 2b 04 93 0d 43 13 b6 c4 2e 00 3f 00 01 00 00 00 00 00 00 f0 01 09 00 00 00 00 00 00 e9 7d c3 c1 df 40 02 00 c0 07 00 13 7b 4f 04 d6 0d 2e fc b5 c4 2e 00 3f 00 01 00 00 00 00 00 00 f0 01 05 00 00 00 00 00 00 68 e8 c3 c1 df 40 02 00 c0 08 00 13 7b 55 04 ca 0d 7b 10 b6 c4 29 00 3f 00 01 00 00 00 00 00 00 f0 01 06 00 00 00 00 00 00 e7 52 c4 c1 df 40 02 00 c0 09 00 13 7b 46 04 f1 0d c5 15 b6 c4 2e 00 3f 00 01 00 00 00 00 00 00
I want whenever I find f0 followed by 01 followed by 01 to copy these pattern and whatever after it till the next f0 and so on till I have all the patterns that have these sequence
and save it in a file
and whenever I find f0 followed by 01 followed by 02 to copy these pattern and whatever after it till the next f0 and so on till I have all the patterns that have these sequence
and save it in another file
and so on with f0 01 03 till f0 01 09
Here is what I tried to do
String filename = "C:\\tm09888.123";
FileInputStream in = null;
int readHexFile = 0;
char hexToChar = ' ';
String[] bytes = new String[10];
try
{
in = new FileInputStream(filename);
while((readHexFile = in.read()) != -1)
{
if (Integer.toHexString(readHexFile).equals("f0"))
{
System.out.print("\n\n\n");
}
System.out.print(Integer.toHexString(readHexFile) + " ");
}
}
catch (IOException ex)
{
Logger.getLogger(NARSSTest.class.getName()).log(Level.SEVERE, null, ex);
}
}
I split all the pattern from f0 but how could I tell the program to look after f0 to be followed by 01 01
and then save all these patterns in another file