forked from Bandashah/9618-Paper-2-VB
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStringEncryptionWithRandomKeyGeneration.vb
More file actions
76 lines (68 loc) · 2.53 KB
/
StringEncryptionWithRandomKeyGeneration.vb
File metadata and controls
76 lines (68 loc) · 2.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
Module Module1
Sub Main()
Dim key(127) As String
Dim pt As String
Dim estring, dstring, dchar As String
Dim choice, x, upperbound, lowerbound As Integer
Dim character As Char
estring = ""
dstring = ""
pt = ""
lowerbound = 0
upperbound = 127
For c = 0 To 127
x = Int((upperbound - lowerbound + 1) * Rnd() + lowerbound)
key(c) = ChrW(x)
Next
While choice <> 3
Console.Write("press 1 to encrypt, 2 decrypt and 3 to exit :")
choice = Console.ReadLine()
Select Case choice
Case 1 : Console.Write("Enter the plain text to encrypt :")
pt = Console.ReadLine()
estring = EncryptString(key, pt)
Console.Write("the encrypted text :" & estring)
Case 2 : For n = 1 To Len(estring)
character = Mid(estring, n, 1)
dchar = decrypt(key, character)
dstring = dstring & dchar
Next
Console.WriteLine("the decrypted string : " & dstring)
Case 3
End Select
Console.WriteLine()
End While
Console.ReadKey()
End Sub
Function EncryptString(ByVal LookUp As Array, ByVal PlainText As String) As String
Dim OldChar, NewChar As Char
Dim OldCharValue As Integer
Dim OutString As String
'first initialise the return string
OutString = "" 'initialise the return string
'loop through PlainText to produce OutString
For n = 1 To Len(PlainText) 'from first to last character
OldChar = Mid(PlainText, n, 1) 'get next character
OldCharValue = Asc(OldChar) 'find the ASCII value
NewChar = LookUp(OldCharValue) 'look up substitute character
OutString = OutString & NewChar ' concatenate to OutString
Next
Return OutString 'EncryptString © OutString
End Function
Function decrypt(ByVal lookup As Array, ByVal chipherchar As Char) As Char
Dim found As Boolean
Dim index As Integer
Dim originalchar As Char
index = 1
found = False
While found = False
If lookup(index) = chipherchar Then
found = True
Else
index = index + 1
End If
End While
originalchar = Chr(index)
Return originalchar
End Function
End Module