浅谈字符和文件的加密 目前计算机应用越来越普遍,了解和懂得计算机知识方面的人越来越多,这无疑对于需要保密的文件和数据产生了更大的不安全性;因此数据和文件的安全性,越来越受到重视。本人在日常的学习和工作中,很在意这一点,也积累了一些小范例。下面我仅把我用过的两个应用程序拿给大家,希望从中我们能受到启发,也能从中获得收益。 字符串加密 字符串加密是对于字符的存储很有价值;即对于有限数据的安全性会起很大的作用,但不适用于文件的加密,它受到存储空间的限制。下面是一个应用程序执行后的界面。 尽管,你觉得看起来很简单,但是我们说只要能实现我们的目的就是很好的应用。 在此,你可以将要加密的字符输入或者粘贴你要加密的字符到原文一栏。 然后在相应的位置输入密码,它是你将字符加密的一个条件(必须输入,否则不预加密);同时它也是你对加密后的字符进行解密的一把钥匙,缺少它,你就不能对你所加密的字符进行解密了。所以你一定要记住密码。 下面是加密应用程序关于加密和解密的详细算法和程序: 加密程序(仅供参考): 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 在实际的应用中,字符串的加密是需要的,但是更多的是对文件的加密;因为文件的加密更加可靠和安全,它确保了数据的完整和安全性。比字符加密更加有一定的优势。 所以我觉得文件的加密更加值得我们的学习。 下面就来详细说明一下文件加密。 关于加密界面和一些相关代码如下(仅供参考): 先浏览你要加密的源文件,然后确定文件加密后存放的位置,即目标文件,然后 点击加密,这时确认你加密的密码(和上面一样这个密码是你解密的一把钥匙,没有它同样你不会将加密后的文件解密,你需要好好保存密码,建议你保存到软盘上,或者
在安全的地方) 密码输完后,文件就加密了!其实这个小程序和字符串加密没什么两样基本原理相同。 加密文件的算法: For Q = 1 To Len(Password) CurrChar = Mid(Password$, Q, 1) keyvalue = Asc(CurrChar) filenum = FreeFile X = Rnd(-keyvalue) Filename$ = txtdestination.Text 'The following code is labeled with obvious variables 'So it is easy to follow what is going on Open Filename$ For Binary As #filenum 'open the file name for output/input. For I = 1 To LOF(filenum) Get #filenum, I, singlechar charnum = Asc(singlechar) randominteger = Int(256 * Rnd) charnum = charnum Xor randominteger singlechar = Chr$(charnum) Put #filenum, I, singlechar Next I Close #filenum Next Q Close #filenum 文件解密的算法: For Q = Len(Password) To 1 Step -1 CurrChar = Mid(Password$, Q, 1) keyvalue = Asc(CurrChar) filenum = FreeFile X = Rnd(-keyvalue) Filename$ = txtdestination.Text 'The following code is labeled with obvious variables 'So it is easy to follow what is going on Open Filename$ For Binary As #filenum 'open the file name for output/input. For I = 1 To LOF(filenum) Get #filenum, I, singlechar charnum = Asc(singlechar) randominteger = Int(256 * Rnd) charnum = charnum Xor randominteger singlechar = Chr$(charnum) Put #filenum, I, singlechar Next I Close #filenum Next Q Close #filenum 以上仅是字符加密和文件加密的小应用,如果我们能够掌握加密的一些方法和技巧,我觉得会在实际应用中获得很大的收益,相信工作中也一定给我们带来更大的价值, 总之,文件的加密较字符的加密有更大的实用性,对于我们保存数据和确保资料的安全性有一定的好处。在此,我仅谈这些! 自动化信息所 2002年11月29日