Stuck? Need help? Ask questions on our forums.

View DES Implementation\main_frm.frm

Implementation of Data Encryption Standard (DES) V1

Submitted By: meetsugan
Rating: starstarstarstar (Rate It)


VERSION 5.00
Object = "{F9043C88-F6F2-101A-A3C9-08002B2F49FB}#1.2#0"; "COMDLG32.OCX"
Begin VB.Form main_frm
   Caption         =   "DES Implementation"
   ClientHeight    =   3975
   ClientLeft      =   60
   ClientTop       =   450
   ClientWidth     =   5280
   Icon            =   "main_frm.frx":0000
   LinkTopic       =   "Form1"
   ScaleHeight     =   3975
   ScaleWidth      =   5280
   StartUpPosition =   3  'Windows Default
   Begin VB.CommandButton dec_btn
      Caption         =   "Decrypt a File"
      Height          =   615
      Left            =   2273
      TabIndex        =   10
      Top             =   1995
      Width           =   1455
   End
   Begin MSComDlg.CommonDialog CommonDialog1
      Left            =   240
      Top             =   2160
      _ExtentX        =   847
      _ExtentY        =   847
      _Version        =   393216
      DefaultExt      =   "*.*"
      DialogTitle     =   "Select File..."
      InitDir         =   "C:\"
   End
   Begin VB.TextBox Text3
      Height          =   375
      IMEMode         =   3  'DISABLE
      Left            =   1440
      TabIndex        =   5
      Text            =   "12345678"
      Top             =   720
      Visible         =   0   'False
      Width           =   3375
   End
   Begin VB.TextBox Text2
      Height          =   375
      Left            =   1440
      TabIndex        =   4
      Top             =   360
      Visible         =   0   'False
      Width           =   3375
   End
   Begin VB.TextBox Text1
      Height          =   375
      Left            =   1440
      TabIndex        =   3
      Text            =   "saravana"
      Top             =   0
      Visible         =   0   'False
      Width           =   3375
   End
   Begin VB.CommandButton quit_btn
      Caption         =   "&Quit"
      Height          =   375
      Left            =   3840
      TabIndex        =   2
      Top             =   3360
      Width           =   1215
   End
   Begin VB.CommandButton decrypt_btn
      Caption         =   "&Decrypt"
      Height          =   375
      Left            =   120
      TabIndex        =   1
      Top             =   3480
      Visible         =   0   'False
      Width           =   1935
   End
   Begin VB.CommandButton encrypt_btn
      Caption         =   "&Encrypt"
      Height          =   375
      Left            =   120
      TabIndex        =   0
      Top             =   3000
      Visible         =   0   'False
      Width           =   1935
   End
   Begin VB.CommandButton enc_btn
      Caption         =   "Encrypt a File"
      Height          =   615
      Left            =   2258
      Style           =   1  'Graphical
      TabIndex        =   9
      Top             =   1275
      Width           =   1455
   End
   Begin VB.Image Image2
      Height          =   705
      Left            =   1553
      Picture         =   "main_frm.frx":0ECA
      Stretch         =   -1  'True
      Top             =   1995
      Width           =   705
   End
   Begin VB.Image Image1
      Height          =   705
      Left            =   1553
      Picture         =   "main_frm.frx":1D94
      Stretch         =   -1  'True
      Top             =   1275
      Width           =   705
   End
   Begin VB.Label Label3
      AutoSize        =   -1  'True
      Caption         =   "Cipher Text"
      Height          =   195
      Left            =   480
      TabIndex        =   8
      Top             =   360
      Visible         =   0   'False
      Width           =   810
   End
   Begin VB.Label Label2
      AutoSize        =   -1  'True
      Caption         =   "Plain Text"
      Height          =   195
      Left            =   480
      TabIndex        =   7
      Top             =   120
      Visible         =   0   'False
      Width           =   705
   End
   Begin VB.Label Label1
      AutoSize        =   -1  'True
      Caption         =   "Password: "
      Height          =   195
      Left            =   480
      TabIndex        =   6
      Top             =   840
      Visible         =   0   'False
      Width           =   780
   End
