-
Notifications
You must be signed in to change notification settings - Fork 0
/
PolyDecryptionViewController.swift
81 lines (62 loc) · 2.32 KB
/
PolyDecryptionViewController.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
//
// PolyDecryptionViewController.swift
// Cryptanalysis
//
// Created by steven lee on 11/6/16.
// Copyright © 2016 steven lee. All rights reserved.
//
import UIKit
class PolyDecryptionController: UIViewController
{
@IBOutlet weak var resultTextView: UITextView!
@IBOutlet weak var keyField: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
resultTextView.text = globalModifiedText
resultTextView.layer.borderWidth=1
resultTextView.layer.borderColor=UIColor.blackColor().CGColor
// Do any additional setup after loading the view, typically from a nib.
}
override func viewWillAppear(animated: Bool)
{
resultTextView.text = globalModifiedText
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func buttonPressed(sender: UIButton) {
let alphanumeric = "0123456789abcdefghijklmnopqrstuvwxyz"
let ctext = globalModifiedText.lowercaseString
let lengthOfCtext = ctext.characters.count
let key = keyField.text!.lowercaseString
let lengthOfKey = key.characters.count
var ptext = String()
for i in 0..<lengthOfCtext {
let indexOfCtext = ctext.startIndex.advancedBy(i)
let indexOfKey = key.startIndex.advancedBy(i%lengthOfKey)
let ctextNum = findCharNum(ctext[indexOfCtext])
if ctextNum == -1 {
ptext.append(ctext[indexOfCtext])
}
else{
let keyNum = findCharNum(key[indexOfKey])
let pchar = alphanumeric.startIndex.advancedBy((ctextNum + keyNum) % 36)
ptext.append(alphanumeric[pchar])
}
}
resultTextView.text = ptext
}
func findCharNum(c : Character) -> Int
{
let alphanumeric = "0123456789abcdefghijklmnopqrstuvwxyz"
var index = 0
if let idx = alphanumeric.characters.indexOf(c) {
index = alphanumeric.startIndex.distanceTo(idx)
}else {
// cannot find the char in the alphanumeric
index = -1
}
return index
}
}