title: Swift - AES 128/256 Encryption different result
body: <p>I'm trying to encrypt with AES based on this code: </p>
  
  <p><a href="https://github.com/eclair4151/SmartCrypto/blob/master/PySmartCrypto/crypto.py" rel="nofollow noreferrer">https://github.com/eclair4151/SmartCrypto/blob/master/PySmartCrypto/crypto.py</a> </p>
  
  <p>Python code: </p>
  
  <pre><code>cipher = AES.new(aes_key, AES.MODE_CBC, iv)
      encrypted = cipher.encrypt(bytes.fromhex(keys.publicKey))
      print("AES encrypted: "+ encrypted.hex())
  </code></pre>
  
  <p>I also using this question to try solving my problem: <a href="https://stackoverflow.com/questions/37680361/aes-encryption-in-swift">AES encryption in swift</a></p>
  
  <p>This is my code that I took from the question above: </p>
  
  <pre><code>import Foundation
  import CommonCrypto
  
  struct AES {
  
      // MARK: - Value
      // MARK: Private
      private let key: Data
      private let iv: Data
  
  
      // MARK: - Initialzier
      init?(key: String, iv: String) {
          guard key.count == kCCKeySizeAES128 || key.count == kCCKeySizeAES256, let keyData = key.data(using: .utf8) else {
              debugPrint("Error: Failed to set a key.")
              return nil
          }
  
          guard iv.count == kCCBlockSizeAES128, let ivData = iv.data(using: .utf8) else {
              debugPrint("Error: Failed to set an initial vector.")
              return nil
          }
  
  
          self.key = keyData
          self.iv  = ivData
      }
  
  
      // MARK: - Function
      // MARK: Public
      func encrypt(string: String) -&gt; Data? {
          return crypt(data: string.data(using: .utf8), option: CCOperation(kCCEncrypt))
      }
  
      func decrypt(data: Data?) -&gt; String? {
          guard let decryptedData = crypt(data: data, option: CCOperation(kCCDecrypt)) else { return nil }
          return String(bytes: decryptedData, encoding: .utf8)
      }
  
      func crypt(data: Data?, option: CCOperation) -&gt; Data? {
          guard let data = data else { return nil }
  
          let cryptLength = data.count + kCCBlockSizeAES128
          var cryptData   = Data(count: cryptLength)
  
          let keyLength = key.count
          let options   = CCOptions(kCCOptionPKCS7Padding)
  
          var bytesLength = Int(0)
  
          let status = cryptData.withUnsafeMutableBytes { cryptBytes in
              data.withUnsafeBytes { dataBytes in
                  iv.withUnsafeBytes { ivBytes in
                      key.withUnsafeBytes { keyBytes in
                      CCCrypt(option, CCAlgorithm(kCCAlgorithmAES), options, keyBytes.baseAddress, keyLength, ivBytes.baseAddress, dataBytes.baseAddress, data.count, cryptBytes.baseAddress, cryptLength, &amp;bytesLength)
                      }
                  }
              }
          }
  
          guard UInt32(status) == UInt32(kCCSuccess) else {
              debugPrint("Error: Failed to crypt data. Status \(status)")
              return nil
          }
  
          cryptData.removeSubrange(bytesLength..&lt;cryptData.count)
          return cryptData
      }
  }
  
  let password = "2cb12bb2cbf7cec713c0fff7b59ae68a96784ae517f41d259a45d20556177c0ffe951ca60ec03a990c9412619d1bee30adc7773088c5721664cffcedacf6d251cb4b76e2fd7aef09b3ae9f9496ac8d94ed2b262eee37291c8b237e880cc7c021fb1be0881f3d0bffa4234d3b8e6a61530c00473ce169c025f47fcc001d9b8051"
  let key128   = "1234567890123456"                   // 16 bytes for AES128
  let key256   = "7110eda4d09e062aa5e4a390b0a572ac"   // 32 bytes for AES256
  let iv       = "0000000000000000"                   // 16 bytes for AES128
  
  let aes128 = AES(key: key128, iv: iv)
  let aes256 = AES(key: key256, iv: iv)
  
  print(password.count)
  let encryptedPassword128 = aes128?.encrypt(string: password)
  aes128?.decrypt(data: encryptedPassword128)
  
  let encryptedPassword256 = aes256?.encrypt(string: password)
  aes256?.decrypt(data: encryptedPassword256)
  </code></pre>
  
  <p>This GitHub code is for generate code and connecting to Samsung tv. I run the code in python and my result is : </p>
  
  <pre><code>AES key: 7110eda4d09e062aa5e4a390b0a572ac
  AES encrypted: ee866a1427e32c057ec3d5cbb0b227f61c9958f0b85685bfec8654ea964db04baea6af4e95b00b6cd22d0f15175ac48203ba4b1181a6b84204a44e4b81b57f4be024372402e59bbadedbdfed38d2e2231abc6a360d327e21d4dd0f6fa55c8a6004a30f0f44ca101966fc7d70585ff5bb77a34d3dc0528a5c476a916696183b80
  </code></pre>
  
  <p>This AES encrypted is 256 length and my result is 544 length. </p>
  
  <pre><code>a1d745160fa1742c05e3798d55499bbaf2d249c0c367688d37a479d6e7d903e90e45a9d495804cdccb1f69c2b97430484a023124659dc37cde6fb21caccae20d65f4f6412f3ee2b4ec487aa0ed5fa546c6a848fefc405f419188f8f332da437e06fd1a356d8b4586d9ba68dad2fede9912b89df4363158bdb0c0e32a19fb383df521949624b0c5512da2debbd81febb7b30f8a0e678c7a0f5a03cf2ef6c647cc485e31b36163ee5f6c9fa16ba54b19c6ebd2c24cbe260acf847d5285d145521ba5028a9437ea95c2b21e3cf25d474b36851ca9657425ec3e875459b0652d507d786cd66760265e9a66f57769db72022c988cf8aa4c0891361d9f32d6b487df7725c05c7966c68be4a4582da1d4e0b047
  </code></pre>
  
  <p>I'm using the right key: 7110eda4d09e062aa5e4a390b0a572ac
  IV: 0000000000000000 and the public key is the same as GitHub publicKey. </p>
  
  <p>Why my result is different? how can I get 256 length string?</p>
  
  <p>Thanks!</p>