节约ROM的DES汇编算法

发布于 2020-02-03  67 次阅读


;=========================================================================
;功能:DES
;      ==>Ln=Rn-1;Rn=Ln-1 xor f(Rn-1,Kn)
;名称:Des_Arithmetic()
;入口: #MINGWEN,8字节明文;#MIYUE,8字节密钥;
;出口: #DES_MIWEN,8字节密文;
;占用:FLAG_TEMP,30H~3FH,40H~43H,48H~4FH,ACC,R0,R1,R2,R5,R6,R7
;其它:a、字节的bit7~0,对应位置的1~8
;	   b、因为用到直接寻址,变量需定义在00H~80H
;	   c、51汇编,foc=12M,program by\Ling
;	   d、代码长度: 约1.5K字节  运行时间: 约11.2ms  深度:0级(无调用)
;=========================================================================
FLAG_TEMP	DATA	20H		;DES算法要用到的位单元
BIT_00H		BIT	00H		;对应第8位
BIT_01H		BIT	01H		;对应第7位
BIT_02H		BIT	02H		;对应第6位
BIT_03H		BIT	03H		;对应第5位
BIT_04H		BIT	04H		;对应第4位
BIT_05H		BIT	05H		;对应第3位
BIT_06H		BIT	06H		;对应第2位
BIT_07H		BIT	07H		;对应第1位
Des_Flag	bit	08h
Des_Flag_1	bit	09h


DES_TEMP0	EQU	30H		;计算(L0,R0),(C0,D0)时暂用单元,8BYTES
DES_TEMP1	DATA	31H
DES_TEMP2	DATA	32H
DES_TEMP3	DATA	33H
DES_TEMP4	DATA	34H
DES_TEMP5	DATA	35H
DES_TEMP6	DATA	36H
DES_TEMP7	DATA	37H

DES_MIWEN	EQU	30H		;密文首地址
DES_Ln0		EQU	30H		;存储Ln(0~3)
DES_Ln1		DATA	31H
DES_Ln2		DATA	32H
DES_Ln3		DATA	33H
DES_Rn0		DATA	34H		;存储Dn(0~3)
DES_Rn1		DATA	35H
DES_Rn2		DATA	36H
DES_Rn3		DATA	37H

MINGWEN		EQU	38H		;明文首地址,8bytes
DES_L0		DATA	38H		;存储Ln-1(0~3)	
DES_L1		DATA	39H
DES_L2		DATA	3AH
DES_L3		DATA	3BH
DES_R0		DATA	3CH		;存储Rn-1(0~3)
DES_R1		DATA	3DH
DES_R2		DATA	3EH
DES_R3		DATA	3FH

DES_Ki		EQU	40H		;Ki首地址,4BYTE
DES_Ki0		DATA	40H
DES_Ki1		DATA	41H
DES_Ki2		DATA	42H
DES_Ki3		DATA	43H

MIYUE		EQU	48H		;*64位密钥,8BYTE
DES_C0		DATA	48H		;存储Cn(0~3)
DES_C1		DATA	49H
DES_C2		DATA	4AH
DES_C3		DATA	4BH		
DES_D0		DATA	4CH		;存储Dn(0~3)
DES_D1		DATA	4DH
DES_D2		DATA	4EH
DES_D3		DATA	4FH
;-------------------------------------------------------------------------
BCD_BYTE3	DATA	40H		;for HEX-->BCD
BCD_BYTE2	DATA	41H
BCD_BYTE1	DATA	42H
BCD_BYTE0	DATA	43H
;=========================================================================
		ORG	0000H
		AJMP	MAIN
		ORG	0030H
;=========================================================================
;构造Cn,Dn的循环移位次数表
;-------------------------------------------------------------------------
HDESR0L:
		DB	1,1,2,2,2,2,2,2,1,2,2,2,2,2,2,1,1
;-------------------------------------------------------------------------
;8个选择函数Si
;-------------------------------------------------------------------------
HDESSI0:
		DB	14,4,13,1,2,15,11,8,3,10,6,12,5,9,0,7	
		DB	0,15,7,4,14,2,13,1,10,6,12,11,9,5,3,8
		DB	4,1,14,8,13,6,2,11,15,12,9,7,3,10,5,0
		DB	15,12,8,2,4,9,1,7,5,11,3,14,10,0,6,13
HDESSI1:
		DB	15,1,8,14,6,11,3,4,9,7,2,13,12,0,5,10
		DB	3,13,4,7,15,2,8,14,12,0,1,10,6,9,11,5
		DB	0,14,7,11,10,4,13,1,5,8,12,6,9,3,2,15
		DB	13,8,10,1,3,15,4,2,11,6,7,12,0,5,14,9
