签到

05月11日
尚未签到

共有回帖数 0

    岁月流逝

    等级:
    都是用 Win32 汇编写的,也可以在
    http://hi.baidu.com/masterray/blog/item/953f8bfdf8bf831308244da7.html
    http://hi.baidu.com/masterray/blog/item/c2e22095e5a2510d7af480a0.html
    察看源代码

    Base64 编码器

    .386
    .model flat, stdcall
    option casemap:none

    include windows.inc
    include kernel32.inc
    includelib kernel32.lib

    .const
       sTitle        db 'MasterRay Base64Encoder', 10, 'Base64Encoder source destination', 10, 10,
                   '  source       Specify the text file.', 10,
                   '  destination  Specify the filename for the encoded file.', 10
       len_sTitle    equ $ - offset sTitle
       sError1        db 'Cannot open the source file specified.', 10
       len_sError1    equ $ - offset sError1
       sError2        db 'Cannot write in the destination file specified.', 10
       len_sError2    equ $ - offset sError2
       Base64_Encode_Table    db 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='

    .data?
       buff        dd ?
       fIn        dd ?
       fOut        dd ?
       hStdOut        dd ?
       pOutfileName    dd ?
       t        dd ?
       TextLen        dd ?

    .code

    Start:
       invoke GetStdHandle, STD_OUTPUT_HANDLE
       mov hStdOut, eax

       ;获取命令行参数
       invoke GetCommandLine
       xor ebx, ebx ;flag
       xor cl, cl ;argc
       .repeat
           .if bl == 0 ;是否在双引号内
               .if byte ptr [eax] == 32
                   xor bh, bh
                   mov byte ptr [eax], 0
               .elseif bh == 0
                   not bh
                   inc cl
                   .if byte ptr [eax] != '"'
                       .if cl == 2
                           mov t, eax ;源文件名
                       .elseif cl == 3
                           mov pOutfileName, eax ;目标文件名
                       .endif
                   .else
                       not bl
             lea edx, [eax+1]
                       .if cl == 2
                           mov t, edx ;源文件名
                       .elseif cl == 3
                           mov pOutfileName, edx ;目标文件名
                       .endif
                   .endif
               .endif
           .elseif byte ptr [eax] == '"'
               not bl
               mov byte ptr [eax], 0
           .endif
           inc eax
       .until byte ptr [eax] == 0
       .if cl != 3 ;argc != 3
           invoke WriteConsole, hStdOut, offset sTitle, len_sTitle, NULL, NULL
           invoke ExitProcess, 0
       .else
           ;打开源文件
           invoke CreateFile, t, GENERIC_READ, FILE_SHARE_READ or FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL
           .if eax == INVALID_HANDLE_VALUE
               invoke WriteConsole, hStdOut, offset sError1, len_sError1, NULL, NULL
               invoke ExitProcess, 0
           .endif
           mov [fIn], eax

           ;获取文件大小
           invoke GetFileSize, eax, NULL
           mov [TextLen], eax ;代码长度

           ;打开目标文件
           invoke CreateFile, pOutfileName, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL
           .if eax == INVALID_HANDLE_VALUE
               invoke WriteConsole, hStdOut, offset sError2, len_sError2, NULL, NULL
               invoke ExitProcess, 0
           .endif
           mov [fOut], eax
       .endif

       mov ebx, TextLen ;源文件长度
       .while ebx = 3
           invoke ReadFile, fIn, offset buff, 3, offset t, NULL ;读取源文件 3 个字符
           mov eax, buff
           xchg al, ah
           rol eax, 16
           xchg al, ah
           xor edx, edx ;存放编码后的 4 个字符

           mov ecx, 4 ;循环 4 次
    @@:
               push eax ;保存 eax
               rol eax, 6
               and eax, 63
               mov al, byte ptr [eax + offset Base64_Encode_Table]
               shl edx, 8
               add dl, al
               pop eax ;获得原 eax
               shl eax, 6
           loopd @B
        xchg dl, dh
           rol edx, 16
           xchg dl, dh
           mov buff, edx ;编码后的 4 个字符
           invoke WriteFile, fOut, offset buff, 4, offset t, NULL
           sub ebx, 3
       .endw
       .if ebx == 1
           invoke ReadFile, fIn, offset buff, 1, offset t, NULL ;读取源文件 1 个字符
           mov eax, buff
           xchg al, ah
           rol eax, 16
           xchg al, ah
           mov edx, 3D3Dh ;存放编码后的 4 个字符,第 3、4 个字符都是 =

           mov ecx, 2 ;循环 2 次
    @@:
               push eax ;保存 eax
               rol eax, 6
               and eax, 63
               mov al, byte ptr [eax + offset Base64_Encode_Table]
               shl edx, 8
               add dl, al
               pop eax ;获得原 eax
               shl eax, 6
           loopd @B

           xchg dl, dh
           mov buff, edx ;编码后的 4 个字符
           invoke WriteFile, fOut, offset buff, 4, offset t, NULL
       .elseif ebx == 2
           invoke ReadFile, fIn, offset buff, 2, offset t, NULL ;读取源文件 2 个字符
           mov eax, buff
           xchg al, ah
           rol eax, 16
           xchg al, ah
           mov edx, 3Dh ;存放编码后的 4 个字符,第 4 个字符是 =

           mov ecx, 3 ;循环 2 次
    @@:
               push eax ;保存 eax
               rol eax, 6
               and eax, 63
               mov al, byte ptr [eax + offset Base64_Encode_Table]
               ror edx, 8
               add dl, al
               pop eax ;获得原 eax
               shl eax, 6
           loopd @B

           rol edx, 16
           mov buff, edx ;编码后的 4 个字符
           invoke WriteFile, fOut, offset buff, 4, offset t, NULL
       .endif
       invoke CloseHandle, fIn
       invoke CloseHandle, fOut
       invoke ExitProcess, 0

    end Start
    Base64 解码器

    .386
    .model flat, stdcall
    option casemap:none

    include windows.inc
    include kernel32.inc
    includelib kernel32.lib

    .const
       sTitle        db 'MasterRay Base64Decoder', 10, 'Base64Decoder source destination', 10, 10,
                   '  source       Specify the encoded file.', 10,
                   '  destination  Specify the filename for the decoded file.', 10
       len_sTitle    equ $ - offset sTitle
       sError1        db 'Cannot open the source file specified.', 10
       len_sError1    equ $ - offset sError1
       sError2        db 'Cannot write in the destination file specified.', 10
       len_sError2    equ $ - offset sError2
       sError3        db 'Invalid code.', 10
       len_sError3    equ $ - offset sError3
       Base64_Decode_Table    db 43 dup(-1),62,3 dup(-1),63,52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1
                   db -1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14
                   db 15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1
                   db -1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40
                   db 41,42,43,44,45,46,47,48,49,50,51,133 dup(-1)

    .data?
       buff        dd ?
       fIn        dd ?
       fOut        dd ?
       hStdOut        dd ?
       pOutfileName    dd ?
       t        dd ?
       TextLen        dd ?

    .code

    Start:
       invoke GetStdHandle, STD_OUTPUT_HANDLE
       mov hStdOut, eax

       ;获取命令行参数
       invoke GetCommandLine
       xor ebx, ebx ;flag
       xor cl, cl ;argc
       .repeat
           .if bl == 0 ;是否在双引号内
               .if byte ptr [eax] == 32
                   xor bh, bh
                   mov byte ptr [eax], 0
               .elseif bh == 0
                   not bh
                   inc cl
                   .if byte ptr [eax] != '"'
                       .if cl == 2
                           mov t, eax ;源文件名
                       .elseif cl == 3
                           mov pOutfileName, eax ;目标文件名
                       .endif
                   .else
                       not bl
            lea edx, [eax+1]
                       .if cl == 2
                           mov t, edx ;源文件名
                       .elseif cl == 3
                           mov pOutfileName, edx ;目标文件名
                       .endif
                   .endif
               .endif
           .elseif byte ptr [eax] == '"'
               not bl
               mov byte ptr [eax], 0
           .endif
           inc eax
       .until byte ptr [eax] == 0
       .if cl != 3 ;argc != 3
           invoke WriteConsole, hStdOut, offset sTitle, len_sTitle, NULL, NULL
           invoke ExitProcess, 0
       .else
           ;打开源文件
           invoke CreateFile, t, GENERIC_READ, FILE_SHARE_READ or FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL
           .if eax == INVALID_HANDLE_VALUE
               invoke WriteConsole, hStdOut, offset sError1, len_sError1, NULL, NULL
               invoke ExitProcess, 0
           .endif
           mov [fIn], eax

           ;获取文件大小
           invoke GetFileSize, eax, NULL
           mov [TextLen], eax ;代码长度

           ;打开目标文件
           invoke CreateFile, pOutfileName, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL
           .if eax == INVALID_HANDLE_VALUE
               invoke WriteConsole, hStdOut, offset sError2, len_sError2, NULL, NULL
               invoke ExitProcess, 0
           .endif
           mov [fOut], eax
       .endif
     mov esi, TextLen ;源文件长度
       .while esi = 5
           invoke ReadFile, fIn, offset buff, 4, offset t, NULL
           mov eax, buff
           xor ebx, ebx
           mov ecx, 4
    @@:
               push eax
               movzx eax, al
               mov al, byte ptr [eax + offset Base64_Decode_Table]
               cmp al, -1
               jz @1 ;该字符非 Base64 编码字符,或该字符为 =
               shl ebx, 6
               or bl, al
               pop eax
               shr eax, 8
               dec edx
           loopd @B
           shl ebx, 8
           xchg bl, bh
           rol ebx, 16
           xchg bl, bh
           mov buff, ebx
           invoke WriteFile, fOut, offset buff, 3, offset t, NULL
           sub esi, 4
       .endw
       .if esi != 0
           invoke ReadFile, fIn, offset buff, esi, offset t, NULL
           mov eax, buff
           xor ebx, ebx
           mov edi, -1
           mov ecx, esi
    @@:
               push eax
               .if al == '='
                   xor esi, esi
               .else
                   test esi, esi
                   jz @1
                   inc edi
               .endif
               movzx eax, al
               mov al, byte ptr [eax + offset Base64_Decode_Table]
               shl ebx, 6
               or bl, al
               pop eax
               shr eax, 8
               dec edx
           loopd @B
           shl ebx, 8
           xchg bl, bh
           rol ebx, 16
           xchg bl, bh
           mov buff, ebx
           invoke WriteFile, fOut, offset buff, edi, offset t, NULL
       .endif
       jmp @2
    @1:
       invoke WriteConsole, hStdOut, offset sError3, len_sError3, NULL, NULL
    @2:
       invoke CloseHandle, fIn
       invoke CloseHandle, fOut
       invoke ExitProcess, 0

    end Start
    这两个工具能够识别命令行参数里的双引号,会把双引号内的字符串当作一个整体,所以可以在参数里输入包含空格的路径
    以前写的 BrainFuck 解释器和 TextToBF 没注意到这点……

    如果你还不知道 Base64 是什么的话,请看
    http://baike.baidu.com/view/469071.htm
    Base64 是传输字节代码的一种常见编码,你收发的邮件 *.eml 里的附件就可能用到了 Base64 编码
    程序和源代码我已经在
    http://groups.google.com/group/MasterRay
    上传了,可以去那里下��

    楼主 2016-01-15 17:02 回复

共有回帖数 0
  • 回 帖
  • 表情 图片 视频
  • 发表

登录直线网账号

Copyright © 2010~2015 直线网 版权所有,All Rights Reserved.沪ICP备10039589号 意见反馈 | 关于直线 | 版权声明 | 会员须知