Dim input, output, password, codec As String
Dim b() As Byte
Dim c() As Byte
Dim d, k As Integer
Dim ren As Boolean
Public Function StringSHA512(ByVal keys As String)
Dim SHA512 As String = ""
Dim c As String
Dim key(100) As Byte
For n = 0 To keys.Length - 1
key(n) = Asc(keys.Chars(n))
Next
keys = Nothing
key = System.Security.Cryptography.SHA512.Create().ComputeHash(key)
For n = 0 To key.Length - 1
c = Conversion.Hex(key(n))
If c.Length = 1 Then
c = "0" & c
End If
keys &= c
Next
keys = keys.ToLower
Return keys
End Function
Sub Main()
0: password = ""
codec = ""
Console.Title = "文件加密"
Console.ForegroundColor = ConsoleColor.DarkGreen
Console.WriteLine("文件加密")
Console.ForegroundColor = ConsoleColor.White
Console.WriteLine("输入要编码/解码的文件的路径:")
1: Console.Write("> ")
input = Console.ReadLine
input = input.Replace("/", "\")
input = input.Replace("""", "")
If My.Computer.FileSystem.FileExists(input) = False Then
Console.ForegroundColor = ConsoleColor.Red
Console.WriteLine("您输入的路径无效!")
Console.ForegroundColor = ConsoleColor.White
GoTo 1
End If
Dim bytes() As Byte = My.Computer.FileSystem.ReadAllBytes(input)
Console.WriteLine("输入保存文件的路径:")
2: Console.Write("> ")
output = Console.ReadLine
output = output.Replace("/", "\")
output = output.Replace("""", "")
If My.Computer.FileSystem.DirectoryExists(Left(output, output.Length - output.Split("\")(output.Split("\").Length - 1).Length)) = False Then
Console.ForegroundColor = ConsoleColor.Red
Console.WriteLine("您输入的路径无效!")
Console.ForegroundColor = ConsoleColor.White
GoTo 2
End If
If ren = True Then GoTo 3
3: If My.Computer.FileSystem.FileExists(output) = True Then
Console.WriteLine("在为输出文件选择的位置中,有另一个文件。")
Console.WriteLine("键入""继续执行(0)""或""重新输入(1)""。")
4: Dim answer As String = Console.ReadLine.ToLower
If answer = "1" Then
ren = True
Console.WriteLine("输入保存文件的路径:")
GoTo 2
ElseIf answer = "0" Then
Console.WriteLine("继续进行:")
ElseIf Not answer = "0" Then
Console.ForegroundColor = ConsoleColor.Red
Console.WriteLine("无法识别该命令!")
Console.ForegroundColor = ConsoleColor.White
GoTo 4
End If
End If
Console.WriteLine("请输入密码:")
5: Console.Write("> ")
password = Console.ReadLine
If password = "" Then
Console.ForegroundColor = ConsoleColor.Red
Console.WriteLine("您尚未输入任何密码!请输入后重试。")
Console.ForegroundColor = ConsoleColor.White
GoTo 5
End If
password = StringSHA512(password)
Console.WriteLine("选择文件是应加密(0)还是解密(1):")
6: Console.Write("> ")
codec = Console.ReadLine
If Not codec = "0" And Not codec = "1" Then
Console.ForegroundColor = ConsoleColor.Red
Console.WriteLine("尚未输入"“0”" 或 "“1”",然后重试。")
Console.ForegroundColor = ConsoleColor.White
GoTo 6
End If
k = 0
Console.ForegroundColor = ConsoleColor.Blue
If codec = "1" Then
Console.WriteLine("文件解密开始了!")
ReDim c(0)
For n = 0 To bytes.Length - 1
If password.Length - 1 < k Then k = 0
d = bytes(n) + Asc(password.Chars(k))
If d > 255 Then c(0) = d - 256 Else c(0) = d
My.Computer.FileSystem.WriteAllBytes(output, c, True)
k += 1
Console.Title = "加密文件 正在进行中..." & (100 * n / (bytes.Length - 1)).ToString("0.000") & "%"
Next
Else
Console.WriteLine("文件加密开始!")
ReDim c(0)
For n = 0 To bytes.Length - 1
If password.Length - 1 < k Then k = 0
d = bytes(n) - Asc(password.Chars(k))
If d < 0 Then c(0) = 256 + d Else c(0) = d
My.Computer.FileSystem.WriteAllBytes(output, c, True)
k += 1
Console.Title = "加密文件 正在进行中... " & (100 * n / (bytes.Length - 1)).ToString("0.000") & "%"
Next
End If
bytes = Nothing
Console.ForegroundColor = ConsoleColor.Green
If codec = "1" Then Console.Write("解密") Else Console.Write("加密")
Console.WriteLine(" 执行成功!")
Console.ForegroundColor = ConsoleColor.White
Console.WriteLine("键入""0""以退出,键入""1""以继续。")
7: Console.Write("> ")
Dim cmd As String = Console.ReadLine.ToLower
If cmd = "1" Then
GoTo 0
ElseIf Not cmd = "0" Then
Console.ForegroundColor = ConsoleColor.Red
Console.WriteLine("无法识别该命令!")
Console.ForegroundColor = ConsoleColor.White
GoTo 7
End If
End Sub
End Module
|
暂时没有评论
发表评论 - 不要忘了输入验证码哦! |