Type rounds_key 'Storage Container raundnyh keys 
         key (255) As Byte 
End Type 

Global data_ () As Byte 'Declare global byte array 
Global key_ () As Byte 'Declare global byte array 
Global data_size_block As Long 'Declare global variable 4 bytes 
Global su_box () As Byte 'Declare global byte array 
Global inv_su_box () As Byte 'Declare global byte array 
Global rounds_key_x () As rounds_key 'Declare global multi byte array using container 
Global init_ok As Long 'Declare global variable 4 bytes 
'HHHHHHHHHHHHHHHHHHHHHH_TEST_FUNCTION_HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
Sub test_encrypt_decrypt () 

init_super_box 64 'initialization block size "64x8, 1bayt = 8 bits, values ​​from 0 to 255" 512-bit (512-bit key and a 512-bit block of data, call initialization routines before encryption or decryption required) 

set_text_block_to_array "1234567890", data_ (), 0 'Populate the global data buffer text "1234567890" 
set_text_block_to_array "1234567890", key_ (), 0 'Populate the global key buffer text "1234567890" 

encrypt_block data_ (), key_ () 'Encrypt 

'You should get 
'F2BB2C17F6EEDAB8DD7F50EAFC20CC05A63BB60F7B6BA98228F0225B8243FA1BFDB2374086DA344580CA06B9288C73E4333251EDB56F7D85E8AAAD9CFB09B337

MsgBox get_msg_block (data_ (), 1) 'visual output encrypted data in hex ​​format 

decrypt_block data_ (), key_ () 'stands for 

'You should get 
'1234567890 
'ie our encrypted data not counting zeros 

MsgBox get_msg_block (data_ (), 0) 'visual output the decrypted data as text 

End Sub 'End Sub 
'HHHHHHHHHHHHHHHHHHHHHH_TEST_FUNCTION_HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH

'HHHHHHHHHHHHHHHHHHHHHH_DECRYPT_FUNCTION_HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
Sub decrypt_block (input_output_data_block () As Byte, key_block () As Byte) 

Dim x As Long 'Declare a local variable 4 bytes 

If init_ok = 0 Then Exit Sub 'Validation initialization (optional) 

save_key rounds_key_x (0). key (), key_block () 'Create an array for the key worker 
gen_rounds_key rounds_key_x () 'Generating keys for rounds 

For x = data_size_block To 0 Step -1 'loop from 0 to data_size_block 

input_output_data_block (x) = inv_su_box (input_output_data_block (x)) Xor rounds_key_x (x). key (x) 'Recovering from the inverted byte lookup table 
xor_block input_output_data_block (), rounds_key_x (x). key () 'Mix with key data 

Next x 'Close the loop 

inv_dif_block input_output_data_block () 'Invert the diffusion data 

End Sub 'End Sub 
'HHHHHHHHHHHHHHHHHHHHHH_DECRYPT_FUNCTION_HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH

'HHHHHHHHHHHHHHHHHHHHHH_ENCRYPT_FUNCTION_HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
Sub encrypt_block (input_output_data_block () As Byte, key_block () As Byte) 

Dim x As Long 'Declare a local variable 4 bytes 

If init_ok = 0 Then Exit Sub 'Validation initialization (optional) 

save_key rounds_key_x (0). key (), key_block () 'Create an array for the key worker 
gen_rounds_key rounds_key_x () 'Generating keys for rounds 
dif_block input_output_data_block () 'produce diffusion data 

For x = 0 To data_size_block 'cycle from 0 to data_size_block 

xor_block input_output_data_block (), rounds_key_x (x). key () 'Mix with key data 
input_output_data_block (x) = su_box (input_output_data_block (x) Xor rounds_key_x (x). key (x)) 'Replace the bytes from the lookup table 

Next x 'Close the loop 

End Sub 'End Sub 
'HHHHHHHHHHHHHHHHHHHHHH_ENCRYPT_FUNCTION_HHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHHH
Sub save_key (input_output_key_block () As Byte, key_block () As Byte) 

Dim x As Long 'Declare a local variable 4 bytes 

For x = 0 To data_size_block 'cycle from 0 to data_size_block 

input_output_key_block (x) = key_block (x) 'Copy the data array with the key in the work array 

Next x 'Close the loop 

