lmw
2024-07-09 b13afc751dbbce24753d008f1f87d2c5e133a4ad
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
package cn.sinata.util
 
import java.io.ByteArrayOutputStream
import java.io.IOException
 
object Base64DES {
 
    private val legalChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".toCharArray()
 
    fun encode(data: ByteArray): String {
 
        val start = 0
        val len = data.size
        val buf = StringBuffer(data.size * 3 / 2)
        val end = len - 3
        var i = start
        var n = 0
        while (i <= end) {
            val d = data[i].toInt() and 0x0ff shl 16 or (data[i + 1].toInt() and 0x0ff shl 8) or (data[i + 2].toInt() and 0x0ff)
 
            buf.append(legalChars[d shr 18 and 63])
            buf.append(legalChars[d shr 12 and 63])
            buf.append(legalChars[d shr 6 and 63])
            buf.append(legalChars[d and 63])
            i += 3
            if (n++ >= 14) {
                n = 0
                buf.append(" ")
            }
        }
        if (i == start + len - 2) {
            val d = data[i].toInt() and 0x0ff shl 16 or (data[i + 1].toInt() and 255 shl 8)
 
            buf.append(legalChars[d shr 18 and 63])
            buf.append(legalChars[d shr 12 and 63])
            buf.append(legalChars[d shr 6 and 63])
            buf.append("=")
 
        } else if (i == start + len - 1) {
            val d = data[i].toInt() and 0x0ff shl 16
            buf.append(legalChars[d shr 18 and 63])
            buf.append(legalChars[d shr 12 and 63])
            buf.append("==")
        }
        return buf.toString()
    }
 
    fun decode(s: String): ByteArray {
 
        var bos: ByteArrayOutputStream? = ByteArrayOutputStream()
        try {
            decode(s, bos!!)
        } catch (e: IOException) {
            throw RuntimeException()
        }
        val decodedBytes = bos.toByteArray()
        try {
            bos.close()
            bos = null
        } catch (ex: IOException) {
            System.err.println("Error while decoding BASE64: " + ex.toString())
        }
        return decodedBytes
    }
 
    private fun decode(s: String, os: ByteArrayOutputStream) {
        var i = 0
        val len = s.length
        while (true) {
            while (i < len && s[i] <= ' ')
                i++
            if (i == len)
                break
            val tri = ((decode(s[i]) shl 18)
                    + (decode(s[i + 1]) shl 12)
                    + (decode(s[i + 2]) shl 6)
                    + decode(s[i + 3]))
            os.write(tri shr 16 and 255)
            if (s[i + 2] == '=')
                break
            os.write(tri shr 8 and 255)
            if (s[i + 3] == '=')
                break
            os.write(tri and 255)
            i += 4
        }
    }
 
    private fun decode(c: Char): Int = when (c) {
        in 'A'..'Z' -> c.toInt() - 65
        in 'a'..'z' -> c.toInt() - 97 + 26
        in '0'..'9' -> c.toInt() - 48 + 26 + 26
        else -> when (c) {
            '+' -> 62
            '/' -> 63
            '=' -> 0
            else -> throw RuntimeException("unexpected code: " + c)
        }
    }
}