加密解密问题,请问这用的什么算法?是根据什么进行加密和解密的?

kimurakenshin 2003-11-03 02:07:17
Option Explicit

' Encipher the text using the pasword.
'加密
Private Sub Cipher(ByVal password As String, ByVal from_text As String, to_text As String)
Const MIN_ASC = 32 ' Space.
Const MAX_ASC = 126 ' ~.
Const NUM_ASC = MAX_ASC - MIN_ASC + 1

Dim offset As Long
Dim str_len As Integer
Dim i As Integer
Dim ch As Integer

' Initialize the random number generator.
offset = NumericPassword(password)
Rnd -1
Randomize offset

' Encipher the string.
str_len = Len(from_text)
For i = 1 To str_len
ch = Asc(Mid$(from_text, i, 1))
If ch >= MIN_ASC And ch <= MAX_ASC Then
ch = ch - MIN_ASC
offset = Int((NUM_ASC + 1) * Rnd)
ch = ((ch + offset) Mod NUM_ASC)
ch = ch + MIN_ASC
to_text = to_text & Chr$(ch)
End If
Next i
End Sub
' Encipher the text using the pasword.
'解密
Private Sub Decipher(ByVal password As String, ByVal from_text As String, to_text As String)
Const MIN_ASC = 32 ' Space.
Const MAX_ASC = 126 ' ~.
Const NUM_ASC = MAX_ASC - MIN_ASC + 1

Dim offset As Long
Dim str_len As Integer
Dim i As Integer
Dim ch As Integer

' Initialize the random number generator.
offset = NumericPassword(password)
Rnd -1
Randomize offset

' Encipher the string.
str_len = Len(from_text)
For i = 1 To str_len
ch = Asc(Mid$(from_text, i, 1))
If ch >= MIN_ASC And ch <= MAX_ASC Then
ch = ch - MIN_ASC
offset = Int((NUM_ASC + 1) * Rnd)
ch = ((ch - offset) Mod NUM_ASC)
If ch < 0 Then ch = ch + NUM_ASC
ch = ch + MIN_ASC
to_text = to_text & Chr$(ch)
End If
Next i
End Sub


' Translate a password into an offset value.
Private Function NumericPassword(ByVal password As String) As Long
Dim value As Long
Dim ch As Long
Dim shift1 As Long
Dim shift2 As Long
Dim i As Integer
Dim str_len As Integer

str_len = Len(password)
For i = 1 To str_len
' Add the next letter.
ch = Asc(Mid$(password, i, 1))
value = value Xor (ch * 2 ^ shift1)
value = value Xor (ch * 2 ^ shift2)

' Change the shift offsets.
shift1 = (shift1 + 7) Mod 19
Debug.Print shift1
shift2 = (shift2 + 13) Mod 23
Debug.Print shift2
Next i
NumericPassword = value
End Function

Private Sub cmdCipher_Click()
Dim cipher_text As String
Cipher txtPassword.Text, txtPlain.Text, cipher_text
txtCipher.Text = cipher_text
txtPlain.Text = ""
End Sub

Private Sub cmdDecipher_Click()
Dim plain_text As String
Dim cipher_text As String
Decipher txtPassword.Text, txtCipher.Text, plain_text
txtPlain.Text = plain_text
txtCipher.Text = ""
End Sub

Private Sub Form_Load()

txtPassword.Text = Asc("c")
End Sub

Private Sub txtPassword_Change()
If Len(txtPassword.Text) > 0 Then
cmdCipher.Enabled = True
cmdDecipher.Enabled = True
Else
cmdCipher.Enabled = False
cmdDecipher.Enabled = False
End If
End Sub
...全文
60 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
heroren 2003-11-04
  • 打赏
  • 举报
回复
采用十进制逆进制方法将当前数据转换为十进制数据,然后逐位左移,再然后转换为ASCII码.
再进行正序转换.
kimurakenshin 2003-11-03
  • 打赏
  • 举报
回复
我在线,请回答!谢谢
kimurakenshin 2003-11-03
  • 打赏
  • 举报
回复
在上面自定义的函数中,把value的值左移的目的是什么?且shift1为什么要加7后mod19,shift2也是,目的何在?在加密和解密中只调用了一次函数,以后就没有再用.
yoki 2003-11-03
  • 打赏
  • 举报
回复
算法不是已经贴了么?这是自定义的算法

7,768

社区成员

发帖
与我相关
我的任务
社区描述
VB 基础类
社区管理员
  • VB基础类社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