-
Notifications
You must be signed in to change notification settings - Fork 0
/
VigenereCipher.swift
127 lines (87 loc) · 4.43 KB
/
VigenereCipher.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
// VigenereCipher.swift
// VigenereCipher
// Copyright © 2019 Stephen Alger. All rights reserved.
class VigenereCipher {
//Declare constants which will be Hardcoded in intialiser
let ALPHABETSIZE: Int
let ALPHABET: String
//Declare key constants
let cipherKey: String
let keyLength: Int
//Initialiser
init(ALPHABET: String = "ABCDEFGHIJKLMNOPQRSTUVWXYZ", cipherKey: String) {
//set attributes & perform to Uppercase conversion
self.ALPHABET = ALPHABET.uppercased()
self.ALPHABETSIZE = ALPHABET.count
self.cipherKey = cipherKey.uppercased()
self.keyLength = cipherKey.count
}
//MARK: getAlphabetPosition - gets the alphabet index of any given character
private func getAlphabetPosition(character: Character) -> Int {
var index = 0
//Iterate through alphabet, returning
for char in ALPHABET {
if char == character {
return index
}
index += 1
}
return -1
}
//MARK: encryptionFunc - take one parameter (message to encipher), return enciphered string
func encryptionFunc(originalMsg: String) -> String {
//establish required variables
var returnMsg = ""
var index = 0
let firstIndex: String.Index = cipherKey.startIndex
//swift style for loop
for character in originalMsg {
//char by char obtain the index of the char in the Alphabet
let originalCharIndex = getAlphabetPosition(character: character)
//If character is not in alphabet i.e. whitespace - simply append to encrypted
if originalCharIndex == -1 {
returnMsg.append(character)
//iterate loop
continue
}
//deal with encryption key
//select character from key to encrypt this character from the original message using modulo arithmetic
let charToEncryptWith: Character = cipherKey[cipherKey.index(firstIndex, offsetBy: index % keyLength)]
//get the alphabet position of the encryption character just selected
let encryptionCharIndex = getAlphabetPosition(character: charToEncryptWith)
//Get the Vigenere encryption chars index - i.e. Use the formula to get the characters index that will replace the original character in the original msg [(originalCharIndex + encryptionCharIndex + 26) Modulo 26]
let encryptedLetterIndex = (originalCharIndex + encryptionCharIndex + ALPHABETSIZE) % ALPHABETSIZE
//append this encrypted letter to the returnMsg
returnMsg.append(ALPHABET[ALPHABET.index(firstIndex, offsetBy: encryptedLetterIndex)])
//iterate index
index += 1
}
return returnMsg
}
//MARK: decryptionFunc - take one parameter (message to decipher), return deciphered string
func decryptionFunc(encryptedMsg: String) -> String {
var returnMsg = ""
var index = 0
let firstIndex: String.Index = cipherKey.startIndex
for character in encryptedMsg {
let originalCharIndex = getAlphabetPosition(character: character)
//If character is not in alphabet i.e. whitespace - simply append to returnMsg
if originalCharIndex == -1 {
returnMsg.append(character)
//iterate loop
continue
}
//select character from key to decrypt character from the encrypted message using modulo arithmetic
let charToEncryptWith: Character = cipherKey[cipherKey.index(firstIndex, offsetBy: index % keyLength)]
//Get the decryption character index in alphabet
let decryptionCharIndex = getAlphabetPosition(character: charToEncryptWith)
//Get the decrypted letter's index in the alphabet
let decryptedLetterIndex = (originalCharIndex - decryptionCharIndex + ALPHABETSIZE) % ALPHABETSIZE
//append this decrypted letter to the returnMsg
returnMsg.append(ALPHABET[ALPHABET.index(firstIndex, offsetBy: decryptedLetterIndex)])
//iterate index
index += 1
}
return returnMsg
}
}