另发一个VBS版本,来至网络上的VB代码- ' ====================================================================================================
- '德国的enigma加密类
- ' Download by http://www.codefans.net,yu2n 改写为VBS
- ' 使用:
- ' Msgbox GetEnigmaEncrypt("123456", "KATHER")
- Function GetEnigmaEncrypt( byVal EncryptStr, byVal KeyStr )
- Set MyEncrypt = New EnigmaEncrypt
- MyEncrypt.KeyString = ( KeyStr ) ' 初始密钥(6个字母,如"KATHER")
- GetEnigmaEncrypt = MyEncrypt.Encrypt( EncryptStr )
- End Function
- Class EnigmaEncrypt
- Private LCW 'As Integer 'Length of CodeWord
- Private LS2E 'As Integer 'Length of String to be Encrypted
- Private LAM 'As Integer 'Length of Array Matrix
- Private MP 'As Integer 'Matrix Position
- Private Matrix 'As String 'Starting Matrix
- Private mov1 'As String 'First Part of Replacement String
- Private mov2 'As String 'Second Part of Replacement String
- Private CodeWord 'As String 'CodeWord
- Private CWL 'As String 'CodeWord Letter
- Private EncryptedString 'As String 'String to Return for Encrypt or String to UnEncrypt for UnEncrypt
- Private EncryptedLetter 'As String 'Storage Variable for Character just Encrypted
- Private strCryptMatrix() 'As String 'yu2n 2013-1-28 生成动态数组 strCryptMatrix(LAM)
- 'Private strCryptMatrix(97) 'As String 'Matrix Array
-
- Public Property Let KeyString(sKeyString) 'As String)
- CodeWord = sKeyString
- End Property
-
- Public Function Encrypt(mstext) 'As String) As String
- Dim X 'As Integer ' Loop Counter
- Dim Y 'As Integer 'Loop Counter
- Dim Z 'As Integer 'Loop Counter
- Dim C2E 'As String 'Character to Encrypt
- Dim Str2Encrypt 'As String 'Text from TextBox
-
- Str2Encrypt = mstext
- LS2E = Len(mstext)
- LCW = Len(CodeWord)
- EncryptedLetter = ""
- EncryptedString = ""
-
- Y = 1
- For X = 1 To LS2E
- C2E = Mid(Str2Encrypt, X, 1)
- MP = InStr(1, Matrix, C2E, 0)
- CWL = Mid(CodeWord, Y, 1)
- For Z = 1 To LAM
- If Mid(strCryptMatrix(Z), MP, 1) = CWL Then
- EncryptedLetter = Left(strCryptMatrix(Z), 1)
- EncryptedString = EncryptedString + EncryptedLetter
- Exit For
- End If
- Next 'Z
- Y = Y + 1
- If Y > LCW Then Y = 1
- Next 'X
- Encrypt = EncryptedString
-
- End Function
-
- Private Sub Class_Initialize()
-
- Dim W 'As Integer 'Loop Counter to set up Matrix
- Dim X 'As Integer 'Loop through Matrix
- Dim I 'As Integer ' 生成ANSCII码
-
- 'yu2n 2013-1-11 按ANSI码表(32~126)生成母字符串
- For I = Asc(Chr(32)) To Asc(Chr(126))
- Matrix = Matrix + Chr(I)
- Next
- 'yu2n 2013-1-28 写入 .ini 文件时去掉空格与[]符号
- 'Matrix = Replace(Matrix, "[", "", 1, -1, 1) : Matrix = Replace(Matrix, "]", "", 1, -1, 1)
-
- 'Matrix = "8x3p5BeabcdfghijklmnoqrstuvwyzACDEFGHIJKLMNOPQRSTUVWXYZ 1246790-.#/\!@$<>&*()[]{}';:,?=+~`^|%_"
- 'Matrix = Matrix + Chr(13) 'Add Carriage Return to Matrix
- 'Matrix = Matrix + Chr(10) 'Add Line Feed to Matrix
- 'Matrix = Matrix + Chr(34) 'Add "
- ' Unique String used to make Matrix - 8x3p5Be
- ' Unique String can be any combination that has a character only ONCE.
- ' EACH Letter in the Matrix is Input ONLY once.
-
- W = 1
- 'yu2n 2013-1-28 生成动态数组 strCryptMatrix(LAM)
- LAM = Len(Matrix) : ReDim Preserve strCryptMatrix(LAM) : strCryptMatrix(1) = Matrix
-
- For X = 2 To LAM ' LAM = Length of Array Matrix
- mov1 = Left(strCryptMatrix(W), 1) 'First Character of strCryptMatrix
- mov2 = Right(strCryptMatrix(W), (LAM - 1)) 'All but First Character of strCryptMatrix
- strCryptMatrix(X) = mov2 & mov1 'Makes up each row of the Array
- W = W + 1
- Next 'X
-
- End Sub
- End Class
复制代码
|