HDESSI2:
		DB	10,0,9,14,6,3,15,5,1,13,12,7,11,4,2,8
		DB	13,7,0,9,3,4,6,10,2,8,5,14,12,11,15,1
		DB	13,6,4,9,8,15,3,0,11,1,2,12,5,10,14,7
		DB	1,10,13,0,6,9,8,7,4,15,14,3,11,5,2,12
HDESSI3:
		DB	7,13,14,3,0,6,9,10,1,2,8,5,11,12,4,15
		DB	13,8,11,5,6,15,0,3,4,7,2,12,1,10,14,9
		DB	10,6,9,0,12,11,7,13,15,1,3,14,5,2,8,4
		DB	3,15,0,6,10,1,13,8,9,4,5,11,12,7,2,14
HDESSI4:
		DB	2,12,4,1,7,10,11,6,8,5,3,15,13,0,14,9
		DB	14,11,2,12,4,7,13,1,5,0,15,10,3,9,8,6
		DB	4,2,1,11,10,13,7,8,15,9,12,5,6,3,0,14
		DB	11,8,12,7,1,14,2,13,6,15,0,9,10,4,5,3
HDESSI5:
		DB	12,1,10,15,9,2,6,8,0,13,3,4,14,7,5,11
		DB	10,15,4,2,7,12,9,5,6,1,13,14,0,11,3,8
		DB	9,14,15,5,2,8,12,3,7,0,4,10,1,13,11,6
		DB	4,3,2,12,9,5,15,10,11,14,1,7,6,0,8,13
HDESSI6:
		DB	4,11,2,14,15,0,8,13,3,12,9,7,5,10,6,1
		DB	13,0,11,7,4,9,1,10,14,3,5,12,2,15,8,6
		DB	1,4,11,13,12,3,7,14,10,15,6,8,0,5,9,2
		DB	6,11,13,8,1,4,10,7,9,5,0,15,14,2,3,12
HDESSI7:
		DB	13,2,8,4,6,15,11,1,10,9,3,14,5,0,12,7
		DB	1,15,13,8,10,3,7,4,12,5,6,11,0,14,9,2
		DB	7,11,4,1,9,12,14,2,0,6,10,13,15,3,5,8
		DB	2,1,14,7,4,10,8,13,15,12,9,0,3,5,6,11	
;-------------------------------------------------------------------------	
;说明:DES算法开始	
;-------------------------------------------------------------------------
Des_Arithmetic:	
;-------------------------------------------------------------------------
;功能:得到L0,R0
;出口:DES_L0~3,DES_R0~3 ;786us
;-------------------------------------------------------------------------
DES_GET_LR0:
		MOV	R1,#DES_TEMP0	;取得暂存首地址
		MOV	R7,#08H			;8个字节的8次循环
		
DES_GET_LRA:
		MOV	R2,#00H			;位暂存清0	
		MOV	R6,#08H			;1个字节的8次循环
		MOV	R0,#MINGWEN		;取得明文首地址
		
DES_GET_LRB:
		MOV	A,@R0		
		MOV	C,ACC.7			;取得1个字节的1位,
	RL	A			;该数据移位,准备取下一位
	MOV	@R0,A
		MOV	A,R2
		RRC	A			;acc.0对应位置8,故向右移
		MOV	R2,A
;		MOV	A,@R0
;		RL	A			;该数据移位,准备取下一位
;		MOV	@R0,A
		INC	R0
		DJNZ	R6,DES_GET_LRB	;1个字节ok?
		MOV	A,R2
		MOV	@R1,A			;ok则存储之
		INC	R1
		DJNZ	R7,DES_GET_LRA	;8个字节都初始置换完毕?
		MOV	DES_L0,DES_TEMP1	;是,则得到相应的L0,R0
		MOV	DES_L1,DES_TEMP3
		MOV	DES_L2,DES_TEMP5
		MOV	DES_L3,DES_TEMP7
		MOV	DES_R0,DES_TEMP0
		MOV	DES_R1,DES_TEMP2
		MOV	DES_R2,DES_TEMP4
		MOV	DES_R3,DES_TEMP6
;-------------------------------------------------------------------------
;功能:得到C0,D0
;出口:DES_C0~3,DES_D0~3 ;691us
;-------------------------------------------------------------------------						
DES_GET_CD0:
		MOV	R1,#DES_TEMP0		;取得暂存首地址
		MOV	R7,#07H			;7个字节的7次循环
		
DES_GET_CDA:
		MOV	R2,#00H			;位暂存清0		
		MOV	R6,#08H			;1个字节的8次循环
		MOV	R0,#MIYUE		;取得密钥首地址
		