End
Attribute VB_Name = "main_frm"
Attribute VB_GlobalNameSpace = False
Attribute VB_Creatable = False
Attribute VB_PredeclaredId = True
Attribute VB_Exposed = False
Option Explicit
Public password As String
Dim str1, str2, str3, str4 As String
Dim diff As Integer
Dim i, j As Integer
Dim asc_code As Long
Dim counter As Integer
Dim plain_text As String
Dim ip_permutation, ip_inv_permutation As String
Dim exp_permutation, permutation As String
Dim s_box(1 To 8) As String
Dim permuted_choice1, permuted_choice2, left_shifts As String
Dim l_block, r_block, c_block, d_block As String
Dim input_key As String
Dim round_no As Integer
Dim tmp_str1, tmp_str2 As String
Dim row_no, col_no As Integer
Dim sub_key(1 To 16) As String
Dim file_no1, file_no2 As Integer
Dim buffer As String


Private Sub enc_btn_Click()
Dim tmp1, tmp2 As String
Dim i, j As Integer
Dim file_name_2 As String
'Do Until CommonDialog1.FileName <> vbNullString
'CommonDialog1.ShowOpen
'Loop

CommonDialog1.ShowOpen
If CommonDialog1.FileName = vbNullString Then
Exit Sub
End If

file_name_2 = Mid$(CommonDialog1.FileName, 1, InStr(1, CommonDialog1.FileName, "."))

file_name_2 = file_name_2 + "enc"

'MsgBox file_name

'get_password_frm.Show
'get_password_frm.SetFocus
'Me.Hide

Do Until (password <> vbNullString And Len(password) = 8)
password = InputBox("Enter 8 character Password")
Loop
file_no1 = FreeFile

Open CommonDialog1.FileName$ For Input As #file_no1
file_no2 = FreeFile

Open file_name_2 For Output As #file_no2

While (Not EOF(file_no1))
Line Input #file_no1, tmp1
tmp2 = vbNullString
For i = 1 To Len(tmp1) Step 8
buffer = Mid$(tmp1, i, 8)
If Len(buffer) <> 8 Then
For j = 1 To 8 - Len(buffer)
buffer = buffer + " "
Next j
End If
DoEvents
encrypt
'MsgBox Str(Len(buffer))
tmp2 = tmp2 + buffer
Next i

'MsgBox Str(Len(tmp2))
Print #file_no2, tmp2
'MsgBox tmp1
Wend

Close #file_no2
Close #file_no1
MsgBox "The File " + CommonDialog1.FileName + " is Encrypted and Stored as " + file_name_2
End Sub

Private Sub dec_btn_Click()
Dim tmp1, tmp2 As String
Dim i, j As Integer
Dim line_len As Integer
Dim file_name_2 As String
'Do Until CommonDialog1.FileName <> vbNullString
'CommonDialog1.ShowOpen
'Loop

CommonDialog1.DefaultExt = "*.enc"

CommonDialog1.ShowOpen
If CommonDialog1.FileName = vbNullString Then
Exit Sub
End If

file_name_2 = Mid$(CommonDialog1.FileName, 1, InStr(1, CommonDialog1.FileName, "."))

file_name_2 = file_name_2 + "dec"

'MsgBox file_name

'get_password_frm.Show
'get_password_frm.SetFocus
'Me.Hide

Do Until (password <> vbNullString And Len(password) = 8)
password = InputBox("Enter 8 character Password")
Loop
file_no1 = FreeFile

Open CommonDialog1.FileName$ For Input As #file_no1
file_no2 = FreeFile

Open file_name_2 For Output As #file_no2

