| xywhsoft |
2012-01-30 03:02 |
- 源代码:
Sub xywhEncry1(ByVal DataPtr As Any Ptr,ByVal DataSize As Integer,ByVal Paaword As ZString Ptr,ByVal PwSize As Integer) Dim SBox(1023) As Integer ' S盒[用于密钥的二次混淆] Dim KBox(63) As Integer ' K盒[用于密钥的一次混淆] Dim TPW(63) As Integer ' 临时密码盒[密码填充于此] Dim PwInt As Integer ' 二次混淆因子[用于S盒混淆的运算因子,取自K盒] Dim PwStep As Integer ' 原始密码的步数[一步=4字节] Dim DtStep As Integer ' 数据的计算步数 Dim RAM As Any Ptr ' 临时内存 Dim i As Integer ' 循环变量 Dim TmpInt As Integer ' 临时Int变量 PwStep = PwSize \ 4 ' 首先算出密码的步数[一步=4字节] If PwSize Mod 4 Then PwStep += 1 EndIf RAM = Allocate(PwStep*4) ' 将密码复制到临时容器中[为了末尾\0处理] CopyMemory(RAM,Paaword,PwSize) For i = 0 To 63 ' 使用密码填满“密码容器” CopyMemory(@TPW(i),((i Mod PwStep) * 4) + RAM,4) Next #Ifdef DebugMode putfile(ExePath & "\Dump-TPW.txt",@TPW(0),0,256) #EndIf For i = 0 To 63 ' 进行第一次对密钥的伪随机混淆[K盒混淆] Dim TB(1) As UShort CopyMemory(@TB(0),@TPW(i),4) TmpInt = (TmpInt + TB(0)) Mod 65536 TB(0) = TmpInt TmpInt = (TmpInt + TB(1)) Mod 65536 TB(1) = TmpInt CopyMemory(@KBox(i),@TB(0),4) PwInt = KBox(i) Xor TPW(i) ' 为第二次密钥伪随机混淆采集因子 Next #Ifdef DebugMode putfile(ExePath & "\Dump-KBox.txt",@KBox(0),0,256) #EndIf For i = 0 To 1023 ' 进行第二次密钥混淆[S盒混淆] Dim TB(1) As UShort PwInt = PwInt Xor KBox(i Mod 64) CopyMemory(@TB(0),@PwInt,4) TmpInt = (TmpInt + TB(0)) Mod 65536 TB(0) = TmpInt TmpInt = (TmpInt + TB(1)) Mod 65536 TB(1) = TmpInt CopyMemory(@SBox(i),@TB(0),4) Next #Ifdef DebugMode PutFile(ExePath & "\Dump-SBox.txt",@SBox(0),0,4096) #EndIf DtStep = DataSize \ 4 ' 准备加密使用的数据[四字节对其] If DataSize Mod 4 Then DtStep += 1 EndIf Dim DataOut(DtStep) As Integer CopyMemory(@DataOut(0),DataPtr,DataSize) For i = 0 To DtStep-1 ' 开始加密 Dim TB(1) As UShort CopyMemory(@TB(0),@SBox(i Mod 1024),4) TmpInt = (TmpInt + TB(0)) Mod 65536 TB(0) = TmpInt TmpInt = (TmpInt + TB(1)) Mod 65536 TB(1) = TmpInt CopyMemory(@SBox(i Mod 1024),@TB(0),4) DataOut(i) = DataOut(i) Xor SBox(i Mod 1024) Next ' 回写结果 CopyMemory(DataPtr,@DataOut(0),DataSize) #Ifdef DebugMode PutFile(ExePath & "\Dump-All.txt",@DataOut(0),0,DataSize) #EndIf End Sub
|
|