DES_GET_CDB:
		MOV	A,@R0		
		MOV	C,ACC.7			;取得1个字节的1位
	RL	A			;该数据移位,准备取下一位
	MOV	@R0,A
		MOV	A,R2
		RRC	A
		MOV	R2,A
;		MOV	A,@R0
;		RL	A			;该数据移位,准备取下一位
;		MOV	@R0,A
		INC	R0
		DJNZ	R6,DES_GET_CDB		;1个字节ok?
		MOV	A,R2
		MOV	@R1,A			;ok则存储之
		INC	R1
		DJNZ	R7,DES_GET_CDA		;7个字节都初始置换完毕?
		
		MOV	DES_C0,DES_TEMP0	;得C0~3
		MOV	DES_C1,DES_TEMP1
		MOV	DES_C2,DES_TEMP2
		MOV	A,DES_TEMP3
		MOV	DES_C3,A		;C3的高4位
		
		MOV	DES_D0,DES_TEMP6	;得D0~3
		MOV	DES_D1,DES_TEMP5
		MOV	DES_D2,DES_TEMP4
		MOV	A,DES_TEMP3		
		SWAP	A
		MOV	DES_D3,A		;D3的高4位
;-------------------------------------------------------------------------	
;循环16次,以得L16,R16
;-------------------------------------------------------------------------
		MOV	R5,#16
DES_LOOP16: 
		NOP
;-------------------------------------------------------------------------
;功能:得到Ln=Rn-1
;出口:DES_Ln0~3
;-------------------------------------------------------------------------
		MOV	DES_Ln0,DES_R0
		MOV	DES_Ln1,DES_R1	
		MOV	DES_Ln2,DES_R2	
		MOV	DES_Ln3,DES_R3
;-------------------------------------------------------------------------
;功能:得到Cn-1,Dn-1,查表移位
;-------------------------------------------------------------------------
		MOV	DPTR,#HDESR0L		;取得表首地址
		MOV	A,R5
		MOVC	A,@A+DPTR
		MOV	R7,A			;取得移位次数于R7
	
DES_CiDi_A:
		MOV	A,DES_C3		;C0~3循环移位
		RLC	A
		MOV	DES_C3,A
		MOV	A,DES_C2
		RLC	A
		MOV	DES_C2,A
		MOV	A,DES_C1
		RLC	A
		MOV	DES_C1,A
		MOV	A,DES_C0
		RLC	A
		MOV	DES_C0,A
		MOV	A,DES_C3
		MOV	ACC.4,C
		MOV	DES_C3,A
		
		MOV	A,DES_D3		;D0~3循环移位
		RLC	A
		MOV	DES_D3,A
		MOV	A,DES_D2
		RLC	A
		MOV	DES_D2,A
		MOV	A,DES_D1
		RLC	A
		MOV	DES_D1,A
		MOV	A,DES_D0
		RLC	A
		MOV	DES_D0,A
		MOV	A,DES_D3
		MOV	ACC.4,C
		MOV	DES_D3,A		
		DJNZ	R7,DES_CiDi_A
;-------------------------------------------------------------------------
;***********************(Rn-1,Kn),Ki,32->48扩展,Si***********************
;-------------------------------------------------------------------------
;说明:a、以6位为单位,取得Ki及32到48位的扩展,这6位以"b5b0,b4b3b2b1"存放;
;	   b、异或实现模2加,然后查si盒子,得新的数据,并把相邻两字节整合为1字节
;出口:DES_Ki0~3
;-------------------------------------------------------------------------
;功能:得6bit,第1字节高4位
;-------------------------------------------------------------------------
		;14 17 11 24 1 5==>14 5 17 11 24 1  	第1行Ki
		; 			5 4  3  2  1 0
		;MOV	FLAG_TEMP,#00H		
		MOV	A,DES_C1			;14 11-->2 5
		MOV	C,ACC.2
		MOV	BIT_05H,C
		MOV	C,ACC.5
		MOV	BIT_02H,C
		
		MOV	A,DES_C2			;17 24-->7 0
		MOV	C,ACC.7
		MOV	BIT_03H,C		
		MOV	C,ACC.0
		MOV	BIT_01H,C
		
		MOV	A,DES_C0			;1 5-->7 3
		MOV	C,ACC.7
		MOV	BIT_00H,C
		MOV	C,ACC.3
		MOV	BIT_04H,C
		
		MOV	DES_Ki0,FLAG_TEMP				
		;32 1 2 3 4 5==>32 5 1 2 3 4		第1列Ln 32->48
		;                5 4 3 2 1 0
		MOV	FLAG_TEMP,#00H
		MOV	A,DES_Ln3			;32-->0
		MOV	C,ACC.0
		MOV	BIT_05H,C
		
		MOV	A,DES_Ln0			;1 2 3 4 5-->7 6 5 4 3 
		MOV	C,ACC.3
		MOV	BIT_04H,C	
		
		ANL	A,#0F0H
		SWAP	A
		ORL	A,FLAG_TEMP
		
		XRL	A,DES_Ki0			;对应位模2加,即异或		
		
		ANL	A,#3FH
		MOV	DPTR,#HDESSI0		;由表S0查询得Ki0高4位
		MOVC	A,@A+DPTR
		SWAP	A
		MOV	DES_Ki0,A