While (Not EOF(file_no1))
Line Input #file_no1, tmp1
line_len = Len(tmp1)
tmp2 = vbNullString
For i = 1 To Len(tmp1) Step 8
buffer = Mid$(tmp1, i, 8)
'MsgBox Str(Len(tmp1))
DoEvents

'MsgBox Str(Len(buffer))
decrypt
tmp2 = tmp2 + buffer
Next i
If (line_len = Len(tmp2)) Then
tmp2 = RTrim$(tmp2)
End If
Print #file_no2, tmp2
'MsgBox tmp1
Wend

Close #file_no1
Close #file_no2
MsgBox "The File " + CommonDialog1.FileName + " is Decrypted and Stored as " + file_name_2
End Sub


Private Sub decrypt_btn_Click()

round_no = 16
str1 = vbNullString
str2 = vbNullString
str3 = vbNullString
str4 = vbNullString
l_block = vbNullString
r_block = vbNullString
c_block = vbNullString
d_block = vbNullString
plain_text = vbNullString
input_key = vbNullString
tmp_str1 = vbNullString
tmp_str2 = vbNullString


str1 = Text3.Text

For i = 1 To Len(str1)
asc_code = (convert_to_binary(Asc(Mid$(str1, i, 1))))
str3 = LTrim$(Str(asc_code))
diff = 9 - Len(str3)

If diff > 1 Then
   
    For j = 1 To diff - 1
        str3 = "0" + str3
    Next j
   
End If

str2 = str2 + str3

Next i

input_key = str2
'input_key = Text3.Text

MsgBox input_key + Str(Len(input_key))

str1 = Text2.Text
str2 = ""

For i = 1 To Len(str1)
  'MsgBox Mid$(str1, i, 1)
asc_code = (convert_to_binary(Asc(Mid$(str1, i, 1))))

  'MsgBox asc_code
str3 = LTrim$(Str(asc_code))
diff = 9 - Len(str3)
If diff > 1 Then
For j = 1 To diff - 1
str3 = "0" + str3
Next j
End If

str2 = str2 + str3
'counter = counter + 1
'MsgBox str3
Next i

plain_text = str2
str4 = plain_text
ip

For i = 1 To 32
l_block = l_block + Mid$(plain_text, i, 1)
r_block = r_block + Mid$(plain_text, 32 + i, 1)
Next i


permuted_choice_1

generate_sub_keys

 'MsgBox "After Permuted Choice 1 " + input_key + Str(Len(input_key))

'For i = 1 To 28
'c_block = c_block + Mid$(input_key, i, 1)
'd_block = d_block + Mid$(input_key, 28 + i, 1)
'Next i



While (round_no >= 1)

plain_text = r_block
Expansion_permutation
str3 = vbNullString

'c_block = left_circular_shift(Val(Mid$(left_shifts, round_no, 1)), 1)
'd_block = left_circular_shift(Val(Mid$(left_shifts, round_no, 1)), 2)

'input_key = c_block + d_block

  'MsgBox Str(Len(plain_text)) + vbCrLf + Str(Len(input_key))

'permuted_choice_2

   'MsgBox Str(Len(plain_text)) + vbCrLf + Str(Len(input_key))

   'MsgBox r_block + vbCrLf + input_key + vbCrLf + Str(Len(input_key))

For i = 1 To 48
   
    If (Mid$(plain_text, i, 1) = Mid$(sub_key(round_no), i, 1)) Then
    str3 = str3 + "0"
    Else
    str3 = str3 + "1"
    End If
Next i
   
'MsgBox plain_text + vbCrLf + input_key + vbCrLf + str3

counter = 1
tmp_str2 = vbNullString
plain_text = str3

For i = 1 To 48 Step 6

str2 = Mid$(plain_text, i, 6)

tmp_str1 = Mid$(str2, 1, 1)
tmp_str1 = tmp_str1 + Mid$(str2, 6, 1)

row_no = convert_to_decimal 'converts the contents in tmp_str1 to decimal...

tmp_str1 = Mid$(str2, 2, 4)

