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

View File

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