;-------------------------------------------------------------------------
;功能:得6bit,第1字节 DES_Ki0 
;-------------------------------------------------------------------------				
		;3 28 15 6 21 10==>3 10 28 15 6 21   	第2行Ki
		; 			   5  4  3  2 1  0
		;MOV	FLAG_TEMP,#00H
		MOV	A,DES_C0			;3,6-->5 2
		MOV	C,ACC.5
		MOV	BIT_05H,C
		MOV	C,ACC.2
		MOV	BIT_01H,C
		
		MOV	A,DES_C1			;15,10-->1,6
		MOV	C,ACC.1
		MOV	BIT_02H,C
		MOV	C,ACC.6
		MOV	BIT_04H,C
		
		MOV	A,DES_C2			;21-->3
		MOV	C,ACC.3
		MOV	BIT_00H,C
		
		MOV	A,DES_C3			;28-->4
		MOV	C,ACC.4
		MOV	BIT_03H,C
		
		MOV	R7,FLAG_TEMP
		;4 5 6 7 8 9==>4 9 5 6 7 8    		第2行Ln 32->48
		;              5 4 3 2 1 0
		MOV	FLAG_TEMP,#00H				
		MOV	A,DES_Ln1			;9-->7
		MOV	C,ACC.7
		MOV	BIT_04H,C
		
		MOV	A,DES_Ln0			;4 5 6 7 8-->4 3 2 1 0
		MOV	C,ACC.4
		MOV	BIT_05H,C
		ANL	A,#0FH
		ORL	A,FLAG_TEMP
				
		XRL	A,R7			;对应位模2加,即异或	
		
		ANL	A,#3FH
		MOV	DPTR,#HDESSI1		;由表S1查询得Ki0低4位
		MOVC	A,@A+DPTR
		ORL	DES_Ki0,A
;-------------------------------------------------------------------------
;功能:得6bit,第2字节高4位
;-------------------------------------------------------------------------			
		;23 19 12 4 26 8==>23 8 19 12 4 26
		;                   5 4  3  2 1  0
		;MOV	FLAG_TEMP,#00H
		MOV	A,DES_C0			;4 8-->4 0
		MOV	C,ACC.4
		MOV	BIT_01H,C
		MOV	C,ACC.0
		MOV	BIT_04H,C
		
		MOV	A,DES_C1			;12-->4
		MOV	C,ACC.4
		MOV	BIT_02H,C
		
		MOV	A,DES_C2			;19 23-->5 1
		MOV	C,ACC.5
		MOV	BIT_03H,C
		MOV	C,ACC.1
		MOV	BIT_05H,C
		
		MOV	A,DES_C3			;26-->6
		MOV	C,ACC.6
		MOV	BIT_00H,C
		
		MOV	DES_Ki1,FLAG_TEMP
		;8 9 10 11 12 13==>8 13 9 10 11 12
		;                  5  4 3  2  1  0
		MOV	FLAG_TEMP,#00H
		MOV	A,DES_Ln0			;8-->0
		MOV	C,ACC.0
		MOV	BIT_05H,C
		
		MOV	A,DES_Ln1			;9 10 11 12 13-->7 6 5 4 3
		MOV	C,ACC.3
		MOV	BIT_04H,C
		ANL	A,#0F0H
		SWAP	A
		ORL	A,FLAG_TEMP
		
		XRL	A,DES_Ki1			;对应位模2加,即异或
		
		ANL	A,#3FH
		MOV	DPTR,#HDESSI2		;由表S2查询得Ki1高4位
		MOVC	A,@A+DPTR
		SWAP	A
		MOV	DES_Ki1,A
