-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathViewController.swift
executable file
·190 lines (170 loc) · 5.33 KB
/
ViewController.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
//
// ViewController.swift
// RSACrypter
//
// Created by tsit.st2 on 2018/07/12.
// Copyright © 2018年 s162164. All rights reserved.
//
import UIKit
import Foundation
import BigInt
class ViewController: UIViewController,UITextFieldDelegate,UITextViewDelegate {
var p:BigInt = 0
var q:BigInt = 0
var r:BigInt = 0
var sample:String = "11111"
@IBOutlet weak var textView1: UITextView!
@IBOutlet weak var textView2: UITextView!
@IBOutlet weak var textField1: UITextField!
@IBOutlet weak var textField2: UITextField!
@IBOutlet weak var textField3: UITextField!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
textField1.delegate = self
textField2.delegate = self
textField3.delegate = self
textView1.delegate = self
textView2.delegate = self
textView1.text = ""
textView2.text = ""
p = PrimeNumber(bit: 512)
q = PrimeNumber(bit: 512)
r = PrimeNumber(bit: 512)
while true {
if CheckCrypt(p: p, q: q, r: r) {
break
} else {
p = PrimeNumber(bit: 512)
q = PrimeNumber(bit: 512)
r = PrimeNumber(bit: 512)
}
}
textField1.text = String(p)
textField2.text = String(q)
textField3.text = String(r)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
//暗号ボタンを押した時の処理
@IBAction func Encrypt(_ sender: UIButton) {
let pq:BigInt = p * q
let crypt:BigInt = Mod(num: BigInt(textView1.text)!, exp: BigInt(textField3.text!)!, a: pq)
textView2.text = String(crypt)
}
//復号ボタンを押した時の処理
@IBAction func Decrypt(_ sender: UIButton) {
let key:BigInt = privateKey(a: BigInt(textField1.text!)!, b: BigInt(textField2.text!)!, c: BigInt(textField3.text!)!)
let pq:BigInt = p * q
let crypt:BigInt = Mod(num: BigInt(textView2.text)!, exp: key, a: pq)
textView1.text = String(crypt)
}
//生成ボタンを押した時の処理
@IBAction func Generate(_ sender: UIButton) {
p = PrimeNumber(bit: 512)
q = PrimeNumber(bit: 512)
r = PrimeNumber(bit: 512)
while true {
if CheckCrypt(p: p, q: q, r: r) {
break
} else {
p = PrimeNumber(bit: 512)
q = PrimeNumber(bit: 512)
r = PrimeNumber(bit: 512)
}
}
textField1.text = String(p)
textField2.text = String(q)
textField3.text = String(r)
}
//素数生成
func PrimeNumber(bit: Int) -> BigInt {
while true {
var random = BigUInt.randomInteger(withExactWidth: bit)
random |= BigUInt(1)
if random.isPrime() {
return BigInt(random)
}
}
}
//最小公倍数の計算
func Lcm(a: BigInt, b: BigInt) -> BigInt {
var i:BigInt = a
var j:BigInt = b
var c:BigInt
let ij:BigInt = i * j
while i % j != 0 {
c = j
j = i % j
i = c
}
return ij / j
}
//べき乗計算
func Mod(num: BigInt, exp: BigInt, a: BigInt) -> BigInt {
var res:BigInt = 1
var number:BigInt = num
var e:BigInt = exp
while e > 0 {
if (e & 1) > 0 {
res = (res * number) % a
}
number = (number * number) % a
e >>= 1
}
return res
}
//秘密鍵生成
func privateKey(a: BigInt, b: BigInt, c: BigInt) -> BigInt {
let lcm = Lcm(a: a - 1, b: b - 1)
var a1:BigInt = 1
var b1:BigInt = 0
var c1:BigInt = lcm
var a2:BigInt = 0
var b2:BigInt = 1
var c2:BigInt = c
var a3:BigInt
var b3:BigInt
var c3:BigInt
var d:BigInt = 1
while c2 > 0 {
d = BigInt(c1 / c2)
a3 = a1 - d * a2
b3 = b1 - d * b2
c3 = c1 - d * c2
a1 = a2
b1 = b2
c1 = c2
a2 = a3
b2 = b3
c2 = c3
}
return (a1 > b1) ? a1 : b1
}
//3つの素数で暗号・復号できるか確認
func CheckCrypt(p: BigInt, q: BigInt, r: BigInt) -> Bool {
let pq:BigInt = p * q
var crypt:BigInt = Mod(num: BigInt(sample)!, exp: r, a: pq)
let key:BigInt = privateKey(a: p, b: q, c: r)
crypt = Mod(num: crypt, exp: key, a: pq)
if String(crypt) == sample {
return true
}
return false
}
//textFieldでreturnキー押す時の処理
func textFieldShouldReturn(_ textField: UITextField) -> Bool {
textField.resignFirstResponder()
return true
}
//textViewでreturnキー押す時の処理
func textView(_ textView: UITextView, shouldChangeTextIn range: NSRange, replacementText text: String) -> Bool {
if text == "\n" {
textView.resignFirstResponder()
return false
}
return true
}
}