Mikle писал(а):Только нужно инициализировать алгоритм при каждом сеансе разным числом, а само число будет ключем к шифру.
Option Explicit
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 'Return/Encrypt or UnEncrypt/UnEncrypt
Private EncryptedLetter As String 'Character just Encrypted
Private strCryptMatrix(255) As String 'Matrix Array
Public Property Let KeyString(KeyString As String)
CodeWord = KeyString
End Property
Public Function Encrypt(Text As String) As String
Dim X As Integer, Y As Integer, Z As Integer
Dim C2E As String, Str2Encrypt As String
Str2Encrypt = Text: LS2E = Len(Text): 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, X As Integer
Matrix = ""
For X = 1 To 255
Matrix = Matrix & Chr$(X)
Next X
W = 1
LAM = Len(Matrix): strCryptMatrix(1) = Matrix
For X = 2 To LAM ' LAM = Length of Array Matrix
mov1 = Left(strCryptMatrix(W), 1)
mov2 = Right(strCryptMatrix(W), (LAM - 1))
strCryptMatrix(X) = mov2 + mov1 'Makes up each row of the Array
W = W + 1
Next X
End Sub
Dim C As Crypt, T1 As String, T2 As String, T3 As String
Set C = New Crypt
C.KeyString = "MyKeyString"
T1 = InputBox("Input text")
T2 = C.Encrypt(T1)
T3 = C.Encrypt(T2)
MsgBox "Text" & _
VbNewLine & "Original: " & T1 & _
VbNewLine & "Encrypted: " & T2 & _
VbNewLine & "Decrypted: " & T3
Шифрование выполняется примерно в 10 раз быстрее , чем DES.
Утверждается, что алгоритм устойчив к дифференциальному и линейному криптоанализу, что в нём нет никаких коротких циклов, и что он в высокой степени нелинеен. RC4 может находиться в ,примерно, 2^1700 возможных состояниях.S -блок медленно изменяется при использовании: i обеспечивает изменение каждого элемента, а j - что элементы изменяются случайным образом. Идею можно обобщить на S -блоки и слова больших размеров.
Option Explicit
Private Declare Sub CopyMem Lib "kernel32" Alias "RtlMoveMemory" (Destination As Any, Source As Any, ByVal Length As Long)
Event Progress(Percent As Long)
Private m_Key As String
Private m_sBox(0 To 255) As Integer
Private byteArray() As Byte
Private hiByte As Long
Private hiBound As Long
Public Function EncryptFile(InFile As String, OutFile As String, Overwrite As Boolean, Optional Key As String) As Boolean
On Error GoTo errorhandler
If FileExist(InFile) = False Then
EncryptFile = False
Exit Function
End If
If FileExist(OutFile) = True And Overwrite = False Then
EncryptFile = False
Exit Function
End If
Dim FileO As Integer, Buffer() As Byte
FileO = FreeFile
Open InFile For Binary As #FileO
ReDim Buffer(0 To LOF(FileO) - 1)
Get #FileO, , Buffer()
Close #FileO
Call EncryptByte(Buffer(), Key)
If FileExist(OutFile) = True Then Kill OutFile
FileO = FreeFile
Open OutFile For Binary As #FileO
Put #FileO, , Buffer()
Close #FileO
EncryptFile = True
Exit Function
errorhandler:
EncryptFile = False
End Function
Public Function DecryptFile(InFile As String, OutFile As String, Overwrite As Boolean, Optional Key As String) As Boolean
On Error GoTo errorhandler
If FileExist(InFile) = False Then
DecryptFile = False
Exit Function
End If
If FileExist(OutFile) = True Then
DecryptFile = False
Exit Function
End If
Dim FileO As Integer, Buffer() As Byte
FileO = FreeFile
Open InFile For Binary As #FileO
ReDim Buffer(0 To LOF(FileO) - 1)
Get #FileO, , Buffer()
Close #FileO
Call DecryptByte(Buffer(), Key)
If FileExist(OutFile) = True Then Kill OutFile
FileO = FreeFile
Open OutFile For Binary As #FileO
Put #FileO, , Buffer()
Close #FileO
DecryptFile = True
Exit Function
errorhandler:
DecryptFile = False
End Function
Public Sub DecryptByte(byteArray() As Byte, Optional Key As String)
Call EncryptByte(byteArray(), Key)
End Sub
Public Function EncryptString(Text As String, Optional Key As String, Optional OutputInHex As Boolean) As String
Dim byteArray() As Byte
byteArray() = StrConv(Text, vbFromUnicode)
Call EncryptByte(byteArray(), Key)
EncryptString = StrConv(byteArray(), vbUnicode)
If OutputInHex = True Then EncryptString = EnHex(EncryptString)
End Function
Public Function DecryptString(Text As String, Optional Key As String, Optional IsTextInHex As Boolean) As String
Dim byteArray() As Byte
If IsTextInHex = True Then Text = DeHex(Text)
byteArray() = StrConv(Text, vbFromUnicode)
Call DecryptByte(byteArray(), Key)
DecryptString = StrConv(byteArray(), vbUnicode)
End Function
Public Sub EncryptByte(byteArray() As Byte, Optional Key As String)
Dim i As Long, j As Long, Temp As Byte, Offset As Long, OrigLen As Long, CipherLen As Long, CurrPercent As Long, NextPercent As Long, sBox(0 To 255) As Integer
If (Len(Key) > 0) Then Me.Key = Key
Call CopyMem(sBox(0), m_sBox(0), 512)
OrigLen = UBound(byteArray) + 1
CipherLen = OrigLen
For Offset = 0 To (OrigLen - 1)
i = (i + 1) Mod 256
j = (j + sBox(i)) Mod 256
Temp = sBox(i)
sBox(i) = sBox(j)
sBox(j) = Temp
byteArray(Offset) = byteArray(Offset) Xor (sBox((sBox(i) + sBox(j)) Mod 256))
If (Offset >= NextPercent) Then
CurrPercent = Int((Offset / CipherLen) * 100)
NextPercent = (CipherLen * ((CurrPercent + 1) / 100)) + 1
RaiseEvent Progress(CurrPercent)
End If
Next
If (CurrPercent <> 100) Then RaiseEvent Progress(100)
End Sub
Private Sub Reset()
hiByte = 0
hiBound = 1024
ReDim byteArray(hiBound)
End Sub
Private Sub Append(ByRef StringData As String, Optional Length As Long)
Dim DataLength As Long
If Length > 0 Then DataLength = Length Else DataLength = Len(StringData)
If DataLength + hiByte > hiBound Then
hiBound = hiBound + 1024
ReDim Preserve byteArray(hiBound)
End If
CopyMem ByVal VarPtr(byteArray(hiByte)), ByVal StringData, DataLength
hiByte = hiByte + DataLength
End Sub
Private Function DeHex(Data As String) As String
Dim iCount As Double
Reset
For iCount = 1 To Len(Data) Step 2
Append Chr$(Val("&H" & Mid$(Data, iCount, 2)))
Next
DeHex = GData
Reset
End Function
Private Function EnHex(Data As String) As String
Dim iCount As Double, sTemp As String
Reset
For iCount = 1 To Len(Data)
sTemp = Hex$(Asc(Mid$(Data, iCount, 1)))
If Len(sTemp) < 2 Then sTemp = "0" & sTemp
Append sTemp
Next
EnHex = GData
Reset
End Function
Private Function FileExist(Filename As String) As Boolean
On Error GoTo errorhandler
Call FileLen(Filename)
FileExist = True
Exit Function
errorhandler:
FileExist = False
End Function
Private Property Get GData() As String
Dim StringData As String
StringData = Space(hiByte)
CopyMem ByVal StringData, ByVal VarPtr(byteArray(0)), hiByte
GData = StringData
End Property
Public Property Let Key(New_Value As String)
Dim a As Long, b As Long, Temp As Byte, Key() As Byte, KeyLen As Long
If (m_Key = New_Value) Then Exit Property
m_Key = New_Value
Key() = StrConv(m_Key, vbFromUnicode)
KeyLen = Len(m_Key)
For a = 0 To 255
m_sBox(a) = a
Next a
For a = 0 To 255
b = (b + m_sBox(a) + Key(a Mod KeyLen)) Mod 256
Temp = m_sBox(a)
m_sBox(a) = m_sBox(b)
m_sBox(b) = Temp
Next
End Property
Сейчас этот форум просматривают: Yandex-бот и гости: 55