-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy path1033.bas
More file actions
106 lines (88 loc) · 1.93 KB
/
Copy path1033.bas
File metadata and controls
106 lines (88 loc) · 1.93 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
' Vigenere Cipher
'
' Based on the paperback book:
' Title: "The Everything Cryptograms book"
' Author: "Nikki Katz"
' ISBN: 1-59337-319-8
'
' See also: http://en.wikipedia.org/wiki/Vigen%C3%A8re_cipher
'
const a_index = asc(" ")
const z_index = asc("z")
'
' encode the phrase character with the given key character
'
func encode(phrase, key)
local phrase_index = asc(phrase)
local key_index = asc(key)
local code = phrase_index + (key_index - a_index)
if (code > z_index) then
code = a_index + (code - z_index) - 1
endif
encode = chr(code)
end
'
' decode the cypher character with the given key character
'
func decode(cypher, key)
local cypher_index = asc(cypher)
local key_index = asc(key)
local plain
if (key_index > cypher_index) then
plain = cypher_index + (z_index - key_index) + 1
else
plain = a_index + (cypher_index - key_index)
endif
decode = chr(plain)
end
'
' encrypt or decrypt the given phrase using key
'
func crypt(phrase, key, func_p)
local result = ""
local len_phrase = len(phrase) - 1
local len_key = len(key)
local k_i = 0
local i, p, k
for i = 0 to len_phrase
p = mid(phrase, i + 1, 1)
k = mid(key, k_i + 1, 1)
result += call(func_p, p, k)
k_i ++
if (k_i = len_key) then
k_i = 0
endif
next i
crypt = result
end
'
' wrapper function for crypt to encrypt the phrase
'
func encrypt(phrase, key)
encrypt = crypt(phrase, key, @encode)
end
'
' wrapper function for crypt to decrypt the cypher
'
func decrypt(cypher, key)
decrypt = crypt(cypher, key, @decode)
end
'
' unit test function
'
sub test
local cypher, plain, phrase, key
phrase = "Love all trust a few, do wrong to none"
key = "turnip"
cypher = encrypt(phrase, key)
plain = decrypt(cypher, key)
if (phrase != plain) then
? "Failed to decode"
else
? cypher
? plain
endif
end
key = "smallbasic"
cypher = "K_G]])'K7+s[YlR^Kk]U]m u"
? decrypt(cypher, key)