End Sub 'End Sub 
'================================================= =============================================== 
Sub gen_rounds_key (key_block () As rounds_key) 

Dim x As Long 'Declare a local variable 4 bytes 

For x = 0 To data_size_block 'cycle from 0 to data_size_block 

dif_block key_block (x). key () 'diffusion produce key 
If x <> data_size_block Then save_key rounds_key_x (x + 1). Key (), key_block (x). Key () 'Save the key to a new round of multi array 

Next x 'Close the loop 

End Sub 'End Sub 
'================================================= =============================================== 
Sub inv_dif_block (input_output_block () As Byte) 

Dim diff_sum As Long 'Declare a local variable 4 bytes 
Dim x As Long 'Declare a local variable 4 bytes 
Dim y As Long 'Declare a local variable 4 bytes 

For y = data_size_block To 0 Step -1 'loop from 0 to data_size_block sshagom -1 

diff_sum = inv_su_box (input_output_block (y)) 'Restore bytes of inverted lookup table 

For x = 0 To data_size_block 'cycle from 0 to data_size_block 

If x <> y Then 'Skipping certain cycles 
diff_sum = diff_sum Xor input_output_block (x) 'node-
input_output_block (x) = inv_su_box (input_output_block (x)) Xor x 'Restore bytes of inverted lookup table 
End If 

Next x 'Close the loop 

If y <> 0 Then 'Check the variable to 0 
input_output_block (y) = inv_su_box (diff_sum Xor inv_su_box (input_output_block (y - 1))) Xor y 'Recovering from the inverted byte lookup table with "y" is not equal to zero 
Else 
input_output_block (y) = inv_su_box (diff_sum) Xor y 'Recovering from the inverted byte lookup table with "y" is equal to zero 
End If 

Next y 'Close the loop 

End Sub 'End Sub 
'================================================= =============================================== 
Sub dif_block (input_output_block () As Byte) 

Dim diff_sum As Long 'Declare a local variable 4 bytes 
Dim x As Long 'Declare a local variable 4 bytes 
Dim y As Long 'Declare a local variable 4 bytes 

For y = 0 To data_size_block 'cycle from 0 to data_size_block 
For x = 0 To data_size_block 'cycle from 0 to data_size_block 

input_output_block (x) = su_box (input_output_block (x) Xor x) 'replaces a byte array from the lookup table 
diff_sum = diff_sum Xor input_output_block (x) 'node-

Next x 'Close the loop 

input_output_block (y) = su_box (diff_sum) 'Equate one cell array, the value from the lookup table 

Next y 'Close the loop 

End Sub 'End Sub 
'================================================= =============================================== 
Sub xor_block (input_output_block () As Byte, input_block () As Byte) 

Dim x As Long 'Declare a local variable 4 bytes 

For x = 0 To data_size_block 'cycle from 0 to data_size_block 

input_output_block (x) = input_output_block (x) Xor input_block (x) 'node-

Next x 'Close the loop 

End Sub 'End Sub 
'================================================= =============================================== 
Sub gen_invert_super_box (su_boxz () As Byte) 

Dim x As Long 'Declare a local variable 4 bytes 

For x = 0 To 255 'Loop from 0 to 255 

inv_su_box (su_boxz (x)) = x 'Invert the replacement table 

Next x 'Close the loop 

End Sub 'End Sub 
'================================================= =============================================== 
Sub init_super_box (sizex_box As Long) 'Sub-start 

Dim dats As String 'Declare a local variable string 
Dim x As Long 'Declare a local variable 4 bytes 
Dim z As Long 'Declare a local variable 4 bytes 

If sizex_box = 0 Or sizex_box> 256 Or (sizex_box Mod 2) Then 'Validation initialization data 
init_ok = 0 'Null variable 
Exit Sub 'If something is wrong then exit 
End If 

data_size_block = sizex_box - 1 'Calculate the size of the block c, taking into account that the array is addressed from scratch 

ReDim data_ (data_size_block) 'specifies the size of the dynamic array 
ReDim key_ (data_size_block) 'specifies the size of the dynamic array 
ReDim su_box (255) 'specifies the size of the dynamic array 
ReDim inv_su_box (255) 'specifies the size of the dynamic array 
ReDim rounds_key_x (data_size_block) 'specifies the size of the dynamic array 

