Posted on 2010-03-30 22:20
David Fang 阅读(14340)
评论(0) 编辑 收藏 引用 所属分类:
磨刀不误砍柴工
最近玩了一下Lua,写了一个小程序,用于实践十六进制字符串和二进制数据之间的转换。主要是因为最近有这方面的需要(在搞一些
RSA加密的东西)。
从十六进制字符串转为二进制数:
1 #! /usr/local/bin/lua
2
3
4 --Note: Input hex string file's content must be
5 --a upper case hex string
6
7 --Check the arguments
8 if #arg ~= 2 then
9 print("Usage:hexstr2bytes.lua <Input Hex String File> <Output Bytes File>\n");
10 os.exit();
11 end
12
13 --Open input hex string file
14 local hexstrfile = io.open(arg[1], "rb");
15 if nil == hexstrfile then
16 print("Can not open input file!\n");
17 os.exit();
18 end
19
20 --Read from the hex string file
21 local hexstr = hexstrfile:read("*a");
22 if nil == hexstr then
23 print("Can not read hex string from input file!\n");
24 elseif 1 == string.len(hexstr)%2 then
25 print("Hex string size must be evel!\n");
26 os.exit();
27 else
28 print("Hex String:", hexstr, "\n");
29 end
30
31 --Close hex string file
32 hexstrfile:close();
33
34 --Open output binary file
35 local bytesfile = io.open(arg[2], "wb");
36 if nil == bytesfile then
37 print("Can not open output file to write!\n");
38 os.exit();
39 end
40
41 --Convert hex string to bytes
42 for i = 1, string.len(hexstr) - 1, 2 do
43 local doublebytestr = string.sub(hexstr, i, i+1);
44 local n = tonumber(doublebytestr, 16);
45 if 0 == n then
46 bytesfile:write('\00');
47 else
48 bytesfile:write(string.format("%c", n));
49 end
50 end
51
52 --Close output binary file
53 bytesfile:close();
54
55
56
从二进制数转十六进制字符串:
1 #! /usr/local/bin/lua
2
3
4 --Check the arguments
5 if #arg ~= 2 then
6 print("Usage:bytes2hexstr.lua <Input Bytes File> <Output Hex String File>\n");
7 os.exit();
8 end
9
10 --Open input bytes file
11 local bytesfile = io.open(arg[1], "rb");
12 if nil == bytesfile then
13 print("Can not open input file!\n");
14 os.exit();
15 end
16
17 --Read from the bytes file
18 local bytes = bytesfile:read("*a");
19 if nil == bytes then
20 print("Can not read bytes from input file!\n");
21 os.exit();
22 end
23
24 --Close bytes file
25 bytesfile:close();
26
27 --Open output hex string file
28 local hexstrfile = io.open(arg[2], "wb");
29 if nil == hexstrfile then
30 print("Can not open output file to write!\n");
31 os.exit();
32 end
33
34 --Convert bytes to hex string
35 for i = 1, string.len(bytes) do
36 local charcode = tonumber(string.byte(bytes, i, i));
37 local hexstr = string.format("%02X", charcode);
38 hexstrfile:write(hexstr);
39 end
40
41 --Close output hex string file
42 hexstrfile:close();
使用Crypto++生成的私钥文件(使用HexEncoder编码的DER格式文
件)ch:
30820275020100300D06092A864886F70D01010105000482025F3082025B020100028
181009EEA4CF16CCD0DEF5FAA7B27D872AA0B5CCC286464E5338AE958E076BE764CA9
7C52E89D52329A4BB10DD586031CDD71D0C0581A8A3A41378AED44AA4F25B09C8BF2C
573C882BD82A8EE670C49229C609FB740CC7230FD25411851B3A37AEF91AA6EF3D2EF
2CEC5E03A04257D2C3FDDAAD092696A96B7D0358B1AE339625F9B50201110281802EB
D61ECA7879AAFD0D7C9DE8AF48C5DB1E1B186F07FA5BF71CED89B6531DA4FF763ADD3
EAFFD307341320AEF1DB503088B10ADAA120314C9245C8E6CBFC06C42AE3AFD1612DB
A9FC6619E5E08E70BA0CB211D73FD94EA35D3D7BDFCA8AFDC98F5E1277FB1C12ACF09
75D3AA74227B6D23A692DF5B9E6720C5B81578CBBA3D2D024100D1FD16E7CB7898978
5B87DF9E80D071AF95AC47B70FD053A8D03784FF46D6484EEB09B200C3800FBE46C02
A20127BED5CC880F0BAD8FDC9408FAE7A3C634C2C3024100C1BC58C4190843FF1A506
8390F9DA0EFC0857E936FD342007D6A86A23E51369E448D9F33ECFDC03BCBD6D67246
C09B5E9A7DF0F9F78DDB664911B0C1E8119A2702403DC2E89E8723782C90BDCAB2E9E
5B6CBB2C057E8122C4CD4FC4C508FFC98A51809F7B5278B1F87D19D892DF369C0654D
F0DCB9218D668C2B8A2BAD8A85970C1B024100B656EA21F97130F018C425DB59FDC4A
569E70DB7F0C6D4B52ABE9CD4EF5B7EB313581D5E0C3A1E56657EE7F31569FBA4552B
3D277F94924226A73CF2BC4CCD51024100BB5DA6433C896D07C88821107EF658DD201
B7049191213A2001D3EC0A68279CCC6A36EC5DB384F2797CA47F94505A069B247758D
8E6D1A8D30B068BCD26FC7F6
使用hexstr2bytes.lua:
hexstr2bytes.lua ch chout
生成chout:
00000000h: 30 82 02 75 02 01 00 30 0D 06 09 2A 86 48 86 F7 ; 0?u0*咹嗺
00000010h: 0D 01 01 01 05 00 04 82 02 5F 30 82 02 5B 02 01 ; .?_0?[..
00000020h: 00 02 81 81 00 9E EA 4C F1 6C CD 0D EF 5F AA 7B ; ..亖.炾L駆?颻獅
00000030h: 27 D8 72 AA 0B 5C CC 28 64 64 E5 33 8A E9 58 E0 ; '豶?\?dd?婇X?
00000040h: 76 BE 76 4C A9 7C 52 E8 9D 52 32 9A 4B B1 0D D5 ; v緑L﹟R铦R2欿??
00000050h: 86 03 1C DD 71 D0 C0 58 1A 8A 3A 41 37 8A ED 44 ; ?.輖欣X.?A7婍D
00000060h: AA 4F 25 B0 9C 8B F2 C5 73 C8 82 BD 82 A8 EE 67 ; 狾%皽嬺舠葌絺g
00000070h: 0C 49 22 9C 60 9F B7 40 CC 72 30 FD 25 41 18 51 ; .I"渀煼@蘲0?A.Q
00000080h: B3 A3 7A EF 91 AA 6E F3 D2 EF 2C EC 5E 03 A0 42 ; 常z飸猲笠?靆.燘
00000090h: 57 D2 C3 FD DA AD 09 26 96 A9 6B 7D 03 58 B1 AE ; W颐?&柀k}.X碑
000000a0h: 33 96 25 F9 B5 02 01 11 02 81 80 2E BD 61 EC A7 ; 3?.亐.絘歆
000000b0h: 87 9A AF D0 D7 C9 DE 8A F4 8C 5D B1 E1 B1 86 F0 ; 嚉咨迠魧]贬眴?
000000c0h: 7F A5 BF 71 CE D8 9B 65 31 DA 4F F7 63 AD D3 EA ; タq呜沞1贠鱟?
000000d0h: FF D3 07 34 13 20 AE F1 DB 50 30 88 B1 0A DA A1 ; ?4. 跴0埍.凇
000000e0h: 20 31 4C 92 45 C8 E6 CB FC 06 C4 2A E3 AF D1 61 ; 1L扙孺它.?惘補
000000f0h: 2D BA 9F C6 61 9E 5E 08 E7 0B A0 CB 21 1D 73 FD ; -簾芶瀆.?犓!.s?
00000100h: 94 EA 35 D3 D7 BD FC A8 AF DC 98 F5 E1 27 7F B1 ; 旉5幼近ǒ軜踽'?
00000110h: C1 2A CF 09 75 D3 AA 74 22 7B 6D 23 A6 92 DF 5B ; ??u营t"{m#遊
00000120h: 9E 67 20 C5 B8 15 78 CB BA 3D 2D 02 41 00 D1 FD ; 瀏 鸥.x撕=-.A.妖
00000130h: 16 E7 CB 78 98 97 85 B8 7D F9 E8 0D 07 1A F9 5A ; .缢x槜吀}鵝
00000140h: C4 7B 70 FD 05 3A 8D 03 78 4F F4 6D 64 84 EE B0 ; 膡p?:?xO鬽d勵?
00000150h: 9B 20 0C 38 00 FB E4 6C 02 A2 01 27 BE D5 CC 88 ; ?.8.l.?'菊虉
00000160h: 0F 0B AD 8F DC 94 08 FA E7 A3 C6 34 C2 C3 02 41 ; ..瓘軘.F4旅.A
00000170h: 00 C1 BC 58 C4 19 08 43 FF 1A 50 68 39 0F 9D A0 ; .良X?.C.Ph9.潬
00000180h: EF C0 85 7E 93 6F D3 42 00 7D 6A 86 A2 3E 51 36 ; 锢厏搊覤.}j啟>Q6
00000190h: 9E 44 8D 9F 33 EC FD C0 3B CB D6 D6 72 46 C0 9B ; 濪崯3忑?酥謗F罌
000001a0h: 5E 9A 7D F0 F9 F7 8D DB 66 49 11 B0 C1 E8 11 9A ; ^殅瘗鲘踗I.傲??
000001b0h: 27 02 40 3D C2 E8 9E 87 23 78 2C 90 BD CA B2 E9 ; '.@=妈瀲#x,惤什?
000001c0h: E5 B6 CB B2 C0 57 E8 12 2C 4C D4 FC 4C 50 8F FC ; 宥瞬繵?,L渣LP忺
000001d0h: 98 A5 18 09 F7 B5 27 8B 1F 87 D1 9D 89 2D F3 69 ; 槬..鞯'?囇潐-骾
000001e0h: C0 65 4D F0 DC B9 21 8D 66 8C 2B 8A 2B AD 8A 85 ; 纄M疖?峟??瓓?
000001f0h: 97 0C 1B 02 41 00 B6 56 EA 21 F9 71 30 F0 18 C4 ; ?..A.禫?鵴0??
00000200h: 25 DB 59 FD C4 A5 69 E7 0D B7 F0 C6 D4 B5 2A BE ; %踄?佛圃??
00000210h: 9C D4 EF 5B 7E B3 13 58 1D 5E 0C 3A 1E 56 65 7E ; 溤颷~?X.^.:.Ve~
00000220h: E7 F3 15 69 FB A4 55 2B 3D 27 7F 94 92 42 26 A7 ; 珞.iU+='敀B&?
00000230h: 3C F2 BC 4C CD 51 02 41 00 BB 5D A6 43 3C 89 6D ; <蚣L蚎.A.籡<塵
00000240h: 07 C8 88 21 10 7E F6 58 DD 20 1B 70 49 19 12 13 ; .葓!.~鯴?.pI
00000250h: A2 00 1D 3E C0 A6 82 79 CC C6 A3 6E C5 DB 38 4F ; ?.>捆倅唐袍8O
00000260h: 27 97 CA 47 F9 45 05 A0 69 B2 47 75 8D 8E 6D 1A ; '検G鵈.爄睪u崕m.
00000270h: 8D 30 B0 68 BC D2 6F C7 F6 ; ?癶家o泅
使用bytes2hexstr.lua:
bytes2hexstr.lua chout chtmp
生成chtmp:
30820275020100300D06092A864886F70D01010105000482025F3082025B020100028
181009EEA4CF16CCD0DEF5FAA7B27D872AA0B5CCC286464E5338AE958E076BE764CA9
7C52E89D52329A4BB10DD586031CDD71D0C0581A8A3A41378AED44AA4F25B09C8BF2C
573C882BD82A8EE670C49229C609FB740CC7230FD25411851B3A37AEF91AA6EF3D2EF
2CEC5E03A04257D2C3FDDAAD092696A96B7D0358B1AE339625F9B50201110281802EB
D61ECA7879AAFD0D7C9DE8AF48C5DB1E1B186F07FA5BF71CED89B6531DA4FF763ADD3
EAFFD307341320AEF1DB503088B10ADAA120314C9245C8E6CBFC06C42AE3AFD1612DB
A9FC6619E5E08E70BA0CB211D73FD94EA35D3D7BDFCA8AFDC98F5E1277FB1C12ACF09
75D3AA74227B6D23A692DF5B9E6720C5B81578CBBA3D2D024100D1FD16E7CB7898978
5B87DF9E80D071AF95AC47B70FD053A8D03784FF46D6484EEB09B200C3800FBE46C02
A20127BED5CC880F0BAD8FDC9408FAE7A3C634C2C3024100C1BC58C4190843FF1A506
8390F9DA0EFC0857E936FD342007D6A86A23E51369E448D9F33ECFDC03BCBD6D67246
C09B5E9A7DF0F9F78DDB664911B0C1E8119A2702403DC2E89E8723782C90BDCAB2E9E
5B6CBB2C057E8122C4CD4FC4C508FFC98A51809F7B5278B1F87D19D892DF369C0654D
F0DCB9218D668C2B8A2BAD8A85970C1B024100B656EA21F97130F018C425DB59FDC4A
569E70DB7F0C6D4B52ABE9CD4EF5B7EB313581D5E0C3A1E56657EE7F31569FBA4552B
3D277F94924226A73CF2BC4CCD51024100BB5DA6433C896D07C88821107EF658DD201
B7049191213A2001D3EC0A68279CCC6A36EC5DB384F2797CA47F94505A069B247758D
8E6D1A8D30B068BCD26FC7F6
文件下载:
HexBytesLua.rar