diff --git a/Sources/Keycard/Crypto.swift b/Sources/Keycard/Crypto.swift index 7c467a3..d420ba7 100644 --- a/Sources/Keycard/Crypto.swift +++ b/Sources/Keycard/Crypto.swift @@ -22,13 +22,13 @@ class Crypto { } func aes256Enc(data: [UInt8], iv: [UInt8], key: [UInt8]) -> [UInt8] { - let result = try! AES(key: key, blockMode: CBC(iv: iv), padding: .zeroPadding).encrypt(data) + let result = try! AES(key: key, blockMode: CBC(iv: iv), padding: .noPadding).encrypt(data) Logger.shared.log("aes256Enc(data=\(Data(data).toHexString()) iv=\(Data(iv).toHexString()) key=\(Data(key).toHexString())) => \(Data(result).toHexString())") return result } func aes256Dec(data: [UInt8], iv: [UInt8], key: [UInt8]) -> [UInt8] { - let result = try! AES(key: key, blockMode: CBC(iv: iv), padding: .zeroPadding).decrypt(data) + let result = try! AES(key: key, blockMode: CBC(iv: iv), padding: .noPadding).decrypt(data) Logger.shared.log("aes256Dec(data=\(Data(data).toHexString()) iv=\(Data(iv).toHexString()) key=\(Data(key).toHexString())) => \(Data(result).toHexString())") return result } diff --git a/Tests/KeycardTests/CryptoTests.swift b/Tests/KeycardTests/CryptoTests.swift index a4581c8..8c5f203 100644 --- a/Tests/KeycardTests/CryptoTests.swift +++ b/Tests/KeycardTests/CryptoTests.swift @@ -10,8 +10,8 @@ final class CryptoTests: XCTestCase { let (sk, pk) = Crypto.shared.secp256k1GeneratePair() let secret = Crypto.shared.secp256k1ECDH(privKey: sk, pubKey: pk) let iv = Crypto.shared.random(count: SecureChannel.blockLength) - let encrypted = Crypto.shared.aes256Enc(data: plaintextBytes, iv: iv, key: secret) - let decrypted = Crypto.shared.aes256Dec(data: encrypted, iv: iv, key: secret) + let encrypted = Crypto.shared.aes256Enc(data: Crypto.shared.iso7816_4Pad(data: plaintextBytes, blockSize: SecureChannel.blockLength), iv: iv, key: secret) + let decrypted = Crypto.shared.iso7816_4Unpad(data: Crypto.shared.aes256Dec(data: encrypted, iv: iv, key: secret)) let decryptedText = String(data: Data(decrypted), encoding: .utf8) XCTAssertEqual(decryptedText, plaintext)