?? form1.frm
字號:
VERSION 5.00
Begin VB.Form Form1
Caption = "Encipher & Decrypt"
ClientHeight = 5985
ClientLeft = 60
ClientTop = 345
ClientWidth = 8850
LinkTopic = "Form1"
ScaleHeight = 5985
ScaleWidth = 8850
StartUpPosition = 3 'Windows Default
Begin VB.PictureBox Picture2
Height = 1935
Left = 3240
ScaleHeight = 1875
ScaleWidth = 5235
TabIndex = 6
Top = 3960
Width = 5295
End
Begin VB.PictureBox Picture1
Height = 3255
Left = 3240
ScaleHeight = 3195
ScaleWidth = 5235
TabIndex = 5
Top = 360
Width = 5295
End
Begin VB.TextBox Text2
Height = 735
Left = 120
MultiLine = -1 'True
ScrollBars = 1 'Horizontal
TabIndex = 4
Top = 2280
Width = 2895
End
Begin VB.TextBox Text1
Height = 375
IMEMode = 3 'DISABLE
Left = 120
PasswordChar = "*"
TabIndex = 1
Top = 360
Width = 2895
End
Begin VB.CommandButton Command1
Caption = "先 加密 再 解密"
Enabled = 0 'False
Height = 615
Left = 120
TabIndex = 0
Top = 1080
Width = 2775
End
Begin VB.Label Label4
AutoSize = -1 'True
Caption = "解密后的字節:"
Height = 180
Left = 3240
TabIndex = 8
Top = 3720
Width = 1260
End
Begin VB.Label Label3
AutoSize = -1 'True
Caption = "加密后的字節:"
Height = 180
Left = 3240
TabIndex = 7
Top = 120
Width = 1260
End
Begin VB.Label Label2
AutoSize = -1 'True
Caption = "解密的字串:"
Height = 180
Left = 120
TabIndex = 3
Top = 2040
Width = 1080
End
Begin VB.Label Label1
AutoSize = -1 'True
Caption = "要加密的字串:"
Height = 180
Left = 120
TabIndex = 2
Top = 120
Width = 1260
End
End
Attribute VB_Name = "Form1"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Private Sub Command1_Click()
Dim X As Integer
Dim Y As Integer
Const Xd = 1
Const Yd = 2
Dim TxtByte() As Byte
Dim I As Integer
Dim txtPassWord() As Byte
Dim J As Integer
Dim str1 As String
If Encipher(Text1.Text, TxtByte) = False Then
MsgBox "Encipher Error( at least 1 byte) "
Exit Sub
End If
Dim FileNo As Integer
FileNo = FreeFile
Open "d:\guest.ppp" For Binary Access Write As #FileNo
'str1 = Str(TxtByte)
I = UBound(TxtByte)
Put #FileNo, , I
For J = 0 To I
Put #FileNo, , TxtByte(J)
Next J
Close #FileNo
Open "d:\guest.ppp" For Binary Access Read As #FileNo
Get #FileNo, , I
ReDim TxtByte(0 To I)
For J = 0 To I
Get #FileNo, , TxtByte(J)
Next J
Close #FileNo
If Decrypt(TxtByte, txtPassWord) = False Then MsgBox "Decrypt Error"
Text2.Text = txtPassWord 'Str(txtPassWord)
With Picture1
.Cls
Picture1.Scale (0, 0)-(12, 22)
For I = LBound(TxtByte) To UBound(TxtByte)
If I Mod 10 Then
X = X + Xd
Else
X = 0
Y = Y + Yd
End If
.CurrentX = X
.CurrentY = Y
Picture1.Print TxtByte(I)
Next I
End With
X = 0
Y = 0
With Picture2
.Cls
Picture2.Scale (0, 0)-(12, 22)
For I = LBound(txtPassWord) To UBound(txtPassWord)
If I Mod 10 Then
X = X + Xd
Else
X = 0
Y = Y + Yd
End If
.CurrentX = X
.CurrentY = Y
Picture2.Print txtPassWord(I)
Next I
End With
End Sub
Private Function Encipher(txtPassWord() As Byte, txtEncipher() As Byte) As Boolean
Dim MaskBit1 As Byte '掩碼變量
Dim MaskBit2 As Byte '掩碼變量
Dim I As Integer
If LenB(CStr(txtPassWord)) = 0 Then '錯誤攔截,判斷要加密的字串的長度是否為零,為零退出。
Encipher = False '加密不成功,返回FALSE。
Exit Function
End If
ReDim txtEncipher(0 To LenB(CStr(txtPassWord)) * 2) '為動態數組變量重新分配存儲空間,用于存儲加密后的字串。
Randomize '初始化隨機數生成器
For I = 0 To LenB(CStr(txtPassWord)) * 2 '產生2N+1個隨機數字節。
txtEncipher(I) = CByte(255 * Rnd + 0)
Next I
Call CreateMask(txtEncipher(0), MaskBit1, MaskBit2) '創建兩個掩碼
For I = 1 To LenB(CStr(txtPassWord)) * 2 Step 2 '根據掩碼中”1“的位置,把隨機序列的相應位置清零。
txtEncipher(I) = txtEncipher(I) And (Not MaskBit1)
txtEncipher(I + 1) = txtEncipher(I + 1) And (Not MaskBit2)
Next I
'下面的程序段把待加密的字串的每一位插入隨機數列中,插入的位置與兩掩碼中1的位置相同,待加密字節(BYTE)中的前4位(BIT)由MASKBIT1確定,后4位(BIT)由MASKBIT1確定。
Dim SP As Integer '指示待加密字串中位(BIT)位置的變量。
Dim M As Integer '指示掩碼中的位(BIT)的位置的變量
Dim T1 As Byte ' 中間變量。
For I = 1 To LenB(CStr(txtPassWord)) * 2 Step 2
'待加密的字串中字節(BYTE)的第0 到3位插入MASKBIT1規定的位置
SP = 0
For M = 0 To 7
If (MaskBit1 And 2 ^ M) <> 0 Then ' 判斷掩碼中”1“的位置。
T1 = (txtPassWord((I - 1) / 2) And 2 ^ SP) * 2 ^ (M - SP) ' / 2 ^ SP * 2 ^ M '把待加密的字節(BYTE)中的SP位,插入隨機序列的第M位。
txtEncipher(I) = txtEncipher(I) Or T1
SP = SP + 1
' Exit For
End If
Next M
'把待加密的字串中字節(BYTE)的 第4 到7位插入MASKBIT2規定的位置
SP = 4
For M = 0 To 7
If (MaskBit2 And 2 ^ M) <> 0 Then
T1 = (txtPassWord((I - 1) / 2) And 2 ^ SP) / 2 ^ SP * 2 ^ M
txtEncipher(I + 1) = txtEncipher(I + 1) Or T1
SP = SP + 1
End If
Next M
txtEncipher(I) = txtEncipher(I) Xor txtEncipher(0) '隨機序列的第1到2N個字節與第一個字節相異或(XOR),你可以拿任任意一個隨機字節來與其他字節相異或(XOR)而得到不同的加密方法。
txtEncipher(I + 1) = txtEncipher(I + 1) Xor txtEncipher(0)
Next I
Encipher = True '加密成功,返回TRUE。
End Function
Private Function Decrypt(txtEncipher() As Byte, txtPassWord() As Byte) As Boolean
Dim MaskBit1 As Byte
Dim MaskBit2 As Byte
Dim I As Integer
If LenB(CStr(txtEncipher)) < 3 Or LenB(CStr(txtEncipher)) Mod 2 = 0 Then '錯誤攔截,判斷要加密的字串的長度是否為零,為零退出
Decrypt = False
Exit Function
End If
ReDim txtPassWord(0 To (LenB(CStr(txtEncipher)) - 1) / 2 - 1)
Call CreateMask(txtEncipher(0), MaskBit1, MaskBit2) '創建兩個掩碼
Dim SP As Integer
Dim M As Integer
Dim T1 As Byte
For I = 1 To LenB(CStr(txtEncipher)) - 1 Step 2
txtEncipher(I) = txtEncipher(I) Xor txtEncipher(0)
txtEncipher(I + 1) = txtEncipher(I + 1) Xor txtEncipher(0)
SP = 0
For M = 0 To 7
If (MaskBit1 And 2 ^ M) <> 0 Then
T1 = (txtEncipher(I) And 2 ^ M) / 2 ^ M * 2 ^ SP
txtPassWord((I - 1) / 2) = txtPassWord((I - 1) / 2) Or T1
SP = SP + 1
End If
Next M
SP = 4
For M = 0 To 7
If (MaskBit2 And 2 ^ M) <> 0 Then
T1 = (txtEncipher(I + 1) And 2 ^ M) / 2 ^ M * 2 ^ SP
txtPassWord((I - 1) / 2) = txtPassWord((I - 1) / 2) Or T1
SP = SP + 1
End If
Next M
Next I
Decrypt = True
End Function
Private Sub CreateMask(TempByte As Byte, MaskBit1 As Byte, MaskBit2 As Byte)
Dim Number1 As Integer ' 記錄掩碼中1的個數
Dim BitZero As Byte '記錄掩碼中0的位置
For I = 0 To 7 Step 2 '先檢測第一個隨機數字節的第0,2,4,6 位是否為1,并把為1的位(BIT)移植到掩碼中。
If TempByte And 2 ^ I Then
MaskBit1 = MaskBit1 Or 2 ^ I
Number1 = Number1 + 1 '記錄掩碼中1的個數。
If Number1 >= 4 Then Exit For '如果掩碼中為1的位(BIT)的個數達到4,停止檢測。
Else
BitZero = BitZero Or 2 ^ I '記錄掩碼中不為1的位(BIT)
End If
Next I
If Number1 >= 4 Then GoTo FF '如果掩碼中為1的位(BIT)的個數達到4,停止檢測第1,3,5,7位。
For I = 1 To 7 Step 2 '檢測第一個隨機數字節的第1,3,5,7 位是否為1,并把為1的位(BIT)移植到掩碼中。
If TempByte And 2 ^ I Then
MaskBit1 = MaskBit1 Or 2 ^ I
Number1 = Number1 + 1
If Number1 >= 4 Then Exit For
Else
BitZero = BitZero Or 2 ^ I
End If
Next I
For I = Number1 + 1 To 4 '確包掩碼中1的個數等于4。
For J = 0 To 7
If BitZero And 2 ^ J Then '檢測掩碼中不為1的位(BIT),并移植到掩碼中。
BitZero = BitZero - 2 ^ J '標記該位(BIT)已被使用。
MaskBit1 = MaskBit1 Or 2 ^ J '把1移植到掩碼中
Exit For '移植1位后退出該此循環
End If
Next J
Next I
FF:
MaskBit2 = Not MaskBit1 '產生第二個掩碼。
End Sub
Private Sub Text1_Change()
If Len(Text1.Text) > 0 Then
Command1.Enabled = True
Else
Command1.Enabled = False
End If
End Sub
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -