加密文件
本例介绍如何加密文件。
Private Sub CodedFile(InputFile As String, OutputFile As String, PasswordKey As Integer)
Dim Char As String * 1
'打开要加密/解密的文件
Open InputFile For Binary As #1
'打开加密/解密后生成的文件
Open OutputFile For Binary As #2
For z = 1 To FileLen(InputFile)
'读取文件中的字符
Get 1, , Char
'与密钥进行异或运算
Char = Chr$((Asc(Char) Xor PasswordKey))
'将运算结果写入文件
Put 2, , Char
Next z
'关闭文件
Close #1
Close #2
End Sub
Private Sub Command1_Click()
Dim InputFile As String
Dim OutputFile As String
Dim PasswordKey As Integer
InputFile = InputBox("输入要加密/解密的文件名", "加密/解密")
OutputFile = InputBox("输入加密/解密后的文件名", "加密 / 解密到")
PasswordKey = InputBox("输入密钥(整数)", "输入密钥")
Call CodedFile(InputFile, OutputFile, PasswordKey)
MsgBox "文件已加密/解密到: " + OutputFile, , "完成"
End
End Sub
|