Working ctr decryption

This commit is contained in:
Ugljesa Jovanovic 2019-09-23 22:14:42 +02:00 committed by Ugljesa Jovanovic
parent 18ac28f3c3
commit 69c81ec8e9
No known key found for this signature in database
GPG Key ID: 33A5F353387711A5

View File

@ -133,7 +133,11 @@ class AesCtr internal constructor(val aesKey: AesKey, val mode: Mode, initialCou
fun decrypt(): Array<UByte> { fun decrypt(): Array<UByte> {
val removePaddingCount = output.last().last() val removePaddingCount = output.last().last()
val removedPadding = output.last().dropLast(removePaddingCount.toInt() and 0x7F) val removedPadding = if (removePaddingCount > 0U && removePaddingCount < 16U) {
output.last().dropLast(removePaddingCount.toInt() and 0x7F)
} else {
output.last().toList()
}
val preparedOutput = output.dropLast(1).toTypedArray() + removedPadding.toTypedArray() val preparedOutput = output.dropLast(1).toTypedArray() + removedPadding.toTypedArray()
//JS compiler freaks out here if we don't supply exact type //JS compiler freaks out here if we don't supply exact type
val reversed : List<Array<UByte>> = preparedOutput.reversed() as List<Array<UByte>> val reversed : List<Array<UByte>> = preparedOutput.reversed() as List<Array<UByte>>
@ -153,7 +157,7 @@ class AesCtr internal constructor(val aesKey: AesKey, val mode: Mode, initialCou
Aes.encrypt(aesKey, blockCount.toUByteArray(Endianness.BIG)) xor data Aes.encrypt(aesKey, blockCount.toUByteArray(Endianness.BIG)) xor data
} }
Mode.DECRYPT -> { Mode.DECRYPT -> {
Aes.decrypt(aesKey, blockCount.toUByteArray(Endianness.BIG)) xor data Aes.encrypt(aesKey, blockCount.toUByteArray(Endianness.BIG)) xor data
} }
} }