@Kognise/

dumB0i's Kryptos

Swift

code jam entry of sanity

fork
loading
Files
  • main.swift
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
//import UIKit
// str is whats to be ciphered
var str = "The images SANITY of scenery @ the event wrote a program. However It showed too much in console! what what! owo"

var stringRay = ""
let strHolder = Array(str)
print (strHolder)
//keylength is the length of the decryption key
var keyLength = (str).count
// I hold the letters as numerical values before ciphering
var numArray:[Int] = []

print (str)
print (keyLength)

//I hold the cipher text that is back in text form

var backtoChars:[String] = []

//I am the array that holdes the cipher after it has addition performed.
var cipherHolder:[Int] = []

//key is a series of numbers the characters are shifted by. the key is as long as the message
var key:[Int] = []

//char is a character values before ciphering eg. a=0, z=25
 var char:[String:Int] = [:]

//all possible chars on a US keyboard in an Array
var letters:[String] = ["a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","1","2","3","4","5","6","7","8","9","0","!","@","#","$","%","^","&","*","(",")","-","_","=","+","[","{","]","}","|","\\",";",":",",","<",">","/","?"," ","`","." ]

//I map the alphabet to numerical values, inside a dictionary, that way, we can output a numerical arrary from an array of strings, then the encrypt function will use additon use additon to our random numbers key, to shift the values using a modullo. if it is higher than 92, they will begin at index 0 again within they key array

func alphabetBoi(){
    
    print (" in the array there are \(letters.count) ")
    //iter is the count of how many times the loop has repeated. it is used to write to the dictionary and referance the array properly
    
    var iter = 0
    
    for n in letters{
        while iter < 92{
        char["\(letters[(iter)])"] = (iter)
        print (iter)
            print ("the value of \(iter) is \(letters[(iter)]) ")
           
            
            iter += 1
        }
        //anything you put here  will be repeated 90 some times
    }
    print ("cleartext letter dictionary is as follows")
    print (char)
    print ("all done making the cleartext letter value dictionary")
}

func Keygen(){
    var keyIndex = 0
    for keyLength in str{
        //key index is used to index the array. ex Arraytest[t,e,s,t] then keyindex 0 will see index 0 of 2 arrays
        //and add those indexes into a thrid array named key
        //put code before here so keyindex 0 is encoded
        var secure = Int.random(in: 0...91)
        key.append(secure)
        keyIndex += 1
        print (keyIndex)
        print ("your personal key is \(key)")
    }
}
var x = 0
func numBoi(){
    //var x = 0
    while x < keyLength{
        //here we convert strholder to numbers using the dictionary
        print (strHolder[x])
        numArray.append (char["\(strHolder[x])"]!)
        print (numArray)
        
        x += 1
   
    }
    //print (numArray)
    
}

func encryptor(){
    var reps = 0
    while reps < keyLength{
        cipherHolder.append (numArray[reps] + key[reps])
        if cipherHolder[reps] > 92{
            cipherHolder[reps] = (cipherHolder[reps] - 92)
        }
        
        reps += 1
    }
    print ("your key is \(key)")
    print ("your ciphered text is \(cipherHolder) in numeric form")
}

func textAgain(){
    var g = 0
    while g < keyLength{
        
       backtoChars.append (letters[cipherHolder[g]])
        
        g += 1
        
    }
    stringRay = backtoChars.joined()
    print (backtoChars)
    print (stringRay)
}

Keygen()
alphabetBoi()
numBoi()
encryptor()
textAgain()