-
Notifications
You must be signed in to change notification settings - Fork 0
/
XXTEA.kt
158 lines (135 loc) · 4.51 KB
/
XXTEA.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
object XXTEA {
private const val DELTA = -0x61c88647
@Suppress("NOTHING_TO_INLINE", "FunctionName")
private inline fun MX(sum: Int, y: Int, z: Int, p: Int, e: Int, k: IntArray): Int {
return (z.ushr(5) xor (y shl 2)) + (y.ushr(3) xor (z shl 4)) xor (sum xor y) + (k[p and 3 xor e] xor z)
}
fun encrypt(data: ByteArray, key: ByteArray): ByteArray =
data.takeIf { it.count() != 0 }
?.let {
encrypt(data.toIntArray(true), key.fixKey().toIntArray(false))
.toByteArray(false)
}
?: data
fun encrypt(data: String, key: ByteArray): ByteArray? =
runCatching { encrypt(data.encodeToByteArray(throwOnInvalidSequence = true), key) }.getOrNull()
fun encrypt(data: ByteArray, key: String): ByteArray? =
runCatching { encrypt(data, key.encodeToByteArray(throwOnInvalidSequence = true)) }.getOrNull()
fun encrypt(data: String, key: String): ByteArray? =
runCatching {
encrypt(
data.encodeToByteArray(throwOnInvalidSequence = true),
key.encodeToByteArray(throwOnInvalidSequence = true)
)
}.getOrNull()
fun decrypt(data: ByteArray, key: ByteArray): ByteArray =
data.takeIf { it.count() != 0 }
?.let {
decrypt(data.toIntArray(false), key.fixKey().toIntArray(false))
.toByteArray(true)
} ?: data
fun decrypt(data: ByteArray, key: String): ByteArray? =
kotlin.runCatching { decrypt(data, key.encodeToByteArray(throwOnInvalidSequence = true)) }.getOrNull()
fun decryptToString(data: ByteArray, key: ByteArray): String? =
kotlin.runCatching { decrypt(data, key).decodeToString(throwOnInvalidSequence = true) }.getOrNull()
fun decryptToString(data: ByteArray, key: String): String? =
kotlin.runCatching { decrypt(data, key)?.decodeToString(throwOnInvalidSequence = true) }.getOrNull()
private fun encrypt(v: IntArray, k: IntArray): IntArray {
val n = v.size - 1
if (n < 1) {
return v
}
var p: Int
var q = 6 + 52 / (n + 1)
var z = v[n]
var y: Int
var sum = 0
var e: Int
while (q-- > 0) {
sum += DELTA
e = sum.ushr(2) and 3
p = 0
while (p < n) {
y = v[p + 1]
v[p] += MX(sum, y, z, p, e, k)
z = v[p]
p++
}
y = v[0]
v[n] += MX(sum, y, z, p, e, k)
z = v[n]
}
return v
}
private fun decrypt(v: IntArray, k: IntArray): IntArray {
val n = v.size - 1
if (n < 1) {
return v
}
var p: Int
val q = 6 + 52 / (n + 1)
var z: Int
var y = v[0]
var sum = q * DELTA
var e: Int
while (sum != 0) {
e = sum.ushr(2) and 3
p = n
while (p > 0) {
z = v[p - 1]
v[p] -= MX(sum, y, z, p, e, k)
y = v[p]
p--
}
z = v[n]
v[0] -= MX(sum, y, z, p, e, k)
y = v[0]
sum -= DELTA
}
return v
}
private fun ByteArray.fixKey(): ByteArray {
if (size == 16) return this
val fixedKey = ByteArray(16)
if (size < 16) {
copyInto(fixedKey)
} else {
copyInto(fixedKey, endIndex = 16)
}
return fixedKey
}
private fun ByteArray.toIntArray(includeLength: Boolean): IntArray {
var n = if (size and 3 == 0)
size.ushr(2)
else
size.ushr(2) + 1
val result: IntArray
if (includeLength) {
result = IntArray(n + 1)
result[n] = size
} else {
result = IntArray(n)
}
n = size
for (i in 0 until n) {
result[i.ushr(2)] = result[i.ushr(2)] or (0x000000ff and this[i].toInt() shl (i and 3 shl 3))
}
return result
}
private fun IntArray.toByteArray(includeLength: Boolean): ByteArray? {
var n = size shl 2
if (includeLength) {
val m = this[size - 1]
n -= 4
if (m < n - 3 || m > n) {
return null
}
n = m
}
val result = ByteArray(n)
for (i in 0 until n) {
result[i] = this[i.ushr(2)].ushr(i and 3 shl 3).toByte()
}
return result
}
}