;-------------------------------------------------------------------------
;功能:得6bit,第2字节 DES_Ki1
;-------------------------------------------------------------------------			
		;16 7 27 20 13 2==>16 2 7 27 20 13
		;                   5 4 3  2  1  0
		;MOV	FLAG_TEMP,#00H
		MOV	A,DES_C0			;2 7-->6 1
		MOV	C,ACC.6
		MOV	BIT_04H,C
		MOV	C,ACC.1
		MOV	BIT_03H,C
		
		MOV	A,DES_C1			;16,13-->0 3
		MOV	C,ACC.0
		MOV	BIT_05H,C
		MOV	C,ACC.3
		MOV	BIT_00H,C
		
		MOV	A,DES_C2			;20-->4
		MOV	C,ACC.4
		MOV	BIT_01H,C
		
		MOV	A,DES_C3			;27-->5
		MOV	C,ACC.5
		MOV	BIT_02H,C
		
		MOV	R7,FLAG_TEMP
		;12 13 14 15 16 17==>12 17 13 14 15 16
		;			      5  4  3  2  1  0
		MOV	FLAG_TEMP,#00H
		MOV	A,DES_Ln2			;17-->7
		MOV	C,ACC.7
		MOV	BIT_04H,C
		
		MOV	A,DES_Ln1			;12 13 14 15 16-->4 3 2 1 0 
		MOV	C,ACC.4
		MOV	BIT_05H,C
		ANL	A,#0FH
		ORL	A,FLAG_TEMP
			
		XRL	A,R7			;对应位模2加,即异或
		
		ANL	A,#3FH
		MOV	DPTR,#HDESSI3		;由表S3查询得Ki1低4位	
		MOVC	A,@A+DPTR		
		ORL	DES_Ki1,A
;-------------------------------------------------------------------------
;功能:得6bit,第3字节高4位 
;-------------------------------------------------------------------------			
		;41 52 31 37 47 55==>41 55 52 31 37 47
		;                     5  4  3  2  1  0
		;MOV	FLAG_TEMP,#00H
		MOV	A,DES_D0			;31-28=3-->5
		MOV	C,ACC.5
		MOV	BIT_02H,C
		
		MOV	A,DES_D1			;37-28=9 41-28=13-->7 3
		MOV	C,ACC.7
		MOV	BIT_01H,C
		MOV	C,ACC.3
		MOV	BIT_05H,C
		
		MOV	A,DES_D2			;47-28=19-->5	52-28=24-->0	
		MOV	C,ACC.5
		MOV	BIT_00H,C
		MOV	C,ACC.0
		MOV	BIT_03H,C
		
		MOV	A,DES_D3			;55-28=27-->5
		MOV	C,ACC.5
		MOV	BIT_04H,C
		
		MOV	DES_Ki2,FLAG_TEMP
		;16 17 18 19 20 21==>16 21 17 18 19 20
		;                     5  4  3  2  1  0
		MOV	FLAG_TEMP,#00H
		MOV	A,DES_Ln1			;16-->0
		MOV	C,ACC.0
		MOV	BIT_05H,C
		
		MOV	A,DES_Ln2			;21 17 18 19 20-->3 7 6 5 4
		MOV	C,ACC.3
		MOV	BIT_04H,C
		ANL	A,#0F0H
		SWAP	A
		ORL	A,FLAG_TEMP
		
		XRL	A,DES_Ki2			;对应位模2加,即异或
		
		ANL	A,#3FH
		MOV	DPTR,#HDESSI4		;由表S4查询得Ki2高4位
		MOVC	A,@A+DPTR
		SWAP	A
		MOV	DES_Ki2,A
;-------------------------------------------------------------------------
;功能:得6bit,第3字节DES_Ki2 
;-------------------------------------------------------------------------			
		;30 40 51 45 33 48==>30 48 40 51 45 33 
		;                     5  4  3  2  1  0
		;MOV	FLAG_TEMP,#00H
		MOV	A,DES_D0			;30-28=2-->6 33-28=5-->3
		MOV	C,ACC.6
		MOV	BIT_05H,C
		MOV	C,ACC.3
		MOV	BIT_00H,C
		
		MOV	A,DES_D1			;40-28=12-->4		
		MOV	C,ACC.4
		MOV	BIT_03H,C
		
		MOV	A,DES_D2			;48-28=20-->4 51-28=23-->1 45-28=17-->7
		MOV	C,ACC.4
		MOV	BIT_04H,C
		MOV	C,ACC.1
		MOV	BIT_02H,C
		MOV	C,ACC.7
		MOV	BIT_01H,C
		
			
		MOV	R7,FLAG_TEMP
		;20 21 22 23 24 25==>20 25 21 22 23 24
		;                     5  4  3  2  1  0
		MOV	FLAG_TEMP,#00H
		MOV	A,DES_Ln3			;25-->7
		MOV	C,ACC.7
		MOV	BIT_04H,C
		
		MOV	A,DES_Ln2			;20 21 22 23 24-->4 3 2 1 0
		MOV	C,ACC.4
		MOV	BIT_05H,C
		ANL	A,#0FH
		ORL	A,FLAG_TEMP
		
		XRL	A,R7			;对应位模2加,即异或
		
		ANL	A,#3FH
		MOV	DPTR,#HDESSI5		;由表S5查询得Ki2低4位
		MOVC	A,@A+DPTR
		ORL	DES_Ki2,A
