-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEncryptionManager.kt
More file actions
89 lines (78 loc) · 2.05 KB
/
EncryptionManager.kt
File metadata and controls
89 lines (78 loc) · 2.05 KB
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
package utils
import kotlin.math.ceil
import kotlin.math.floor
import kotlin.math.min
import kotlin.math.sqrt
internal object EncryptionManager {
/**
* Function to encrypt the String unsing a MATRIXTRANSPOSITION
*/
fun encryption(s: String): String {
val l = s.length
var b: Int = ceil(sqrt(l.toDouble())).toInt()
var a: Int = floor(sqrt(l.toDouble())).toInt()
var encrypted = ""
if (b * a < l) {
if (min(b, a) == b) {
b += 1
} else {
a += 1
}
}
// Matrix to generate the Encrypted String
val arr = Array(a) {
CharArray(b){
' '
}
}
var k = 0
// Fill the matrix row-wise
for (j in 0 until a) {
for (i in 0 until b) {
if (k < l) {
arr[j][i] = s[k]
}
k++
}
}
// Loop to generate encrypted String
for (j in 0 until b) {
for (i in 0 until a) {
encrypted += arr[i][j]
}
}
return encrypted
}
/**
* Function to decrypt the String
*/
fun decryption(s: String): String {
val l = s.length
val b: Int = ceil(sqrt(l.toDouble())).toInt()
val a: Int = floor(sqrt(l.toDouble())).toInt()
var decrypted = ""
// Matrix to generate the Encrypted String
val arr = Array(a) {
CharArray(b) { ' ' }
}
var k = 0
// Fill the matrix column-wise
for (j in 0 until b) {
for (i in 0 until a) {
if (k < l) {
arr[i][j] = s[k]
k++
}
}
}
// Loop to generate decrypted String
for (i in 0 until a) {
for (j in 0 until b) {
if (arr[i][j] != ' ') {
decrypted += arr[i][j]
}
}
}
return decrypted
}
}