利用随机码进行加密

创建一个应用程序,再改变窗体的大小,并在窗体上放置2个ComandButton,3个Label和3个TextBox组件.将窗体、三个Label组件和两个ComandButton的Caption属性分别更改为:“简单的加密程序”,“加密密码”,“原文”,“密文”,“加密”,“解密”。

具体代码如下:

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
        ch = Asc(Mid$(password, i, 1))
        '将值左移shift1位后与value异或
        value = value Xor (ch * 2 ^ shift1)
        '将值左移shift2位后与value异或
        value = value Xor (ch * 2 ^ shift2)
        '改变移动偏移量
        shift1 = (shift1 + 7) Mod 19
        shift2 = (shift2 + 13) Mod 23
    Next i
    NumericPassword = value
   

End Function

Private Sub cmdCipher_Click()
Dim cipher_text As String
Const MIN_ASC = 32 '空格
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
   '初始化随机码生成器
   offset = NumericPassword(txtPassword.Text)
   Rnd -1
   Randomize offset
   '加密字符串
   str_len = Len(txtPlain.Text)
   For i = 1 To str_len
      ch = Asc(Mid$(txtPlain.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
        cipher_text = cipher_text & Chr$(ch)
      End If
    Next i
    txtCipher.Text = cipher_text
   
End Sub

Private Sub cmdDecipher_Click()
Dim plain_text As String
Const MIN_ASC = 32 '空格
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
   '初始化随机码生成器
   offset = NumericPassword(txtPassword.Text)
   Rnd -1
   Randomize offset
   '解密字符串
   str_len = Len(txtCipher.Text)
   For i = 1 To str_len
      ch = Asc(Mid$(txtCipher.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
        plain_text = plain_text & Chr$(ch)
      End If
    Next i
    txtPlain.Text = plain_text

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

——————————————————————————

欢迎各位同仁,互相交流,共同进步!

——————————————————————————

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值