"Location, value, and number of bytes in the lookup table can not be changed is a constant
'Total of 256 byte table from 0 to 255 scattered randomly 
dats = dats & "E5B0DF9CD4B74CEB3CF6C539151E1916F9E83E4936E3C00B06257ACAA7C8DED6" 'Merge string values ​​in one row 
dats = dats & "AB5F8FD56803FA9297984DF169B38DA16190001F515C85F426317742ECF308AD" 'Merge string values ​​in one row 
dats = dats & "3058DB9A994E489464ED6E290FEA37A8BEE2EFA956670DCF05548E1213C3669F" 'Merge string values ​​in one row 
dats = dats & "5DB5A3AC9B2ACEC2B2BD107E8A14D9605318CDE0860E814A3AE7590CD11D07AA" 'Merge string values ​​in one row 
dats = dats & "2B1B343BD3555A35328BDDE94683014B1AA252220293B604AFDA20845ED74457" 'Merge string values ​​in one row 
dats = dats & "11722E872DF7D0C7F2456D7F384376CCE673F87C8027B1FE3D89C141C9F06B88" 'Merge string values ​​in one row 
dats = dats & "D21C21332C17505BC6FDB87D6F790A4F7523E4097470EEDCFF4728AED896A5BF" 'Merge string values ​​in one row 
dats = dats & "24828C6A78636CBB7BA03FE162BA91C440F5B99DB42F6595719EA6FBCBBCA4FC" 'Merge string values ​​in one row 

For x = 1 To 512 Step 2 'Loop from 1 to 512 in increments of 2 

su_box (z) = Val ("& h" & Mid $ (dats, x, 2)) 'Populate the lookup table values ​​from a string by converting them from hexadecimal bytes in text form 
z = z + 1 'Increment the following value per unit cell array 

Next x 'Close the loop 

gen_invert_super_box su_box () 'Creates an inverted replacement table 

init_ok = 1 'Initialize held true 

End Sub 'End Sub 
'================================================= =============================================== 
Sub set_text_block_to_array (text_data As String, input_output_block () As Byte, hexz As Byte) 

Dim sizex_text As Long 'Declare a local variable 4 bytes 
Dim x As Long 'Declare a local variable 4 bytes 
Dim z As Long 'Declare a local variable 4 bytes 

If init_ok = 0 Then Exit Sub 'Validation initialization (optional) 

If hexz = 0 Then 'Check form text output 

sizex_text = Len (text_data) 'Determine the length of the string 

Else 

sizex_text = Len (text_data) \ 2 'Determine the length of the string in hexadecimal form 
z = 1 'Start at the first character 

End If 

For x = 0 To data_size_block 'cycle from 0 to data_size_block 

If (x + 1) <= sizex_text Then 'Check that would not go beyond the border of the text 

If hexz = 0 Then 'Check form text output 

input_output_block (x) = Asc (Mid $ (text_data, x + 1, 1)) And 255 'Fill the array with text data, transforming them into a byte array 

Else 

input_output_block (x) = Val ("& h" & Mid $ (text_data, z, 2)) 'Fill the array with text data in hexadecimal form, transforming them into a byte array 
z = z + 2 'jumps to the next two characters 

End If 

Else 

input_output_block (x) = 0 'Fill the array with zero bytes 

End If 

Next x 'Close the loop 

End Sub 'End Sub 
'================================================= =============================================== 
Function get_msg_block (input_block () As Byte, hexz As Byte) As String 

Dim dump As String 'Declare local peremennuyustroka 
Dim x As Long 'Declare a local variable 4 bytes 

If init_ok = 0 Then Exit Function 'Validation initialization (optional) 

For x = 0 To data_size_block 'Fill our string variable data from the block parallel converting them to hexadecimal system in a loop from 0 to data_size_block 

If hexz = 0 Then 'Check form text output 

dump = dump & Chr $ (input_block (x)) 'We add string values ​​to each other 

Else 

If input_block (x) <= 15 Then dump = dump & "0" 'Insert the leading zero if necessary 

dump = dump & Hex $ (input_block (x)) 'We add string values ​​to each other, c considering a leading zero ie if the value of "F" will be "0F" 

End If 

Next x 'Close the loop 

get_msg_block = dump 'returns a string 

End Function 'End Sub

Edited 2 Years Ago by Kev756

This article has been dead for over six months. Start a new discussion instead.