col_no = convert_to_decimal 'converts the contents in tmp_str1 to decimal...

j = row_no * 16 + (col_no)
j = (j * 2) + 1

j = convert_to_binary(Val(Mid$(s_box(counter), j, 2)))

str3 = LTrim$(Str(j))

diff = 5 - Len(str3)

If diff > 1 Then
For j = 1 To diff - 1
str3 = "0" + str3
Next j
End If

tmp_str2 = tmp_str2 + str3

'MsgBox Str(counter) + "Row :" + Str(row_no) + vbCrLf + "Col :" + Str(col_no) + vbCrLf + tmp_str1
'MsgBox tmp_str2
counter = counter + 1

Next i
'After Substitution Boxes...
plain_text = tmp_str2

'MsgBox plain_text
permutation_P
'MsgBox tmp_str2 + vbCrLf + plain_text + Str(Len(plain_text))
'                       End of Fk...
'******************************************************
str3 = vbNullString
For i = 1 To 32
   
    If (Mid$(l_block, i, 1) = Mid$(plain_text, i, 1)) Then
    str3 = str3 + "0"
    Else
    str3 = str3 + "1"
    End If

Next i
'After X-Or-ing with Left Block...
l_block = r_block
r_block = str3



round_no = round_no - 1

Wend

' Swapping...
tmp_str1 = r_block
r_block = l_block
l_block = tmp_str1

plain_text = l_block + r_block

ip_inverse

' End of Encryption...
 'Text2.Text = plain_text
 'Exit Sub

'MsgBox str4 + Str(Len(str4)) + vbCrLf + plain_text + Str(Len(plain_text))

str2 = vbNullString
str3 = vbNullString
For i = 1 To 64 Step 8

tmp_str1 = Mid$(plain_text, i, 8)
str2 = LTrim$(Str(convert_to_decimal))
Text1.Text = Text1.Text + Chr(Val(str2))

Next i

MsgBox str4 + Str(Len(str4)) + vbCrLf + plain_text + Str(Len(plain_text)) + vbCrLf + str3



End Sub

Private Sub encrypt_btn_Click()

round_no = 1

str1 = Text3.Text

For i = 1 To Len(str1)
asc_code = (convert_to_binary(Asc(Mid$(str1, i, 1))))
str3 = LTrim$(Str(asc_code))
diff = 9 - Len(str3)

If diff > 1 Then
   
    For j = 1 To diff - 1
        str3 = "0" + str3
    Next j
   
End If

str2 = str2 + str3

Next i

input_key = str2
'input_key = Text3.Text

MsgBox input_key + Str(Len(input_key))

str1 = Text1.Text
str2 = ""

For i = 1 To Len(str1)
  'MsgBox Mid$(str1, i, 1)
asc_code = (convert_to_binary(Asc(Mid$(str1, i, 1))))

  'MsgBox asc_code
str3 = LTrim$(Str(asc_code))
diff = 9 - Len(str3)
If diff > 1 Then
For j = 1 To diff - 1
str3 = "0" + str3
Next j
End If

str2 = str2 + str3
'counter = counter + 1
'MsgBox str3
Next i

plain_text = str2
str4 = plain_text
ip

For i = 1 To 32
l_block = l_block + Mid$(plain_text, i, 1)
r_block = r_block + Mid$(plain_text, 32 + i, 1)
Next i


permuted_choice_1
'*********************************************************
'Generate Sub-Keys...
'*********************************************************

   'MsgBox "After Permuted Choice 1 " + input_key + Str(Len(input_key))

'For i = 1 To 28
'c_block = c_block + Mid$(input_key, i, 1)
'd_block = d_block + Mid$(input_key, 28 + i, 1)
'Next i


generate_sub_keys

While (round_no <= 16)

plain_text = r_block
Expansion_permutation
str3 = vbNullString

'c_block = left_circular_shift(Val(Mid$(left_shifts, round_no, 1)), 1)
'd_block = left_circular_shift(Val(Mid$(left_shifts, round_no, 1)), 2)

