-
Notifications
You must be signed in to change notification settings - Fork 0
/
encryptDecrypt.kt
142 lines (132 loc) · 4.3 KB
/
encryptDecrypt.kt
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
package encryptdecrypt
import java.io.*
import java.util.*
import kotlin.system.exitProcess
fun fileCreation(file: String) {
File(file).writeText("")
}
fun getFile(file: String): List<String>{
return File(file).readLines()
}
fun writeFile(directory: String, content: String){
File(directory).appendText(content)
}
fun encrypt(string: String, key: Int, algo: String): String{
var result = ""
var alphabet = ('a'..'z').toList()
when {
algo == "unicode" -> {
string.forEach {
result += it + key
}
}
algo == "shift" -> {
for (i in string) {
if (i == ' ' || !i.isLetter()) {
result += i
}
for (j in alphabet.indices) {
if (i == alphabet[j]) {
Collections.rotate(alphabet, -key)
result += alphabet[j]
Collections.rotate(alphabet, +key)
} else if (i.uppercase() == alphabet[j].uppercase()) {
Collections.rotate(alphabet, -key)
result += alphabet[j].uppercase()
Collections.rotate(alphabet, +key)
}
}
}
}
}
return result
}
fun decrypt(string: String, key: Int, algo: String): String{
var result = ""
var alphabet = ('a'..'z').toList()
when {
algo == "unicode" -> {
string.forEach {
result += it - key
}
}
algo == "shift" -> {
for (i in string) {
if (i == ' ' || !i.isLetter()) {
result += i
}
for (j in alphabet.indices) {
if (i == alphabet[j]) {
Collections.rotate(alphabet, +key)
result += alphabet[j]
Collections.rotate(alphabet, -key)
} else if (i.uppercase() == alphabet[j].uppercase()) {
Collections.rotate(alphabet, +key)
result += alphabet[j].uppercase()
Collections.rotate(alphabet, -key)
}
}
}
}
}
return result
}
fun main(args: Array<String>) {
var mode = "enc"
var key = "0"
var data = ""
var alg = "shift"
var out = ""
var fileName = ""
val storedModeResult = mutableListOf<String>()
for (i in args.indices) {
when {
args[i] == "-mode" -> mode = args[i + 1]
args[i] == "-data" -> data = args[i + 1]
args[i] == "-key" -> key = args[i + 1]
args[i] == "-alg" -> alg = args[i + 1]
args[i] == "-out" -> out = args[i + 1]
args[i] == "-in" -> fileName = args[i + 1]
args[i] == "-in" && args[i].contains("data") -> fileName = ""
}
}
try {
when {
fileName.isNotEmpty() -> {
for (j in getFile(fileName)) {
if (mode == "enc") {
storedModeResult.add(encrypt(j, key.toInt(), alg))
} else if (mode == "dec") {
storedModeResult.add(decrypt(j, key.toInt(), alg))
}
}
}
data.isNotEmpty() || data.isNotEmpty() && fileName.isNotEmpty() -> {
if (mode == "enc") {
storedModeResult.add(encrypt(data, key.toInt(), alg))
} else if (mode == "dec") {
storedModeResult.add(decrypt(data, key.toInt(), alg))
}
}
}
} catch(e: Exception){
println("Error")
exitProcess(1)
}
when {
out.isNotEmpty() -> {
fileCreation(out)
for (i in storedModeResult.indices) {
if (i < storedModeResult.size -1) {
writeFile(out, storedModeResult[i])
writeFile(out, "\n")
} else {
writeFile(out, storedModeResult[i])
}
}
}
out.isEmpty() -> {
println(storedModeResult[0])
}
}
}