Skip to content

Instantly share code, notes, and snippets.

@albert1205
Last active February 6, 2022 22:39
Show Gist options
  • Save albert1205/c8430b5bfa505f9308e4fa789b9b1d7f to your computer and use it in GitHub Desktop.
Save albert1205/c8430b5bfa505f9308e4fa789b9b1d7f to your computer and use it in GitHub Desktop.
Encrypt And Decrypt String by VBScript
Call EncryptTool
Call DecryptTool
'------------------------
'【 加密字符串 】
'------------------------
Private Function Encrypt(Plaintext)
Const offset = 10
Const minAsc = 33
Const maxAsc = 126
Dim Ciphertext
Randomize
Ciphertext = Ciphertext & Chr(Int((maxAsc-minAsc+1)*Rnd+minAsc))
Ciphertext = Ciphertext & Chr(Int((maxAsc-minAsc+1)*Rnd+minAsc))
For i=1 To Len(Plaintext)*2
If i mod 2 = 0 Then
newAsc = Asc(Mid(Plaintext,i/2,1)) - offset
If newAsc < minAsc Then
newAsc = newAsc + maxAsc - minAsc + 1
End If
Ciphertext = Ciphertext & Chr(newAsc)
' MsgBox Asc(Mid(Plaintext,i/2,1)) & " -> " & newAsc
Else
Ciphertext = Ciphertext & Chr(Int((maxAsc-minAsc+1)*Rnd+minAsc))
' MsgBox "Rnd:" & Chr(Int((maxAsc-minAsc+1)*Rnd+minAsc))
End If
Next
Ciphertext = Ciphertext & Chr(Int((maxAsc-minAsc+1)*Rnd+minAsc))
Ciphertext = Ciphertext & Chr(Int((maxAsc-minAsc+1)*Rnd+minAsc))
Encrypt = Ciphertext
End Function
Private Sub EncryptTool()
Plaintext = InputBox("在下面输入【明文】:","加密工具","")
Ciphertext = Encrypt(Plaintext)
InputBox "【明文】:" & Plaintext & vbNewLine & vbNewLine & "【密文】:如下","加密工具",Ciphertext
End Sub
'------------------------
'【 解密字符串 】
'------------------------
Private Function Decrypt(Ciphertext)
Const offset = 10
Const minAsc = 33
Const maxAsc = 126
If Len(Ciphertext) < 5 Then
Decrypt = ""
Exit Function
End If
Dim Plaintext
Ciphertext = Mid(Ciphertext,3,Len(Ciphertext)-4)
For i=2 To Len(Ciphertext) Step 2
oldAsc = Asc(Mid(Ciphertext,i,1)) + offset
If oldAsc > maxAsc Then
oldAsc = oldAsc - maxAsc + minAsc - 1
End If
Plaintext = Plaintext & Chr(oldAsc)
' MsgBox Asc(Mid(Ciphertext,i,1)) & " -> " & oldAsc
Next
Decrypt = Plaintext
End Function
Private Sub DecryptTool()
Ciphertext = InputBox("在下面输入【密文】:","解密工具","")
Plaintext = Decrypt(Ciphertext)
InputBox "【密文】:" & Ciphertext & vbNewLine & vbNewLine & "【明文】:如下","解密工具",Plaintext
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment