Fix decryption

This commit is contained in:
Ugljesa Jovanovic 2019-09-21 22:39:59 +02:00 committed by Ugljesa Jovanovic
parent 1345125252
commit 8ecc55af1f
No known key found for this signature in database
GPG Key ID: 33A5F353387711A5
2 changed files with 6 additions and 3 deletions

View File

@ -18,6 +18,7 @@ package com.ionspin.kotlin.crypto.symmetric
import com.ionspin.kotlin.crypto.SRNG
import com.ionspin.kotlin.crypto.chunked
import com.ionspin.kotlin.crypto.toHexString
import com.ionspin.kotlin.crypto.xor
/**
@ -145,10 +146,10 @@ class AesCbc internal constructor(val aesKey: AesKey, val mode: Mode, initializa
Mode.DECRYPT -> {
if (currentOutput.isEmpty()) {
currentOutput = Aes.decrypt(aesKey, data) xor iv
previousEncrypted = data
} else {
currentOutput = Aes.decrypt(aesKey, data) xor previousEncrypted
}
previousEncrypted = data
currentOutput
}
}

View File

@ -38,7 +38,7 @@ class AesCbcTest {
val aesCbc = AesCbc(AesKey.Aes128Key(key), mode = Mode.ENCRYPT, initializationVector = iv.hexStringToUByteArray())
aesCbc.addData(plaintext.hexStringToUByteArray())
val encrypted = aesCbc.encrypt()
println("Decrypted: ${encrypted.toHexString()}")
println("Encrypted: ${encrypted.toHexString()}")
assertTrue {
expectedCipherText == encrypted.toHexString()
}
@ -54,7 +54,7 @@ class AesCbcTest {
val aesCbc = AesCbc(AesKey.Aes128Key(key), mode = Mode.DECRYPT, initializationVector = iv.hexStringToUByteArray())
aesCbc.addData(cipherText.hexStringToUByteArray())
val decrypted = aesCbc.decrypt()
println("Encrypted: ${decrypted.toHexString()}")
println("Decrypted: ${decrypted.toHexString()}")
assertTrue {
expectedPlainText == decrypted.toHexString()
}
@ -64,4 +64,6 @@ class AesCbcTest {
}