'input_key = c_block + d_block

  'MsgBox Str(Len(plain_text)) + vbCrLf + Str(Len(input_key))

'permuted_choice_2

  'MsgBox Str(Len(plain_text)) + vbCrLf + Str(Len(input_key))

'MsgBox r_block + vbCrLf + input_key + vbCrLf + Str(Len(input_key))


For i = 1 To 48
   
    If (Mid$(plain_text, i, 1) = Mid$(sub_key(round_no), i, 1)) Then
    str3 = str3 + "0"
    Else
    str3 = str3 + "1"
    End If
Next i
   
'MsgBox plain_text + vbCrLf + input_key + vbCrLf + str3

counter = 1
tmp_str2 = vbNullString
plain_text = str3

For i = 1 To 48 Step 6

str2 = Mid$(plain_text, i, 6)

tmp_str1 = Mid$(str2, 1, 1)
tmp_str1 = tmp_str1 + Mid$(str2, 6, 1)

row_no = convert_to_decimal 'converts the contents in tmp_str1 to decimal...

tmp_str1 = Mid$(str2, 2, 4)

col_no = convert_to_decimal 'converts the contents in tmp_str1 to decimal...

j = row_no * 16 + (col_no)
j = (j * 2) + 1

j = convert_to_binary(Val(Mid$(s_box(counter), j, 2)))

str3 = LTrim$(Str(j))

diff = 5 - Len(str3)

If diff > 1 Then
For j = 1 To diff - 1
str3 = "0" + str3
Next j
End If

tmp_str2 = tmp_str2 + str3

'MsgBox Str(counter) + "Row :" + Str(row_no) + vbCrLf + "Col :" + Str(col_no) + vbCrLf + tmp_str1
'MsgBox tmp_str2
counter = counter + 1

Next i
'After Substitution Boxes...
plain_text = tmp_str2

'MsgBox plain_text
permutation_P
'MsgBox tmp_str2 + vbCrLf + plain_text + Str(Len(plain_text))
'                       End of Fk...
'******************************************************
str3 = vbNullString
For i = 1 To 32
   
    If (Mid$(l_block, i, 1) = Mid$(plain_text, i, 1)) Then
    str3 = str3 + "0"
    Else
    str3 = str3 + "1"
    End If

Next i
'After X-Or-ing with Left Block...
l_block = r_block
r_block = str3



round_no = round_no + 1

Wend

' Swapping...
tmp_str1 = r_block
r_block = l_block
l_block = tmp_str1

plain_text = l_block + r_block

ip_inverse

' End of Encryption...
 'Text2.Text = plain_text
 'Exit Sub

'MsgBox str4 + Str(Len(str4)) + vbCrLf + plain_text + Str(Len(plain_text))

str2 = vbNullString
str3 = vbNullString
For i = 1 To 64 Step 8

tmp_str1 = Mid$(plain_text, i, 8)
str2 = LTrim$(Str(convert_to_decimal))
Text2.Text = Text2.Text + Chr(Val(str2))

Next i

MsgBox str4 + Str(Len(str4)) + vbCrLf + plain_text + Str(Len(plain_text)) + vbCrLf + str3



End Sub

Private Sub Form_Load()
'***********************************************************

ip_permutation = "58504234261810026052443628201204625446383022140664564840322416085749413325170901595143352719110361534537292113056355473931231507"

ip_inv_permutation = "40084816562464323907471555236331380646145422623037054513532161293604441252206028350343115119592734024210501858263301410949175725"

exp_permutation = "320102030405040506070809080910111213121314151617161718192021202122232425242526272829282930313201"

permutation = "1607202129122817011523260518311002082414322703091913300622110425"

s_box(1) = "14041301021511080310061205090007001507041402130110061211090503080401140813060211151209070310050015120802040901070511031410000613"

