Last active
August 27, 2019 09:10
-
-
Save Syrou/52db691f91928cb6aad6bcfd84bf5cff to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun String.toUByteArray(): UByteArray { | |
return this.toCharArray() | |
.foldIndexed(UByteArray(this.length)) { i, a, v -> a.apply { set(i, v.toByte().toUByte()) } } | |
} | |
fun ByteArray.toUByteArray(): UByteArray { | |
return this.foldIndexed(UByteArray(this.size)) { i, a, v -> a.apply { set(i, v.toByte().toUByte()) } } | |
} | |
fun CharArray.toUByteArray(): UByteArray { | |
return this.foldIndexed(UByteArray(this.size)) { i, a, v -> a.apply { set(i, v.toByte().toUByte()) } } | |
} | |
fun UByteArray.toHexString() = this.joinToString("") { it.toString(16).padStart(2, '0') + " " } | |
fun testAes(input: String = "asdfasdf") { | |
val key = ubyteArrayOf(15u, 14u, 13u, 12u, 11u, 10u, 9u, 8u, 7u, 6u, 5u, 4u, 3u, 2u, 1u, 0u) | |
val iv = ubyteArrayOf(0u, 1u, 2u, 3u, 4u, 5u, 6u, 7u, 8u, 9u, 10u, 11u, 12u, 13u, 14u, 15u) | |
val length = 16 | |
val output = UByteArray(length).usePinned { pinned -> | |
AES_CBC_encrypt_buffer( | |
pinned.addressOf(0), | |
input.toUByteArray().toCValues(), | |
(pinned.get().size - 1).convert(), | |
key.toCValues(), | |
iv.toCValues() | |
) | |
pinned.get() | |
} | |
println(output.toHexString()) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment