-
Notifications
You must be signed in to change notification settings - Fork 0
/
VCP.lua
58 lines (46 loc) · 1.3 KB
/
VCP.lua
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
message = "THEJAVAPROGRAMMER"
keyText = "NEERAJ"
--msg = message.split("")
--key = keyText.split("")
--msgLen = msg.length
msg = {}
for i = 1,#message do
msg[i] = message:sub(i,i)
end
key = {}
for i=1,#keyText do
key[i] = keyText:sub(i,i)
end
msgLen = #message
newKey = {}
encryptedMsg = {}
decryptedMsg = {}
j = 1
for i = 1, msgLen do
if j == #keyText+1 then
j = 1
end
newKey[i] = key[j];
j = j + 1
end
for i = 1,msgLen do
encryptedMsg[i] = string.char(((string.byte(msg[i]) + string.byte(newKey[i])) % 26) + string.byte("A"))
--encryptedMsg[i] = string.char(((msg[i].charCodeAt(0) + newKey[i].charCodeAt(0)) % 26) + 'A'.charCodeAt(0))
end
for i = 1,msgLen do
decryptedMsg[i] = string.char((((string.byte(encryptedMsg[i]) - string.byte(newKey[i])) + 26) % 26) + string.byte("A"))
--decryptedMsg[i] = string.char(((((encryptedMsg[i].charCodeAt(0) - newKey[i].charCodeAt(0)) + 26) % 26) + 'A'.charCodeAt(0)))
end
function tablePrint(l)
str = ""
for i,v in pairs(l) do
--io.write(v)
str = str..v
end
return str
end
print("Original Message: "..tablePrint(msg))
print("Key: "..tablePrint(key))
print("Generated Key: "..tablePrint(newKey))
print("Encrypted Message: "..tablePrint(encryptedMsg))
print("Decrytped Message: "..tablePrint(decryptedMsg))