;-------------------------------------------------------------------------
;功能:得6bit,第4字节高4位 
;-------------------------------------------------------------------------			
		;44 49 39 56 34 53==>44 53 49 39 56 34
		;                     5  4  3  2  1  0
		;MOV	FLAG_TEMP,#00H
		MOV	A,DES_D0		;34-28=6-->2 
		MOV	C,ACC.2
		MOV	BIT_00H,C
		
		MOV	A,DES_D1		;44-28=16-->0  39-28=11-->5
		MOV	C,ACC.0
		MOV	BIT_05H,C
		MOV	C,ACC.5
		MOV	BIT_02H,C
		
		MOV	A,DES_D2		;49-28=21-->3
		MOV	C,ACC.3
		MOV	BIT_03H,C  
		
		MOV	A,DES_D3		;53-28=25-->7  56-28=28-->4
		MOV	C,ACC.7
		MOV	BIT_04H,C
		MOV	C,ACC.4
		MOV	BIT_01H,C
		
		MOV	DES_Ki3,FLAG_TEMP
		;24 25 26 27 28 29==>24 29 25 26 27 28
		;                     5  4  3  2  1  0
		MOV	FLAG_TEMP,#00H
		MOV	A,DES_Ln2		;24-->0
		MOV	C,ACC.0
		MOV	BIT_05H,C
		
		MOV	A,DES_Ln3		;29 25 26 27 28-->3 7 6 5 4
		MOV	C,ACC.3
		MOV	BIT_04H,C
		ANL	A,#0F0H
		SWAP	A
		ORL	A,FLAG_TEMP
		
		XRL	A,DES_Ki3		;对应位模2加,即异或
		
		ANL	A,#3FH
		MOV	DPTR,#HDESSI6	;由表S6查询得Ki3高4位
		MOVC	A,@A+DPTR
		SWAP	A
		MOV	DES_Ki3,A
;-------------------------------------------------------------------------
;功能:得6bit,第4字节 DES_Ki3
;-------------------------------------------------------------------------			
		;46 42 50 36 29 32==>46 32 42 50 36 29
		;                     5  4  3  2  1  0
		;MOV	FLAG_TEMP,#00H
		MOV	A,DES_D0		;29-28=1-->7 32-28=4-->4 36-28=8-->0
		MOV	C,ACC.7
		MOV	BIT_00H,C
		MOV	C,ACC.4
		MOV	BIT_04H,C			
		MOV	C,ACC.0
		MOV	BIT_01H,C
		
		MOV	A,DES_D1		;42-28=14-->2
		MOV	C,ACC.2
		MOV	BIT_03H,C
		
		MOV	A,DES_D2		;50-28=22-->2 46-28=18-->6
		MOV	C,ACC.2
		MOV	BIT_02H,C
		MOV	C,ACC.6
		MOV	BIT_05H,C
		
		MOV	R7,FLAG_TEMP
		;28 29 30 31 32 1==>28 1 29 30 31 32 
		;                    5 4  3  2  1  0
		MOV	FLAG_TEMP,#00H
		MOV	A,DES_Ln0		;1-->7
		MOV	C,ACC.7
		MOV	BIT_04H,C
		
		MOV	A,DES_Ln3		;28-->4
		MOV	C,ACC.4
		MOV	BIT_05H,C
		
		MOV	A,DES_Ln3		;29 30 31 32-->3 2 1 0
		ANL	A,#0FH
		ORL	A,FLAG_TEMP
		
		XRL	A,R7		;对应位模2加,即异或	
		
		ANL	A,#3FH
		MOV	DPTR,#HDESSI7	;由表S7查询得Ki3低4位
		MOVC	A,@A+DPTR
		ORL	DES_Ki3,A
;-------------------------------------------------------------------------
;**********f(Rn-1,Kn).32bit置换,并得Rn=Ln-1 xor f(Rn-1,Kn)**************
;出口:DES_R0~3
;-------------------------------------------------------------------------
;功能:得Rn0
;-------------------------------------------------------------------------
		;16 7 20 21 29 12 28 17
		; 7 6  5  4  3  2  1  0
		;MOV	FLAG_TEMP,#00H
		MOV	A,DES_Ki1		;16-->0 12-->4
		MOV	C,ACC.0
		MOV	BIT_07H,C
		MOV	C,ACC.4
		MOV	BIT_02H,C
		
		MOV	A,DES_Ki0		;7-->1
		MOV	C,ACC.1
		MOV	BIT_06H,C
		
		MOV	A,DES_Ki2		;17-->7 20-->4 21-->3
		MOV	C,ACC.7
		MOV	BIT_00H,C
		MOV	C,ACC.4
		MOV	BIT_05H,C
		MOV	C,ACC.3
		MOV	BIT_04H,C
		
		MOV	A,DES_Ki3		;28-->4 29-->3
		MOV	C,ACC.4
		MOV	BIT_01H,C
		MOV	C,ACC.3
		MOV	BIT_03H,C
		MOV	A,FLAG_TEMP
		
		XRL	A,DES_L0
		MOV	DES_R0,A