s_box(2) = "15010814061103040907021312000510031304071502081412000110060911050014071110041301050812060903021513081001031504021106071200051409"

s_box(3) = "10000914060315050113120711040208130700090304061002080514121115011306040908150300110102120510140701101300060908070415140311050212"

s_box(4) = "07131403000609100102080511120415130811050615000304070212011014091006090012110713150103140502080403150006100113080904051112070214"

s_box(5) = "02120401071011060805031513001409141102120407130105001510030908060402011110130708150912050603001411081207011402130615000910040503"

s_box(6) = "12011015090206080013030414070511101504020712090506011314001103080914150502081203070004100113110604030212090515101114010706000813"

s_box(7) = "04110214150008130312090705100601130011070409011014030512021508060104111312030714101506080005090206111308010410070905001514020312"

s_box(8) = "13020804061511011009031405001207011513081003070412050611001409020711040109121402000610131503050802011407041008131512090003050611"

permuted_choice1 = "5749413325170901585042342618100259514335271911036052443663554739312315076254463830221406615345372921130528201204"

permuted_choice2 = "141711240105032815062110231912042608160727201302415231374755304051453348444939563453464250362932"

left_shifts = "1122222212222221"

'***********************************************************

'For i = 1 To 64 Step 2
'MsgBox Mid$(permutation, i, 2)
'Next i


End Sub





Private Sub quit_btn_Click()
End
End Sub


Public Function convert_to_binary(dec As Variant) As Long
Dim temp As Long
Dim bin As Long
Dim c As Long
 

While (Abs(dec) > 0)
temp = dec Mod 2

bin = bin + (10 ^ c) * temp
c = c + 1

dec = dec / 2

If (InStr(1, dec, ".", vbTextCompare)) Then
dec = dec - 0.5
End If

Wend
convert_to_binary = bin
End Function


Public Sub ip()

Dim tmp_str As String
Dim i As Integer

For i = 1 To 128 Step 2
     
     tmp_str = tmp_str + Mid$(plain_text, Val(Mid$(ip_permutation, i, 2)), 1)
         
Next i
plain_text = tmp_str
End Sub



Public Sub Expansion_permutation()

Dim tmp_str As String
Dim i As Integer

For i = 1 To 96 Step 2
     
     tmp_str = tmp_str + Mid$(r_block, Val(Mid$(exp_permutation, i, 2)), 1)
         
Next i
plain_text = tmp_str

End Sub

Public Sub permuted_choice_1()
Dim tmp_str As String
Dim i As Integer

For i = 1 To 112 Step 2
     
     tmp_str = tmp_str + Mid$(input_key, Val(Mid$(permuted_choice1, i, 2)), 1)
         
Next i
input_key = tmp_str


End Sub

Public Function left_circular_shift(no_of_rotations As Integer, block_no As Integer) As String
Dim tmp_str, block As String
Dim i As Integer

If (block_no = 1) Then
block = c_block
Else
block = d_block
End If

For i = (no_of_rotations + 1) To 28
tmp_str = tmp_str + Mid$(block, i, 1)
Next i

If no_of_rotations = 1 Then
tmp_str = tmp_str + Mid$(block, 1, 1)
Else
tmp_str = tmp_str + Mid$(block, 1, 1)
tmp_str = tmp_str + Mid$(block, 2, 1)
End If
left_circular_shift = tmp_str
End Function

Public Sub permuted_choice_2()
Dim tmp_str As String
Dim i As Integer

For i = 1 To 96 Step 2
     
     tmp_str = tmp_str + Mid$(input_key, Val(Mid$(permuted_choice2, i, 2)), 1)
         
Next i
input_key = tmp_str

End Sub


Public Function convert_to_decimal() As Integer

Dim tmp, i As Integer

If (Len(tmp_str1) = 2) Then

tmp = 2 * Val(Mid$(tmp_str1, 1, 1))
tmp = tmp + 1 * Val(Mid$(tmp_str1, 2, 1))

