Option Compare Database
Option Explicit
'VERSION 1.0 CLASS
'Um eine Zeichenfolge zu verschlüsseln:
'VerschlüsselterText = Encrypt("Beispieltext", "Beispielpasswort")
'Um eine Zeichenfolge wieder zu entschlüsseln:
'NormalerText = Decrypt("VerschüsselterString", "Beispielpasswort")
'Bei Passwörtern, die kürzer als 11 Zeichen sind, wird
'die Zeichenfolge mit 80 Bit verschlüsselt, ansonsten
'mit 128 Bit.
'_________________________________________________________________
' Copyright ©2000 by SHADOWare, Thomas Bachem
'
' [url=http://www.SHADOWare.de]SHADOWare.de[/url]
Private x1a0(9) As Long
Private cle(17) As Long
Private x1a2 As Long
Private inter As Long, res As Long, ax As Long, bx As Long
Private cx As Long, dx As Long, si As Long, tmp As Long
Private i As Long, C As Byte
Private Sub Assemble128()
x1a0(0) = ((cle(1) * 256) + cle(2)) Mod 65536
code128
inter = res
x1a0(1) = x1a0(0) Xor ((cle(3) * 256) + cle(4))
code128
inter = inter Xor res
x1a0(2) = x1a0(1) Xor ((cle(5) * 256) + cle(6))
code128
inter = inter Xor res
x1a0(3) = x1a0(2) Xor ((cle(7) * 256) + cle(8))
code128
inter = inter Xor res
x1a0(4) = x1a0(3) Xor ((cle(9) * 256) + cle(10))
code128
inter = inter Xor res
x1a0(5) = x1a0(4) Xor ((cle(11) * 256) + cle(12))
code128
inter = inter Xor res
x1a0(6) = x1a0(5) Xor ((cle(13) * 256) + cle(14))
code128
inter = inter Xor res
x1a0(7) = x1a0(6) Xor ((cle(15) * 256) + cle(16))
code128
inter = inter Xor res
i = 0
End Sub
Private Sub code128()
dx = (x1a2 + i) Mod 65536
ax = x1a0(i)
cx = &H15A
bx = &H4E35
tmp = ax
ax = si
si = tmp
tmp = ax
ax = dx
dx = tmp
If (ax <> 0) Then
ax = (ax * bx) Mod 65536
End If
tmp = ax
ax = cx
cx = tmp
If (ax <> 0) Then
ax = (ax * si) Mod 65536
cx = (ax + cx) Mod 65536
End If
tmp = ax
ax = si
si = tmp
ax = (ax * bx) Mod 65536
dx = (cx + dx) Mod 65536
ax = ax + 1
x1a2 = dx
x1a0(i) = ax
res = ax Xor dx
i = i + 1
End Sub
Public Function Encrypt128(ByVal Plaintext As String, ByVal Key As String) As String
Dim sData As String
Dim fois, champ1, lngchamp1, cfc, cfd, compte, d, e
si = 0
x1a2 = 0
i = 0
For fois = 1 To 16
cle(fois) = 0
Next fois
champ1 = Key
lngchamp1 = Len(champ1)
For fois = 1 To lngchamp1
cle(fois) = Asc(Mid(champ1, fois, 1))
Next fois
champ1 = Plaintext
lngchamp1 = Len(champ1)
For fois = 1 To lngchamp1
C = Asc(Mid(champ1, fois, 1))
Assemble128
cfc = (((inter / 256) * 256) - (inter Mod 256)) / 256
cfd = inter Mod 256
For compte = 1 To 16
cle(compte) = cle(compte) Xor C
Next compte
C = C Xor (cfc Xor cfd)
d = (((C / 16) * 16) - (C Mod 16)) / 16
e = C Mod 16
sData = sData & Chr$(&H61 + d)
sData = sData & Chr$(&H61 + e)
Next fois
Encrypt128 = sData
End Function
Public Function Decrypt128(ByVal Text As String, ByVal Key As String) As String
Dim sData As String
Dim fois, champ1, lngchamp1, cfc, cfd, compte, d, e
si = 0
x1a2 = 0
i = 0
For fois = 1 To 16
cle(fois) = 0
Next fois
champ1 = Key
lngchamp1 = Len(champ1)
For fois = 1 To lngchamp1
cle(fois) = Asc(Mid(champ1, fois, 1))
Next fois
champ1 = Text
lngchamp1 = Len(champ1)
For fois = 1 To lngchamp1
d = Asc(Mid(champ1, fois, 1))
If (d - &H61) >= 0 Then
d = d - &H61
If (d >= 0) And (d <= 15) Then
d = d * 16
End If
End If
If (fois <> lngchamp1) Then
fois = fois + 1
End If
e = Asc(Mid(champ1, fois, 1))
If (e - &H61) >= 0 Then
e = e - &H61
If (e >= 0) And (e <= 15) Then
C = d + e
End If
End If
Assemble128
cfc = (((inter / 256) * 256) - (inter Mod 256)) / 256
cfd = inter Mod 256
C = C Xor (cfc Xor cfd)
For compte = 1 To 16
cle(compte) = cle(compte) Xor C
Next compte
sData = sData & Chr$(C)
Next fois
Decrypt128 = sData
End Function
Private Sub Assemble80()
x1a0(0) = ((cle(1) * 256) + cle(2)) Mod 65536
code80
inter = res
x1a0(1) = x1a0(0) Xor ((cle(3) * 256) + cle(4))
code80
inter = inter Xor res
x1a0(2) = x1a0(1) Xor ((cle(5) * 256) + cle(6))
code80
inter = inter Xor res
x1a0(3) = x1a0(2) Xor ((cle(7) * 256) + cle(8))
code80
inter = inter Xor res
x1a0(4) = x1a0(3) Xor ((cle(9) * 256) + cle(10))
code80
inter = inter Xor res
i = 0
End Sub
Private Sub code80()
dx = (x1a2 + i) Mod 65536
ax = x1a0(i)
cx = &H15A
bx = &H4E35
tmp = ax
ax = si
si = tmp
tmp = ax
ax = dx
dx = tmp
If (ax <> 0) Then
ax = (ax * bx) Mod 65536
End If
tmp = ax
ax = cx
cx = tmp
If (ax <> 0) Then
ax = (ax * si) Mod 65536
cx = (ax + cx) Mod 65536
End If
tmp = ax
ax = si
si = tmp
ax = (ax * bx) Mod 65536
dx = (cx + dx) Mod 65536
ax = ax + 1
x1a2 = dx
x1a0(i) = ax
res = ax Xor dx
i = i + 1
End Sub
Private Function Encrypt80(ByVal Plaintext, ByRef Key) As String
Dim Crooked As String
Dim fois, champ1, lngchamp1, cfc, cfd, compte, d, e
si = 0
x1a2 = 0
i = 0
For fois = 1 To 10
cle(fois) = 0
Next fois
champ1 = Key
lngchamp1 = Len(champ1)
For fois = 1 To lngchamp1
cle(fois) = Asc(Mid(champ1, fois, 1))
Next fois
champ1 = Plaintext
lngchamp1 = Len(champ1)
For fois = 1 To lngchamp1
C = Asc(Mid(champ1, fois, 1))
Assemble80
cfc = (((inter / 256) * 256) - (inter Mod 256)) / 256
cfd = inter Mod 256
For compte = 1 To 10
cle(compte) = cle(compte) Xor C
Next compte
C = C Xor (cfc Xor cfd)
d = (((C / 16) * 16) - (C Mod 16)) / 16
e = C Mod 16
Crooked = Crooked + Chr$(&H61 + d)
Crooked = Crooked + Chr$(&H61 + e)
Next fois
Encrypt80 = Crooked
End Function
Private Function Decrypt80(ByVal EncryptedText, ByRef Key) As String
Dim Plaintext As String
Dim fois, champ1, lngchamp1, cfc, cfd, compte, d, e
si = 0
x1a2 = 0
i = 0
For fois = 1 To 10
cle(fois) = 0
Next fois
champ1 = Key
lngchamp1 = Len(champ1)
For fois = 1 To lngchamp1
cle(fois) = Asc(Mid(champ1, fois, 1))
Next fois
champ1 = EncryptedText
lngchamp1 = Len(champ1)
For fois = 1 To lngchamp1
d = Asc(Mid(champ1, fois, 1))
If (d - &H61) >= 0 Then
d = d - &H61
If (d >= 0) And (d <= 15) Then
d = d * 16
End If
End If
If (fois <> lngchamp1) Then
fois = fois + 1
End If
e = Asc(Mid(champ1, fois, 1))
If (e - &H61) >= 0 Then
e = e - &H61
If (e >= 0) And (e <= 15) Then
C = d + e
End If
End If
Assemble80
cfc = (((inter / 256) * 256) - (inter Mod 256)) / 256
cfd = inter Mod 256
C = C Xor (cfc Xor cfd)
For compte = 1 To 10
cle(compte) = cle(compte) Xor C
Next compte
Plaintext = Plaintext + Chr$(C)
Next fois
Decrypt80 = Plaintext
End Function
Public Function Encrypt(TextIn As String, Key As String) As String
Dim sKey As String
On Error GoTo TooLong:
If Len(Key) < 11 Then
sKey = Key & Space$(10 - Len(Key))
Encrypt = Encrypt80(TextIn, sKey)
Else
sKey = Key & Space$(16 - Len(Key))
Encrypt = Encrypt128(TextIn, sKey)
End If
Exit Function
TooLong:
MsgBox "Das Passwort darf maximal nur 16 Zeichen lang sein."
End Function
Public Function Decrypt(TextIn As String, Key As String) As String
Dim sKey As String
On Error GoTo TooLong:
If Len(Key) < 11 Then
sKey = Key & Space$(10 - Len(Key))
Decrypt = Decrypt80(TextIn, sKey)
Else
sKey = Key & Space$(16 - Len(Key))
Decrypt = Decrypt128(TextIn, sKey)
End If
Exit Function
TooLong:
MsgBox "Das Passwort darf maximal nur 16 Zeichen lang sein."
End Function
Public Function CreateKey(ByVal nLen As Integer) As String
' zufälligen Key aus Großbuchstaben und
' Zahlen ermitteln
Dim i As Integer
Dim nValue As Integer
Dim sKey As String
For i = 1 To nLen
Randomize -Timer
nValue = Int(1000 * Rnd)
If (nValue Mod 10 = 0) Or (nValue Mod 5 = 0) Or (nValue Mod 3 = 0) Then
' Zahl
Randomize -Timer
Do
nValue = Int(8 * Rnd + 1)
Loop Until nValue >= 1 And nValue <= 9
sKey = sKey & Chr$(48 + nValue)
Else
' Buchstabe
Randomize -Timer
Do
nValue = Int(26 * Rnd)
Loop Until nValue >= 1 And nValue <= 26 And nValue <> 9 And nValue <> 15
sKey = sKey & Chr$(97 + nValue)
End If
Next i
CreateKey = sKey
End Function