|
-
Jun 26th, 2000, 08:08 PM
#24
Fanatic Member
I had the same problem about a week ago. All of the information I needed was at http://ciphersaber.gurus.com/ and the links contained at the site.
I am by no means an expert but I can copy well.
In brief there a 5 steps that you need to go through to encrypt something.
1. Get you key phrase (password) from the user.
2. Create a key using the password.
What we are doing is getting the ASCII value of the password entered and assigning it to the Key.
3. Create an Initialization Vector (This is not required but is an excepted practice for CipherSaber)
The IV is used to further randomize the Encryption, without it if you enter the same text and passphrase the output text will always be the same. The only drawback is you have to save this randomly generated 10 byte array and then extract it when you want to decrypt the text. I put mine in the same file that I am putting my encrypted text in.
We take the IV and add it to the end of the end of the key.
4. Initialize the state.
The state is a 255 byte array which will be used later to encrypt the text.
We assign the bytes 0 to 255 to the array.
Using the combined key which contains the passphrase and the IV we really mix up the order of the state array.
5. Encrypt or Decrypt you text.
Now using the state you just shuffled this get XOR'd with the plaintext to be encrypted. Now honestly I don't understand how the XOR works but it does. Maybe somebody out there can explain it better.
I am also not sure why the state array is swapped again.
I hope this helps.
Steve
**** Global Variables Used ****
Dim State(0 To 255) As Integer 'S-Box
Dim Key(0 To 255) As Integer
Dim KeyLength As Integer
Dim IV(0 To 9) As Byte
**** Create a key using the password ****
Private Sub InitKey(PassPhrase As String)
' The Key is 256 bytes
Dim i As Integer
Dim j As Integer
j = 0
KeyLength = Len(PassPhrase)
' Initialize the Key array
For i = 0 To KeyLength - 1
j = j + 1
Key(i) = Asc(Mid$(PassPhrase, j, 1))
Next i
End Sub
**** Create an Initialaztion Vector ****
Function CreateIV(FileName As String) As Boolean
' initialization vector combined with the PassPhrase creates the Key
Dim i As Integer
Dim TempBinaryFile As Integer
CreateIV = False
If FileExists(FileName) = True Then Kill (FileName)
TempBinaryFile = FreeFile
Open FileName For Binary As TempBinaryFile
Randomize Timer
For i = 0 To 9
IV(i) = Rnd * 256
Key(KeyLength + i) = IV(i)
Put TempBinaryFile, , IV(i)
Next i
KeyLength = KeyLength + 10
Close TempBinaryFile
CreateIV = True
End Function
**** Read Initialaztion Vector (Only needed during Decryption) ****
Function ReadIV(FileName As String) As Boolean
' initialization vector combined with the PassPhrase creates the Key
Dim i As Integer
Dim TempBinaryFile As Integer
ReadIV = False
TempBinaryFile = FreeFile
Open FileName For Binary As TempBinaryFile
If LOF(TempBinaryFile) < 10 Then Exit Function
For i = 0 To 9
Get TempBinaryFile, , IV(i)
Key(KeyLength + i) = IV(i)
Next i
KeyLength = KeyLength + 10
Close TempBinaryFile
ReadIV = True
End Function
**** Initialize the state ****
Private Sub InitState()
' The State array is subjected to 256 mixing operations using a loop that steps "i" through the values from zero to 255. Each mixing operation consists of two steps:
'
' 1. Add to the variable "j" the contents of the "i"th element of the state array and the "n"th element of the Key array.
'
' 2. Swap the "i"th and "j"th elements of the state array.
Dim TempSwap As Integer
Dim i As Integer
Dim j As Integer
i = 0
j = 0
'INI S-Box
' Initialize the State array
For i = 0 To 255
State(i) = i
Next i
j = 0
For i = 0 To 255
j = (j + State(i) + Key(i Mod KeyLength)) Mod 256
' Swap( State(i),State(j) )
TempSwap = State(i)
State(i) = State(j)
State(j) = TempSwap
Next i
End Sub
**** Encrypt or Decrypt you text ****
Public Function EnDeCrypt(TempString As String) As String
' During the ciphering operation, the following steps are performed for each byte of the message:
'
' 1. The variable i is incremented by one.
'
' 2. The contents of the ith element of the state array is then added to j
'
' 3. The ith and jth elements of the state array are swapped and their contents are added together to form a new value n.
'
' 4. The nth element of the state array is then combined with the message byte, using a bit by bit exclusive-or operation, to form the output byte.
'
' 5. The same ciphering steps are performed for encryption and for decryption.
Dim Counter As Integer
Dim i As Integer
Dim j As Integer
Dim k As Integer
Dim TempSwap As Integer
Dim TempByteArrayOrg() As Byte
Dim TempByteArrayNew() As Byte
EnDeCrypt = ""
i = 0
j = 0
ReDim TempByteArrayOrg(Len(TempString))
ReDim TempByteArrayNew(Len(TempString))
'Convert the plain text to it's equivlant unicode values
TempByteArrayOrg = StrConv(TempString, vbFromUnicode)
' Save the individual bytes of the plain text in CipherString
For Counter = 0 To UBound(TempByteArrayOrg)
' If the plain text is greater than 256 characters then MOD i
i = (i + 1) Mod 256
j = (j + State(i)) Mod 256
' Swap( State(i),State(j) )
TempSwap = State(i)
State(i) = State(j)
State(j) = TempSwap
'Generate Keybyte k
k = State((State(i) + State(j)) Mod 256)
'Plaintextbyte xor Keybyte
TempByteArrayNew(Counter) = TempByteArrayOrg(Counter) Xor k
Next Counter
EnDeCrypt = StrConv(TempByteArrayNew, vbUnicode)
End Function
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|