;-------------------------------------------------------------------------
;功能:得Rn1
;-------------------------------------------------------------------------		
		;1 15 23 26 5 18 31 10
		;7  6  5  4 3  2  1  0	
		;MOV	FLAG_TEMP,#00H	
		MOV	A,DES_Ki0		;1 5-->7 3
		MOV	C,ACC.7
		MOV	BIT_07H,C
		MOV	C,ACC.3
		MOV	BIT_03H,C
		
		MOV	A,DES_Ki1		;10 15-->6 1
		MOV	C,ACC.6
		MOV	BIT_00H,C
		MOV	C,ACC.1
		MOV	BIT_06H,C
		
		MOV	A,DES_Ki2		;23 18-->1 6
		MOV	C,ACC.1
		MOV	BIT_05H,C
		MOV	C,ACC.6
		MOV	BIT_02H,C
		
		MOV	A,DES_Ki3		;26 31-->6 1
		MOV	C,ACC.6
		MOV	BIT_04H,C
		MOV	C,ACC.1
		MOV	BIT_01H,C
		
		MOV	A,FLAG_TEMP
		XRL	A,DES_L1
		MOV	DES_R1,A
;-------------------------------------------------------------------------
;功能:得Rn2
;-------------------------------------------------------------------------		
		;2 8 24 14 32 27 3 9
		;7 6  5  4  3  2 1 0
		;MOV	FLAG_TEMP,#00H
		MOV	A,DES_Ki0		;2 3 8-->6 5 0
		MOV	C,ACC.6
		MOV	BIT_07H,C
		MOV	C,ACC.5
		MOV	BIT_01H,C
		MOV	C,ACC.0
		MOV	BIT_06H,C
		
		MOV	A,DES_Ki1		;14-->2 9-->7
		MOV	C,ACC.2
		MOV	BIT_04H,C	
		MOV	C,ACC.7
		MOV	BIT_00H,C		
		
		MOV	A,DES_Ki2		;24-->0
		MOV	C,ACC.0
		MOV	BIT_05H,C
		
		MOV	A,DES_Ki3		;27 32-->5 0
		MOV	C,ACC.5
		MOV	BIT_02H,C
		MOV	C,ACC.0
		MOV	BIT_03H,C
		
		MOV	A,FLAG_TEMP
		XRL	A,DES_L2
		MOV	DES_R2,A
;-------------------------------------------------------------------------
;功能:得Rn3
;-------------------------------------------------------------------------		
		;19 13 30 6 22 11 4 25
		; 7  6  5 4  3  2 1  0
		;MOV	FLAG_TEMP,#00H
		MOV	A,DES_Ki0		;4 6-->4 2
		MOV	C,ACC.4
		MOV	BIT_01H,C
		MOV	C,ACC.2
		MOV	BIT_04H,C
		
		MOV	A,DES_Ki1		;11 13-->5 3
		MOV	C,ACC.5
		MOV	BIT_02H,C	
		MOV	C,ACC.3
		MOV	BIT_06H,C	
		
		MOV	A,DES_Ki2		;22-->2 19-->5
		MOV	C,ACC.2
		MOV	BIT_03H,C
		MOV	C,ACC.5
		MOV	BIT_07H,C
		
		MOV	A,DES_Ki3		;25 30-->7 2
		MOV	C,ACC.7
		MOV	BIT_00H,C
		MOV	C,ACC.2
		MOV	BIT_05H,C
		
		MOV	A,FLAG_TEMP
		XRL	A,DES_L3
		MOV	DES_R3,A
;-------------------------------------------------------------------------
		MOV	DES_L0,DES_Ln0	;DES_L0~3存储Ln-1
		MOV	DES_L1,DES_Ln1
		MOV	DES_L2,DES_Ln2
		MOV	DES_L3,DES_Ln3
		
		DEC	R5
		MOV	A,R5
		JZ	DES_GET_R16		
		AJMP	DES_LOOP16	;16次未到,则继续
;-------------------------------------------------------------------------	
;功能:得到R16-->DES_Rn0~3
;出口:DES_Rn0~3
;-------------------------------------------------------------------------
DES_GET_R16:	MOV	DES_Rn0,DES_R0	;16次到,得R16
		MOV	DES_Rn1,DES_R1
		MOV	DES_Rn2,DES_R2
		MOV	DES_Rn3,DES_R3
