-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathlib.swift
38 lines (36 loc) · 979 Bytes
/
lib.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
/*********************************
*
* Use: "Hello World!".encrypt([134])
* => "Îãêêé¦Ñéôêâ§"
* "U[êqQ¦JQôqZ§".decrypt([134])
* => "Hello World!"
*
*********************************/
import Foundation
//ascii code
extension Character
{
func unicodeValue() -> UInt32
{
let characterString = String(self)
let scalars = characterString.unicodeScalars
return scalars[scalars.startIndex].value
}
}
// main
extension String {
func encrypt(_ keyValue: Array<Int>) -> String {
var out = "";
let inArray = Array(self);
let keyArray = keyValue;
for i in 0...self.count - 1 {
let c = Int(inArray[i].unicodeValue())
let k = Int(keyArray[i % keyValue.count])
out += String(Character(UnicodeScalar(Int(c ^ k))!))
}
return out
}
func decrypt(_ keyValue: Array<Int>) -> String {
return self.encrypt(keyValue)
}
}