@@ -58,19 +58,33 @@ final class AESTests: XCTestCase {
5858 XCTAssertEqual ( decrypted, input, " decryption failed " )
5959 }
6060
61- func testAES_encrypt_cbc ( ) {
61+ func testAES_encrypt_cbc_no_padding ( ) {
6262 let key : [ UInt8 ] = [ 0x2b , 0x7e , 0x15 , 0x16 , 0x28 , 0xae , 0xd2 , 0xa6 , 0xab , 0xf7 , 0x15 , 0x88 , 0x09 , 0xcf , 0x4f , 0x3c ] ;
6363 let iv : [ UInt8 ] = [ 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x08 , 0x09 , 0x0A , 0x0B , 0x0C , 0x0D , 0x0E , 0x0F ]
6464 let plaintext : [ UInt8 ] = [ 0x6b , 0xc1 , 0xbe , 0xe2 , 0x2e , 0x40 , 0x9f , 0x96 , 0xe9 , 0x3d , 0x7e , 0x11 , 0x73 , 0x93 , 0x17 , 0x2a ]
6565 let expected : [ UInt8 ] = [ 0x76 , 0x49 , 0xab , 0xac , 0x81 , 0x19 , 0xb2 , 0x46 , 0xce , 0xe9 , 0x8e , 0x9b , 0x12 , 0xe9 , 0x19 , 0x7d ] ;
66-
66+
6767 let aes = try ! AES ( key: key, iv: iv, blockMode: . CBC)
6868 XCTAssertTrue ( aes. blockMode == . CBC, " Invalid block mode " )
6969 let encrypted = try ! aes. encrypt ( plaintext, padding: nil )
7070 XCTAssertEqual ( encrypted, expected, " encryption failed " )
7171 let decrypted = try ! aes. decrypt ( encrypted, padding: nil )
7272 XCTAssertEqual ( decrypted, plaintext, " decryption failed " )
7373 }
74+
75+ func testAES_encrypt_cbc_with_padding( ) {
76+ let key : [ UInt8 ] = [ 0x2b , 0x7e , 0x15 , 0x16 , 0x28 , 0xae , 0xd2 , 0xa6 , 0xab , 0xf7 , 0x15 , 0x88 , 0x09 , 0xcf , 0x4f , 0x3c ] ;
77+ let iv : [ UInt8 ] = [ 0x00 , 0x01 , 0x02 , 0x03 , 0x04 , 0x05 , 0x06 , 0x07 , 0x08 , 0x09 , 0x0A , 0x0B , 0x0C , 0x0D , 0x0E , 0x0F ]
78+ let plaintext : [ UInt8 ] = [ 0x6b , 0xc1 , 0xbe , 0xe2 , 0x2e , 0x40 , 0x9f , 0x96 , 0xe9 , 0x3d , 0x7e , 0x11 , 0x73 , 0x93 , 0x17 , 0x2a ]
79+ let expected : [ UInt8 ] = [ 0x76 , 0x49 , 0xab , 0xac , 0x81 , 0x19 , 0xb2 , 0x46 , 0xce , 0xe9 , 0x8e , 0x9b , 0x12 , 0xe9 , 0x19 , 0x7d , 0x89 , 0x64 , 0xe0 , 0xb1 , 0x49 , 0xc1 , 0x0b , 0x7b , 0x68 , 0x2e , 0x6e , 0x39 , 0xaa , 0xeb , 0x73 , 0x1c ]
80+
81+ let aes = try ! AES ( key: key, iv: iv, blockMode: . CBC)
82+ XCTAssertTrue ( aes. blockMode == . CBC, " Invalid block mode " )
83+ let encrypted = try ! aes. encrypt ( plaintext, padding: PKCS7 ( ) )
84+ XCTAssertEqual ( encrypted, expected, " encryption failed " )
85+ let decrypted = try ! aes. decrypt ( encrypted, padding: PKCS7 ( ) )
86+ XCTAssertEqual ( decrypted, plaintext, " decryption failed " )
87+ }
7488
7589 func testAES_encrypt_cfb( ) {
7690 let key : [ UInt8 ] = [ 0x2b , 0x7e , 0x15 , 0x16 , 0x28 , 0xae , 0xd2 , 0xa6 , 0xab , 0xf7 , 0x15 , 0x88 , 0x09 , 0xcf , 0x4f , 0x3c ] ;
0 commit comments