;-------------------------------------------------------------------------
;功能:R16L16初始置换的逆作用
;出口:#MIWEN 8字节(Ln0~3,Rn0~3)
;-------------------------------------------------------------------------
		MOV	DES_L0,DES_Ln0	;调整字节位置,以便循环操作
		MOV	DES_L1,DES_Rn0
		MOV	DES_L2,DES_Ln1
		MOV	DES_L3,DES_Rn1
		MOV	DES_R0,DES_Ln2
		MOV	DES_R1,DES_Rn2
		MOV	DES_R2,DES_Ln3
		MOV	DES_R3,DES_Rn3
		
		MOV	R1,#DES_TEMP0	;取得暂存首地址
		MOV	R7,#08H			;8个字节的8次循环
		
DES_GET_LR16A:
		MOV	R2,#00H			;位暂存清0	
		MOV	R6,#08H			;1个字节的8次循环
		MOV	R0,#MINGWEN		;取得首地址
		
DES_GET_LR16B:
		MOV	A,@R0		
		MOV	C,ACC.0			;取得1个字节的1位,acc.7的逆取acc.0
		MOV	A,R2
		RLC	A			;第1位为最高位
		MOV	R2,A
		MOV	A,@R0
		RR	A			;该数据移位,准备取下一位
		MOV	@R0,A
		INC	R0
		DJNZ	R6,DES_GET_LR16B	;1个字节ok?
		MOV	A,R2
		MOV	@R1,A			;ok则存储之
		INC	R1
		DJNZ	R7,DES_GET_LR16A	;8个字节都初始置换完毕?	
;-------------------------------------------------------------------------
;功能: DES_Rn0~3 异或 DES_Ln0~3
;出口:DES_Ln0~3
;-------------------------------------------------------------------------		
		ret
		MOV	A,DES_Rn0
		XRL	DES_Ln0,A

		MOV	A,DES_Rn1
		XRL	DES_Ln1,A

		MOV	A,DES_Rn2
		XRL	DES_Ln2,A

		MOV	A,DES_Rn3
		XRL	DES_Ln3,A		
		RET
;--------------------------------------------------------------------------
;***************************DES 加密算法结束*******************************		
;==========================================================================
;功能:4字节HEX-->4字节BCD,采用移位带进位自加1的算法;
;名称:HEX4_BCD4()
;入口:(高)DES_TEMP0,DES_TEMP1,DES_TEMP2,DES_TEMP3
;出口:(高)BCD_BYTE3,BCD_BYTE2,BCD_BYTE1,BCD_BYTE0
;--------------------------------------------------------------------------
HEX4_BCD4:  	CLR	A
		MOV	BCD_BYTE0,A
		MOV	BCD_BYTE1,A
		MOV	BCD_BYTE2,A
		MOV	BCD_BYTE3,A
		
		MOV	R7,#32
HEX4_BCD4_A:	MOV	R0,#DES_TEMP0+3
		MOV	R6,#04
		CLR	C
HEX4_BCD4_B:	MOV	A,@R0
		RLC	A
		MOV	@R0,A
		DEC	R0
		DJNZ	R6,HEX4_BCD4_B
		
		MOV	R6,#04
		MOV	R1,#BCD_BYTE0
HEX4_BCD4_C:	MOV	A,@R1
		ADDC	A,@R1
		DA	A
		MOV	@R1,A
		DEC	R1
		DJNZ	R6,HEX4_BCD4_C						
		DJNZ	R7,HEX4_BCD4_A
		RET
;==========================================================================
;TEST Des_Arithmetic
;--------------------------------------------------------------------------
MAIN:
		CLR	EA
		MOV	SP,#60H
		NOP
		MOV	48H,#38H		;64bits密钥
		MOV	49H,#38H
		MOV	4AH,#38H
		MOV	4BH,#38H
		MOV	4CH,#38H
		MOV	4DH,#38H
		MOV	4EH,#38H
		MOV	4FH,#38H
		
		MOV	38H,#30H		;64bits明文
		MOV	39H,#31H
		MOV	3AH,#32H
		MOV	3BH,#33H
		MOV	3CH,#34H
		MOV	3DH,#35H
		MOV	3EH,#36H
		MOV	3FH,#37H
		ACALL	Des_Arithmetic	;A6 0E A8 32 58 CC E8 ED=>LR异或 FEC240DFh  
		NOP
;		ACALL	HEX4_BCD4		;74143455
		NOP
		SJMP	$
		END		
	
      



 

喜欢这篇文章吗,不妨分享给朋友们吧!

科学是第一生产力