ElseIf Len(tmp_str1) = 4 Then

tmp = 8 * Val(Mid$(tmp_str1, 1, 1))
tmp = tmp + 4 * Val(Mid$(tmp_str1, 2, 1))
tmp = tmp + 2 * Val(Mid$(tmp_str1, 3, 1))
tmp = tmp + 1 * Val(Mid$(tmp_str1, 4, 1))

Else

tmp = 128 * Val(Mid$(tmp_str1, 1, 1))
tmp = tmp + 64 * Val(Mid$(tmp_str1, 2, 1))
tmp = tmp + 32 * Val(Mid$(tmp_str1, 3, 1))
tmp = tmp + 16 * Val(Mid$(tmp_str1, 4, 1))
tmp = tmp + 8 * Val(Mid$(tmp_str1, 5, 1))
tmp = tmp + 4 * Val(Mid$(tmp_str1, 6, 1))
tmp = tmp + 2 * Val(Mid$(tmp_str1, 7, 1))
tmp = tmp + 1 * Val(Mid$(tmp_str1, 8, 1))

End If

convert_to_decimal = tmp
End Function

Public Function permutation_P()
Dim tmp_str As String
Dim i As Integer

For i = 1 To 64 Step 2
     
     'MsgBox Mid$(plain_text, Val(Mid$(permutation, i, 2)), 1)
     tmp_str = tmp_str + Mid$(plain_text, Val(Mid$(permutation, i, 2)), 1)
         
Next i
plain_text = tmp_str
End Function
Public Sub ip_inverse()

Dim tmp_str As String
Dim i As Integer

For i = 1 To 128 Step 2
     
     tmp_str = tmp_str + Mid$(plain_text, Val(Mid$(ip_inv_permutation, i, 2)), 1)
         
Next i
plain_text = tmp_str
End Sub


Public Sub generate_sub_keys()
'Input is input_key after permuted choice 1...
Dim i As Integer

c_block = vbNullString
d_block = vbNullString

For i = 1 To 28
c_block = c_block + Mid$(input_key, i, 1)
d_block = d_block + Mid$(input_key, 28 + i, 1)
Next i


For i = 1 To 16

c_block = left_circular_shift(Val(Mid$(left_shifts, i, 1)), 1)
d_block = left_circular_shift(Val(Mid$(left_shifts, i, 1)), 2)

permuted_choice_2

sub_key(i) = input_key

Next i

End Sub

Public Function get_block() As Boolean
Dim i As Integer
Dim tmp As String

