-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.swift
247 lines (172 loc) · 7.64 KB
/
main.swift
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
//
// Vigenere Cipher
//
// Created by deathlezz on 12/07/2021.
//
import Foundation
extension String {
subscript(i: Int) -> Character {
return self[self.index(self.startIndex, offsetBy: i)]
}
}
print("* Welcome to Vigenere Cipher *")
func vigenere() {
class Vigenere {
let alphabet: String
let alphabetSize: Int
let key: String
let keySize: Int
init(alphabet: String = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'!?.,", key: String) {
self.alphabet = alphabet
self.alphabetSize = alphabet.count
self.key = key
self.keySize = key.count
}
// Index of alphabet
func alphabetIndex(forCharacter character: Character) -> Int {
var index = 0
for char in alphabet {
if char == character {
return index
}
index += 1
}
return -1
}
// Encrypt
func encrypt(plainText: String) -> String {
var encryptedText = ""
var index = 0
for char in plainText {
let indexInAlphabet = alphabetIndex(forCharacter: char)
if indexInAlphabet == -1 {
encryptedText.append(char)
continue
}
let keyToEncryptWith = key[index % keySize]
let keyIndexInAlphabet = alphabetIndex(forCharacter: keyToEncryptWith)
let encryptedLetterIndex = (indexInAlphabet + keyIndexInAlphabet + alphabetSize) % alphabetSize
encryptedText.append(alphabet[encryptedLetterIndex])
index += 1
}
return encryptedText
}
// Decrypt
func decrypt(encryptedText: String) -> String {
var decryptedText = ""
var index = 0
for char in encryptedText {
let indexInAlphabet = alphabetIndex(forCharacter: char)
if indexInAlphabet == -1 {
decryptedText.append(char)
continue
}
let keyToDecryptWith = key[index % keySize]
let keyIndexInAlphabet = alphabetIndex(forCharacter: keyToDecryptWith)
let decryptedLetterIndex = (indexInAlphabet - keyIndexInAlphabet + alphabetSize) % alphabetSize
decryptedText.append(alphabet[decryptedLetterIndex])
index += 1
}
return decryptedText
}
}
print()
print("Select operation:")
print("[1] Encrypt")
print("[2] Decrypt")
let userChoice = Int(readLine()!)
if userChoice == 1 { // Encrypt
func enterKey() {
print("Enter key:")
let key = readLine()
let keyChar = CharacterSet(charactersIn: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890")
if (key!.rangeOfCharacter(from: keyChar.inverted) != nil) {
print()
print("* Enter letters or numbers only *")
print()
return enterKey()
} else if key!.count >= 2 {
func enterText() {
let vigenereKey = Vigenere(key: key!)
print("Enter text to encrypt:")
let text = readLine()
let textChar = CharacterSet(charactersIn: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'!?., ")
if (text!.rangeOfCharacter(from: textChar.inverted) != nil) {
print()
print("* Bad input, try again *")
print()
return enterText()
} else if text != "" && !text!.hasPrefix(" ") && !text!.hasSuffix(" ") {
let encrypt = vigenereKey.encrypt(plainText: text!)
print()
print("Your key: \(key!)")
print("Plain text: \(text!)")
print("Encrypted text: \(encrypt)")
return vigenere()
} else {
print()
print("* Avoid whitespaces *")
print()
return enterText()
}
}
enterText()
} else {
print()
print("* Enter at least 2 characters *")
print()
return enterKey()
}
}
enterKey()
} else if userChoice == 2 { // Decrypt
func enterKey() {
print("Enter key:")
let key = readLine()
let keyChar = CharacterSet(charactersIn: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890")
if (key!.rangeOfCharacter(from: keyChar.inverted) != nil) {
print()
print("* Enter letters or numbers only *")
print()
return enterKey()
} else if key!.count >= 2 {
func enterText() {
let vigenereKey = Vigenere(key: key!)
print("Enter text to decrypt:")
let text = readLine()
let textChar = CharacterSet(charactersIn: "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890'!?., ")
if (text!.rangeOfCharacter(from: textChar.inverted) != nil) {
print()
print("* Bad input, try again *")
print()
return enterText()
} else if text != "" && !text!.hasPrefix(" ") && !text!.hasSuffix(" ") {
let decrypt = vigenereKey.decrypt(encryptedText: text!)
print()
print("Your key: \(key!)")
print("Encrypted text: \(text!)")
print("Plain text: \(decrypt)")
return vigenere()
} else {
print()
print("* Avoid whitespaces *")
print()
return enterText()
}
}
enterText()
} else {
print()
print("* Enter at least 2 characters *")
print()
return enterKey()
}
}
enterKey()
} else {
print()
print("* Enter 1 or 2 *")
return vigenere()
}
}
vigenere()