algorithm - 汇编中的 Adob​​e Type 1 加密算法

标签 algorithm file assembly encryption tasm

我正在尝试使用 adobe type 1 字体加密算法来加密文本文件。但是,我不知道如何用汇编语言正确地实现算法。如果可以,请帮助我。

下面是adobe type 1字体加密算法:

 unsigned short int r;
 unsigned short int c1 =52845;
 unsigned short int c2 = 22719;


unsigned char eencrypt(char plain) unsigned char plain;
   {    unsigned char cipher; 

     cipher = (plain ^ (r >> 8));  
     r = (cipher + r) * c1 + c2;    
 return cipher; 
 } 

这是我的代码:

.model tiny
.data


filename db "file.txt", 0
bufferSize = 512
filehandle dw ?
buffer db bufferSize dup (0)

 r dw 0
c1 dw 52845
c2 dw 22719
cipher dw ?

message1 db 'Cannot open file. $' 
message2 db 'Cannot read file. $' 
message3 db 'Cannot close file. $' 



 .code
org 100h

 start:

call open
call read
call close
call Exit 

;procedures

 open:
  mov ah,3DH
  mov al,0
  mov dx, offset filename
  int 21h
  jc openErr
  mov filehandle, ax
  ret

   read:                        ;reads file
  mov ah, 3Fh
  mov bx, filehandle
  mov cx, bufferSize
  mov dx, offset buffer
  int 21h
  cmp ax,0
  jc readErr


;displays content of file
 call clear
 mov ah, 9
 mov dx, offset buffer
 int 21h
 ret 

close:
 mov ah, 3Eh
 mov bx, filehandle
int 21h
jc closeErr
ret

 encrypt:
; need loop to loop through each char, don't know how to do that 
        mov ax, [r]
        shr ax, 8          
        mov bl, [buffer]
        xor bh,bh    
        xor bx, ax       
        mov cipher, bx      
        mov dx, cipher     
        add dx, [r]       ;get error: extra characters on line
        imul dx, c1 
        add dx, c2 
        mov [r], dx     



 ;decrypt:

 clear: ;clears the screen

      mov ax,003h
      int 10h
      ret 
  Exit: 
  mov ax, 4C00h
  int 21h


newline:  ;prints a newline                           
        mov ah, 2                    
        mov dl, 0DH
        int 21h            
        mov dl, 0AH                 
        int 21h         
        ret

   ;error messages 

  openErr :
     call newline
    lea  DX,message1    ;set up pointer to error message
    mov  AH,9          ;display string function
    int  21H           ;DOS call
    stc               ;set error flag
    ret

 readErr :
    call newline
     lea  DX,message2    ;set up pointer to error message
     mov  AH,9          ;display string function
    int  21H           ;DOS call
    stc               ;set error flag
    ret

closeErr :
  call newline
  lea  DX,message3    ;set up pointer to error message
  mov  AH,9          ;display string function
  int  21H           ;DOS call
  stc               ;set error flag
  ret

结束开始

最佳答案

 ;encrypt:
; need loop to loop through each char, don't know how to do that 
;mov ax, r
;shr ax, 8           r>>8
;mov bl, buffer      bl = buffer

buffer是符号地址,不是缓冲区中的值。使用 mov bl,[buffer]从内存中读取值。如果你想遍历完整的缓冲区内容,请将该地址放入某个寄存器,si通常因为保留指向数据源的指针而受到青睐(因为它在 lods 指令中被硬编码,并且“s”可能被读作“源”)。如果你是 C++ 编译器,你不关心,你选择任何你有的备用寄存器,而不考虑它的名字。

;xor bx, ax          bx = buffer ^(r>>8)

bh没有设置,可能是任何东西,破坏你的计算。要么做xor bx,bx在加载 bl 之前来自缓冲区,或 xor bh,bh清除 bh仅(在现代 x86 上速度稍慢,因为它必须将 bh + bl 组合成 bx ),或使用 movzx bx,byte ptr [buffer]在上一步中将 char 值零扩展为 word 值。

;mov cipher, bx      cipher = buffer^(r>>8)

标签 cipherdb ? 之前定义, 但在这里你要将两个字节写入内存,所以它也会覆盖 message1 的第一个字节。字符串。我强烈建议使用 []每次你写入内存时,即使 TASM/MASM 允许这种语法,但我发现它很难阅读(看到 [cipher] 会让我的大脑自动想到“内存访问”,即使快速浏览源代码)。

;mov cx, c1

c1c2可以是EQU , 因此它们将作为中间值直接编译成代码。

;add cx, c2

这不是 C 加密的作用(乘法优先于加法)。

;mov dx, cipher

您再次对待 cipher作为单词,这是有道理的,但它反对 db ? (因此 db 需要修复,而不是此处的代码)。

;add dx, r
;imul dx, cx     

我会使用 [] r 左右的括号再次,乘法/加法顺序当然是错误的。

不错的尝试,但不要犹豫取消注释那段代码,用一些调试值预先设置寄存器,然后直接跳到它,在调试器中打开二进制文件,单步执行以验证它按你的意愿工作。您可能会很快找到我在上面写的所有这些内容。

然后只需将其变成循环(使用任何 ASM 书籍/教程以及一些有关使用数组/字符串的示例,学习使用指针,然后应该不难完成)。

关于algorithm - 汇编中的 Adob​​e Type 1 加密算法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44163068/

相关文章:

algorithm - 以编程方式在 KML 中创建矢量箭头

file - 如何使用unix从文件中提取特定字节

python - 使用python在postgresql数据库中插入json数据的问题

组装-更换外壳

assembly - 打印存储在字节中的值的宏。 assembly 体

c# - 更高效的数据表排序

mysql - 计算数据集之间相似度百分比的有效方法

algorithm - 使用 AND 运算符检查是否设置了某个位

python - 无法使用 MySQL 数据写入文件

c - 在Assembly中打印多个值