title: iOS Swift 4:How to perform encryption/decryption with DES-ECB-PKCS5Padding?
body: <p>I want to perform encryption/decryption with <strong>DES-ECB-PKCS5Padding</strong> in iOS Swift.</p>
  
  <p>I have some code from <strong>Server Side</strong> (most probably in ActionScript) to help which is as follows:</p>
  
  <pre><code>private static const type:String='simple-des-ecb';
  
  public static function encrypt(txt:String, salt:String): String
  {
      var key:ByteArray = Hex.toArray(Hex.fromString(salt));
      var data:ByteArray = Hex.toArray(Hex.fromString(txt));
      var pad:IPad = new PKCS5;
      var mode:ICipher = Crypto.getCipher(type, key, pad);
      pad.setBlockSize(mode.getBlockSize());
      mode.encrypt(data);
      data.position = 0;
      return Base64.encodeByteArray(data);
  }
  
  public static function decrypt(txt:String, salt:String): String
  {
      var key:ByteArray = Hex.toArray(Hex.fromString(salt));
      var data:ByteArray = Base64.decodeToByteArray(txt);
      var pad:IPad = new PKCS5;
      var mode:ICipher = Crypto.getCipher(type, key, pad);
      pad.setBlockSize(mode.getBlockSize());
      try
      {
          mode.decrypt(data);
      }
      catch (e:Error)
      {
          trace(e.message);
          trace(e.getStackTrace());
      }
      return Hex.toString(Hex.fromArray(data));
  }
  </code></pre>
  
  <p>I have tried to reciprocate the above functions in <strong>Swift</strong>, but I'm unable to get desired results</p>
  
  <p>My Swift code goes as follows:</p>
  
  <pre><code>func encrypt(text: String, salt: String) -&gt; String {
  
      let strHexKey = hexFromString(string: salt)
      let key = strHexKey.hexaBytes
  
      let strHexData = hexFromString(string: text)
      let data = strHexData.hexaBytes
  
      let cryptor = Cryptor(operation: .encrypt, algorithm: .des, options: .PKCS7Padding, key: key, iv: Array&lt;UInt8&gt;())
      let cipherText = cryptor.update(byteArray: data)?.final()
  
      let strBase64 = cipherText!.data.base64EncodedString()
  
      return strBase64
  
  }
  
  func decrypt(text: String, salt: String) -&gt; String {
  
      let strHexKey = hexFromString(string: salt)
      let key = arrayFrom(hexString: strHexKey)
  
      let data = text.fromBase64()?.data(using: .utf8)
  
      let cryptor = Cryptor(operation: .decrypt, algorithm: .des, options: .PKCS7Padding, key: key, iv: Array&lt;UInt8&gt;())
      let cipherText = cryptor.update(data: data!)!.final()
  
      return (cipherText?.hexa)!
  
  }
  </code></pre>
  
  <p>I have been using <a href="https://github.com/iosdevzone/IDZSwiftCommonCrypto" rel="nofollow noreferrer">https://github.com/iosdevzone/IDZSwiftCommonCrypto</a> for Cryptor Library.</p>