XXTEA encryption algorithm library for Kotlin. Imported from xxtea-java with version 1.0.5
First, copy coresponding platform source file to your project:
- JVM, Android (Better to fix package info with your own package path on top of this source file)
- Native
Second:
fun main() {
// ------------- String -------------
val str = "Hello World! 你好,中国!"
val key = "1234567890"
// Returns null if decode from string to byte array fails
val encryptData = XXTEA.encrypt(str, key)
// Default charset is UTF_8. To use other charsets:
// val encryptData = XXTEA.encrypt(str, key, Charsets.US_ASCII)
// val encryptData = XXTEA.encrypt(str, key, Charset.forName("GBK"))
val decryptData = XXTEA.decryptToString(encryptData!!, key)
assert(str == decryptData)
// ------------- ByteArray -------------
val strBytes = str.toByteArray()
val keyBytes = key.toByteArray()
val encryptBytesData = XXTEA.encrypt(strBytes, keyBytes)
val decryptBytesData = XXTEA.decrypt(encryptBytesData, keyBytes)
assert(strBytes.contentEquals(decryptBytesData))
}
If you need to change default encoding of XXTEA
, just call
XXTea.defaultCharset = [charset]
before use it.