Skip to content

xJoeWoo/xxtea-kotlin

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 

Repository files navigation

XXTEA for Kotlin

Introduction

XXTEA encryption algorithm library for Kotlin. Imported from xxtea-java with version 1.0.5

Usage

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))
}

Encoding

If you need to change default encoding of XXTEA, just call

XXTea.defaultCharset = [charset]

before use it.

About

XXTEA encryption algorithm library for Kotlin.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages