Fix decryption
This commit is contained in:
parent
1345125252
commit
8ecc55af1f
@ -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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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 {
|
|||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user