vb 字符串加密

第一次试水…只会vb(6.0)的某不知名高中生
第一篇就讲讲之前遇到的一个很有意思的题好了:

已知字符的ascii码可以用8位二进制数来表示,某同学想用以下加密方法进行加密:将原八位二进制码每四位进行前后交换
将得到的二进制码再转化为字符输出 即:0000 1111→1111 0000

解题思想:拿到这个题下手方向有两个 一是用mid函数强行进行交换 二是转化用16进制的数来考虑
分析一下第一个思路:虽然是可行的但是写出代码会比较冗长(对原ascii值进行取余倒数为二进制再用mid函数)理解上会更简单 但原题是个填空题需要以一行代码实现交换 显然不太可行
第二个:16进制数如18→81 会比较容易实现 而一行代码中实现交换并转为10进制输出也比较容易实现
注意点:对于字符要进行单个取出处理(想起来当时演示的时候有人问为什么不直接全部先除得到对应的值 没能及时解答这里补上 )全除后要对首末位处理比较困难orz
当时写的时候想过会不会有>9的情况 后来发现不用担心 进行╲及mod运算得到的都是<9的
(为了可以适用于输入一连串字符通用加了个循环
来看以下代码:

x=text1.text 
 for i=1 to len (x)  
  c=asc (mid (x,i,1))   
  d=(c mod 16)*16+c╲16  
  y=y+chr(d)  
 next i  
 text2.text=y

有更好的想法欢迎提出orz

下次主题:实现输出不重复的随机数或者是讲基础算法((仅适用于入门观望的小白…佬可以无视

VB源码--自定义数字&字符串加密工具 --VB源码 加密 解密 字符串 源码 *************以下为窗口及控件代码************ Private Sub Command1_Click() Label3.Caption = cipher(Text1.Text, Text2.Text) Label8.Caption = "加密完成!" End Sub Private Sub Command2_Click() Label4.Caption = Decipher(Text4.Text, Text3.Text) Label9.Caption = "解密完成!" End Sub Private Sub Label3_Click() Text3.Text = Label3.Caption End Sub Private Sub Label3_DblClick() Clipboard.SetText Label3.Caption Label8.Caption = "复制成功!" End Sub Private Sub Label4_DblClick() Clipboard.SetText Label4.Caption Label9.Caption = "复制成功!" End Sub ************以下为模块代码*************** ' Encipher the text using the pasword.加密 Public Function cipher(ByVal password As String, ByVal from_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 Dim to_text As String ' 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 cipher = to_text End Function ' Encipher the text using the pasword.解密 Public Function Decipher(ByVal password As String, ByVal from_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 Dim to_text As String ' 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 ***************** 省略部分代码(内详) ***************** If ch < 0 Then ch = ch + NUM_ASC ch = ch + MIN_ASC to_text = to_text & Chr$(ch) End If Next i Decipher = to_text End Function ' 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)) ***************** 省略部分代码(内详) ***************** Next i NumericPassword = value End Function
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值