Do Until EOF(file_no1)
tmp = tmp + Input$(1, #file_no1)
i = i + 1
If i = 8 Then
i = 0
buffer = tmp
get_block = True
Exit Function
Else
'If its not exacty divisible by 8...
buffer = tmp
get_block = False
End If

Loop


buffer = tmp

End Function

Public Sub encrypt()
round_no = 1

str1 = vbNullString
str2 = vbNullString
str3 = vbNullString
str4 = vbNullString
l_block = vbNullString
r_block = vbNullString
c_block = vbNullString
d_block = vbNullString
plain_text = vbNullString
input_key = vbNullString
tmp_str1 = vbNullString
tmp_str2 = vbNullString



str1 = password

For i = 1 To Len(str1)
asc_code = (convert_to_binary(Asc(Mid$(str1, i, 1))))
str3 = LTrim$(Str(asc_code))
diff = 9 - Len(str3)

If diff > 1 Then
   
    For j = 1 To diff - 1
        str3 = "0" + str3
    Next j
   
End If

str2 = str2 + str3

Next i

input_key = str2
'input_key = Text3.Text

'MsgBox input_key + Str(Len(input_key))

str1 = buffer
str2 = ""

For i = 1 To Len(str1)
  'MsgBox Mid$(str1, i, 1)
asc_code = (convert_to_binary(Asc(Mid$(str1, i, 1))))

  'MsgBox asc_code
str3 = LTrim$(Str(asc_code))
diff = 9 - Len(str3)
If diff > 1 Then
For j = 1 To diff - 1
str3 = "0" + str3
Next j
End If

str2 = str2 + str3
'counter = counter + 1
'MsgBox str3
Next i

plain_text = str2
str4 = plain_text

ip

For i = 1 To 32
l_block = l_block + Mid$(plain_text, i, 1)
r_block = r_block + Mid$(plain_text, 32 + i, 1)
Next i


permuted_choice_1
'*********************************************************
'Generate Sub-Keys...
'*********************************************************

   'MsgBox "After Permuted Choice 1 " + input_key + Str(Len(input_key))

'For i = 1 To 28
'c_block = c_block + Mid$(input_key, i, 1)
'd_block = d_block + Mid$(input_key, 28 + i, 1)
'Next i


generate_sub_keys

While (round_no <= 16)

plain_text = r_block
Expansion_permutation
str3 = vbNullString

'c_block = left_circular_shift(Val(Mid$(left_shifts, round_no, 1)), 1)
'd_block = left_circular_shift(Val(Mid$(left_shifts, round_no, 1)), 2)

'input_key = c_block + d_block

  'MsgBox Str(Len(plain_text)) + vbCrLf + Str(Len(input_key))

'permuted_choice_2

  'MsgBox Str(Len(plain_text)) + vbCrLf + Str(Len(input_key))

'MsgBox r_block + vbCrLf + input_key + vbCrLf + Str(Len(input_key))


For i = 1 To 48
   
    If (Mid$(plain_text, i, 1) = Mid$(sub_key(round_no), i, 1)) Then
    str3 = str3 + "0"
    Else
    str3 = str3 + "1"
    End If
Next i
   
'MsgBox plain_text + vbCrLf + input_key + vbCrLf + str3

counter = 1
tmp_str2 = vbNullString
plain_text = str3

For i = 1 To 48 Step 6

str2 = Mid$(plain_text, i, 6)

tmp_str1 = Mid$(str2, 1, 1)
tmp_str1 = tmp_str1 + Mid$(str2, 6, 1)

row_no = convert_to_decimal 'converts the contents in tmp_str1 to decimal...

tmp_str1 = Mid$(str2, 2, 4)

col_no = convert_to_decimal 'converts the contents in tmp_str1 to decimal...

j = row_no * 16 + (col_no)
j = (j * 2) + 1

j = convert_to_binary(Val(Mid$(s_box(counter), j, 2)))

str3 = LTrim$(Str(j))

diff = 5 - Len(str3)

If diff > 1 Then
For j = 1 To diff - 1
str3 = "0" + str3
Next j
End If

tmp_str2 = tmp_str2 + str3

'MsgBox Str(counter) + "Row :" + Str(row_no) + vbCrLf + "Col :" + Str(col_no) + vbCrLf + tmp_str1
'MsgBox tmp_str2
counter = counter + 1

Next i
'After Substitution Boxes...
plain_text = tmp_str2

'MsgBox plain_text
permutation_P
'MsgBox tmp_str2 + vbCrLf + plain_text + Str(Len(plain_text))
'                       End of Fk...
'******************************************************
str3 = vbNullString
For i = 1 To 32
   
    If (Mid$(l_block, i, 1) = Mid$(plain_text, i, 1)) Then
    str3 = str3 + "0"
    Else
    str3 = str3 + "1"
    End If

Next i
'After X-Or-ing with Left Block...
l_block = r_block
r_block = str3



round_no = round_no + 1

Wend

' Swapping...
tmp_str1 = r_block
r_block = l_block
l_block = tmp_str1

plain_text = l_block + r_block

ip_inverse

' End of Encryption...
 'Text2.Text = plain_text
 'Exit Sub

'MsgBox str4 + Str(Len(str4)) + vbCrLf + plain_text + Str(Len(plain_text))

str2 = vbNullString
str3 = vbNullString


For i = 1